A personal rust firmware for the Badger 2040 W

time updates

Changed files
+41 -60
src
+41 -60
src/main.rs
··· 1 1 #![no_std] 2 2 #![no_main] 3 3 4 + use crate::pcf85063a::Control; 4 5 use badge_display::display_image::DisplayImage; 5 6 use badge_display::{ 6 7 CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, FORCE_SCREEN_REFRESH, RECENT_WIFI_NETWORKS, ··· 45 46 use serde::Deserialize; 46 47 use static_cell::StaticCell; 47 48 use temp_sensor::run_the_temp_sensor; 49 + use time::PrimitiveDateTime; 48 50 use {defmt_rtt as _, panic_probe as _}; 49 51 50 52 mod badge_display; ··· 122 124 let btn_a = Input::new(p.PIN_12, Pull::Down); 123 125 let btn_b = Input::new(p.PIN_13, Pull::Down); 124 126 let btn_c = Input::new(p.PIN_14, Pull::Down); 127 + let rtc_alarm = Input::new(p.PIN_8, Pull::Down); 125 128 126 129 let spi = Spi::new( 127 130 p.SPI0, ··· 156 159 ); 157 160 158 161 //rtc setup 159 - let mut rtc = embassy_rp::rtc::Rtc::new(p.RTC); 162 + // let mut rtc = embassy_rp::rtc::Rtc::new(p.RTC); 160 163 161 164 spawner.must_spawn(net_task(runner)); 162 165 //Attempt to connect to wifi to get RTC time loop for 2 minutes ··· 193 196 let i2c_dev = I2cDevice::new(i2c_bus); 194 197 let mut rtc_device = PCF85063::new(i2c_dev); 195 198 196 - let mut time_was_set = false; 197 199 if connected_to_wifi { 198 200 info!("waiting for DHCP..."); 199 201 while !stack.is_config_up() { ··· 291 293 second: second as u8, 292 294 }; 293 295 294 - // // prepare date and time to be set 295 - // let now = DateTime { 296 - // year: 21, // 2021 297 - // month: 4, // April 298 - // weekday: 0, // Sunday 299 - // day: 4, 300 - // day_of_week: DayOfWeek::Sunday, 301 - // hour, 302 - // minute, 303 - // hours: 16, 304 - // minutes: 52, 305 - // seconds: 00, 306 - // second: 0, 307 - // }; 308 - 309 296 rtc_device 310 297 .set_datetime(&rtc_time) 311 298 .expect("TODO: panic message"); 312 - // rtc.set_datetime(rtc_time).unwrap(); 313 - time_was_set = true; 314 - let _ = control.leave().await; 315 299 } 316 300 Err(_e) => { 317 301 error!("Failed to parse response body"); ··· 324 308 // return; // handle the error 325 309 } 326 310 }; 311 + //leave the wifi no longer needed 312 + let _ = control.leave().await; 327 313 } 328 314 329 315 //Set up saving 330 316 let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH3); 331 - //TODO baaaaaad 332 317 let mut save = 333 318 read_postcard_from_flash(ADDR_OFFSET, &mut flash, SAVE_OFFSET).unwrap_or_else(|err| { 334 319 error!("Error getting the save from the flash: {:?}", err); ··· 344 329 let cycle = Duration::from_millis(100); 345 330 let mut current_cycle = 0; 346 331 let mut time_to_scan = true; 347 - //5 minutes(ish) idk it's late and my math is so bad rn 348 - let reset_cycle = 3_000; 332 + //15 minutes(ish) idk it's late and my math is so bad rn 333 + let reset_cycle = 9_000; 334 + 349 335 //Turn off led to signify that the badge is ready 350 336 user_led.set_low(); 351 337 352 - loop { 353 - //TODO take out 354 - Timer::after(Duration::from_millis(5_000)).await; 355 - 356 - let time = rtc_device.get_datetime().unwrap(); 357 - info!("Hour: {:?}", time.hour()); 358 - info!("Minute: {:?}", time.minute()); 359 - info!("Second: {:?}", time.second()); 338 + //RTC alarm stuff 339 + // info!("going to sleep"); 340 + // Timer::after(Duration::from_millis(5_000)).await; 341 + // //Set the rtc and sleep for 15 minutes 342 + // //goes to sleep for 15 mins 343 + // _ = rtc_device.clear_alarm_flag(); 344 + // _ = rtc_device.set_alarm_minutes(5); 345 + // _ = rtc_device.control_alarm_minutes(Control::On); 346 + // _ = rtc_device.control_alarm_interrupt(Control::On); 347 + // power.set_low(); 360 348 361 - // match rtc_device.get_datetime() { 362 - // Ok(time) => { 363 - // info!("RTC time: {:?}", time.as_utc().time().minute()); 364 - // } 365 - // Err(err) => { 366 - // error!("Error reading rtc"); 367 - // } 368 - // } 369 - 349 + loop { 370 350 //Change Image Button 371 351 if btn_c.is_high() { 372 352 info!("Button C pressed"); ··· 408 388 409 389 if btn_b.is_high() { 410 390 info!("Button B pressed"); 411 - 412 391 SCREEN_TO_SHOW.lock(|screen| { 413 392 if *screen.borrow() == Screen::Badge { 414 393 //IF on badge screen and b pressed reset wifi count ··· 455 434 continue; 456 435 } 457 436 458 - if time_was_set { 459 - let now = rtc.now(); 460 - match now { 461 - Ok(time) => set_display_time(time), 462 - Err(_) => { 463 - info!("Error getting time"); 464 - } 437 + match rtc_device.get_datetime() { 438 + Ok(now) => set_display_time(now), 439 + Err(_err) => { 440 + error!("Error getting time"); 441 + RTC_TIME_STRING.lock(|rtc_time_string| { 442 + rtc_time_string.borrow_mut().clear(); 443 + rtc_time_string.borrow_mut().push_str("Error").unwrap(); 444 + }); 465 445 } 466 - } else { 467 - RTC_TIME_STRING.lock(|rtc_time_string| { 468 - rtc_time_string.borrow_mut().clear(); 469 - rtc_time_string.borrow_mut().push_str("No Wifi").unwrap(); 470 - }); 471 - } 446 + }; 447 + 472 448 if time_to_scan { 473 449 info!("Scanning for wifi networks"); 474 450 time_to_scan = false; ··· 489 465 } 490 466 } 491 467 492 - fn set_display_time(time: DateTime) { 468 + fn set_display_time(time: PrimitiveDateTime) { 493 469 let mut am = true; 494 - let twelve_hour = if time.hour > 12 { 470 + let twelve_hour = if time.hour() == 0 { 471 + 12 472 + } else if time.hour() == 12 { 495 473 am = false; 496 - time.hour - 12 497 - } else if time.hour == 0 { 498 474 12 475 + } else if time.hour() > 12 { 476 + am = false; 477 + time.hour() - 12 499 478 } else { 500 - time.hour 479 + time.hour() 501 480 }; 502 481 503 482 let am_pm = if am { "AM" } else { "PM" }; 504 483 505 484 let formatted_time = easy_format::<8>(format_args!( 506 485 "{:02}:{:02} {}", 507 - twelve_hour, time.minute, am_pm 486 + twelve_hour, 487 + time.minute(), 488 + am_pm 508 489 )); 509 490 510 491 RTC_TIME_STRING.lock(|rtc_time_string| {