A personal rust firmware for the Badger 2040 W

Decent enough?

+17 -13
+14 -11
src/badge_display/mod.rs
··· 37 //Display state 38 pub static SCREEN_TO_SHOW: blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<Screen>> = 39 blocking_mutex::Mutex::new(RefCell::new(Screen::Badge)); 40 pub static DISPLAY_CHANGED: AtomicBool = AtomicBool::new(false); 41 pub static CURRENT_IMAGE: AtomicU8 = AtomicU8::new(0); 42 pub static CHANGE_IMAGE: AtomicBool = AtomicBool::new(true); ··· 91 env_value("DETAILS") 92 )); 93 94 - // \nWritten in rust\nRunning on a pico w"; 95 let name_and_detail_box = TextBox::with_textbox_style( 96 &display_text, 97 name_and_detail_bounds, ··· 99 textbox_style, 100 ); 101 102 - // Draw the text box. 103 - name_and_detail_box.draw(&mut display).unwrap(); 104 - 105 // let _ = display.update().await; 106 107 //Each cycle is half a second 108 let cycle: Duration = Duration::from_millis(500); 109 110 - let mut first_run = true; 111 //New start every 120 cycles or 60 seconds 112 let cycles_to_clear_at: i32 = 120; 113 let mut cycles_since_last_clear = 0; 114 let mut current_screen = Screen::Badge; 115 loop { 116 //Timed based display events 117 if DISPLAY_CHANGED.load(core::sync::atomic::Ordering::Relaxed) { 118 let clear_rectangle = Rectangle::new(Point::new(0, 0), Size::new(WIDTH, HEIGHT)); ··· 122 .unwrap(); 123 let _ = display.update().await; 124 DISPLAY_CHANGED.store(false, core::sync::atomic::Ordering::Relaxed); 125 - first_run = true; 126 } 127 128 SCREEN_TO_SHOW.lock(|x| current_screen = *x.borrow()); 129 info!("Current Screen: {:?}", current_screen); 130 if current_screen == Screen::Badge { 131 //Updates the top bar 132 //Runs every 60 cycles/30 seconds and first run 133 - if cycles_since_last_clear % 60 == 0 || first_run { 134 let count = WIFI_COUNT.load(core::sync::atomic::Ordering::Relaxed); 135 let temp = TEMP.load(core::sync::atomic::Ordering::Relaxed); 136 let humidity = HUMIDITY.load(core::sync::atomic::Ordering::Relaxed); ··· 165 } 166 167 //Runs every 120 cycles/60 seconds and first run 168 - if cycles_since_last_clear == 0 || first_run { 169 let mut time_text: String<8> = String::<8>::new(); 170 171 let time_box_rectangle_location = Point::new(0, 96); ··· 212 213 //Manually triggered display events 214 215 - if CHANGE_IMAGE.load(core::sync::atomic::Ordering::Relaxed) || first_run { 216 let current_image = get_current_image(); 217 let tga: Bmp<BinaryColor> = Bmp::from_slice(&current_image.image()).unwrap(); 218 let image = Image::new(&tga, current_image.image_location()); ··· 232 CHANGE_IMAGE.store(false, core::sync::atomic::Ordering::Relaxed); 233 } 234 } else { 235 - if cycles_since_last_clear % 60 == 0 || first_run { 236 let top_bounds = Rectangle::new(Point::new(0, 0), Size::new(WIDTH, 24)); 237 top_bounds 238 .into_styled( ··· 268 if cycles_since_last_clear >= cycles_to_clear_at { 269 cycles_since_last_clear = 0; 270 } 271 - first_run = false; 272 // info!("Display Cycle: {}", cycles_since_last_clear); 273 Timer::after(cycle).await; 274 }
··· 37 //Display state 38 pub static SCREEN_TO_SHOW: blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<Screen>> = 39 blocking_mutex::Mutex::new(RefCell::new(Screen::Badge)); 40 + pub static FORCE_SCREEN_REFRESH: AtomicBool = AtomicBool::new(true); 41 pub static DISPLAY_CHANGED: AtomicBool = AtomicBool::new(false); 42 pub static CURRENT_IMAGE: AtomicU8 = AtomicU8::new(0); 43 pub static CHANGE_IMAGE: AtomicBool = AtomicBool::new(true); ··· 92 env_value("DETAILS") 93 )); 94 95 let name_and_detail_box = TextBox::with_textbox_style( 96 &display_text, 97 name_and_detail_bounds, ··· 99 textbox_style, 100 ); 101 102 // let _ = display.update().await; 103 104 //Each cycle is half a second 105 let cycle: Duration = Duration::from_millis(500); 106 107 //New start every 120 cycles or 60 seconds 108 let cycles_to_clear_at: i32 = 120; 109 let mut cycles_since_last_clear = 0; 110 let mut current_screen = Screen::Badge; 111 loop { 112 + let mut force_screen_refresh = 113 + FORCE_SCREEN_REFRESH.load(core::sync::atomic::Ordering::Relaxed); 114 //Timed based display events 115 if DISPLAY_CHANGED.load(core::sync::atomic::Ordering::Relaxed) { 116 let clear_rectangle = Rectangle::new(Point::new(0, 0), Size::new(WIDTH, HEIGHT)); ··· 120 .unwrap(); 121 let _ = display.update().await; 122 DISPLAY_CHANGED.store(false, core::sync::atomic::Ordering::Relaxed); 123 + force_screen_refresh = true; 124 } 125 126 SCREEN_TO_SHOW.lock(|x| current_screen = *x.borrow()); 127 info!("Current Screen: {:?}", current_screen); 128 if current_screen == Screen::Badge { 129 + if force_screen_refresh { 130 + // Draw the text box. 131 + name_and_detail_box.draw(&mut display).unwrap(); 132 + } 133 + 134 //Updates the top bar 135 //Runs every 60 cycles/30 seconds and first run 136 + if cycles_since_last_clear % 60 == 0 || force_screen_refresh { 137 let count = WIFI_COUNT.load(core::sync::atomic::Ordering::Relaxed); 138 let temp = TEMP.load(core::sync::atomic::Ordering::Relaxed); 139 let humidity = HUMIDITY.load(core::sync::atomic::Ordering::Relaxed); ··· 168 } 169 170 //Runs every 120 cycles/60 seconds and first run 171 + if cycles_since_last_clear == 0 || force_screen_refresh { 172 let mut time_text: String<8> = String::<8>::new(); 173 174 let time_box_rectangle_location = Point::new(0, 96); ··· 215 216 //Manually triggered display events 217 218 + if CHANGE_IMAGE.load(core::sync::atomic::Ordering::Relaxed) || force_screen_refresh { 219 let current_image = get_current_image(); 220 let tga: Bmp<BinaryColor> = Bmp::from_slice(&current_image.image()).unwrap(); 221 let image = Image::new(&tga, current_image.image_location()); ··· 235 CHANGE_IMAGE.store(false, core::sync::atomic::Ordering::Relaxed); 236 } 237 } else { 238 + if cycles_since_last_clear % 60 == 0 || force_screen_refresh { 239 let top_bounds = Rectangle::new(Point::new(0, 0), Size::new(WIDTH, 24)); 240 top_bounds 241 .into_styled( ··· 271 if cycles_since_last_clear >= cycles_to_clear_at { 272 cycles_since_last_clear = 0; 273 } 274 + FORCE_SCREEN_REFRESH.store(false, core::sync::atomic::Ordering::Relaxed); 275 // info!("Display Cycle: {}", cycles_since_last_clear); 276 Timer::after(cycle).await; 277 }
+3 -2
src/main.rs
··· 6 #![no_main] 7 use badge_display::display_image::DisplayImage; 8 use badge_display::{ 9 - run_the_display, Screen, CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, RTC_TIME_STRING, 10 - SCREEN_TO_SHOW, WIFI_COUNT, 11 }; 12 use core::fmt::Write; 13 use core::str::from_utf8; ··· 319 save.wifi_counted = 0; 320 save.bssid.clear(); 321 WIFI_COUNT.store(0, core::sync::atomic::Ordering::Relaxed); 322 Timer::after(Duration::from_millis(500)).await; 323 current_cycle += 500; 324 continue;
··· 6 #![no_main] 7 use badge_display::display_image::DisplayImage; 8 use badge_display::{ 9 + run_the_display, Screen, CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, FORCE_SCREEN_REFRESH, 10 + RTC_TIME_STRING, SCREEN_TO_SHOW, WIFI_COUNT, 11 }; 12 use core::fmt::Write; 13 use core::str::from_utf8; ··· 319 save.wifi_counted = 0; 320 save.bssid.clear(); 321 WIFI_COUNT.store(0, core::sync::atomic::Ordering::Relaxed); 322 + FORCE_SCREEN_REFRESH.store(true, core::sync::atomic::Ordering::Relaxed); 323 Timer::after(Duration::from_millis(500)).await; 324 current_cycle += 500; 325 continue;