A personal rust firmware for the Badger 2040 W

Good enough

Changed files
+25 -21
src
badge_display
+5 -4
src/badge_display/mod.rs
··· 34 34 35 35 use crate::{env::env_value, helpers::easy_format, Spi0Bus}; 36 36 37 - pub type RECENT_WIFI_NETWORKS_VEC = Vec<String<32>, 4>; 37 + pub type RecentWifiNetworksVec = Vec<String<32>, 4>; 38 38 39 39 //Display state 40 40 pub static SCREEN_TO_SHOW: blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<Screen>> = 41 41 blocking_mutex::Mutex::new(RefCell::new(Screen::Badge)); 42 42 pub static RECENT_WIFI_NETWORKS: blocking_mutex::Mutex< 43 43 CriticalSectionRawMutex, 44 - RefCell<RECENT_WIFI_NETWORKS_VEC>, 45 - > = blocking_mutex::Mutex::new(RefCell::new(RECENT_WIFI_NETWORKS_VEC::new())); 44 + RefCell<RecentWifiNetworksVec>, 45 + > = blocking_mutex::Mutex::new(RefCell::new(RecentWifiNetworksVec::new())); 46 46 47 47 pub static FORCE_SCREEN_REFRESH: AtomicBool = AtomicBool::new(true); 48 48 pub static DISPLAY_CHANGED: AtomicBool = AtomicBool::new(false); ··· 131 131 } 132 132 133 133 SCREEN_TO_SHOW.lock(|x| current_screen = *x.borrow()); 134 - info!("Current Screen: {:?}", current_screen); 134 + // info!("Current Screen: {:?}", current_screen); 135 135 if current_screen == Screen::Badge { 136 136 if force_screen_refresh { 137 137 // Draw the text box. ··· 142 142 //Runs every 60 cycles/30 seconds and first run 143 143 if cycles_since_last_clear % 60 == 0 || force_screen_refresh { 144 144 let count = WIFI_COUNT.load(core::sync::atomic::Ordering::Relaxed); 145 + info!("Wifi count: {}", count); 145 146 let temp = TEMP.load(core::sync::atomic::Ordering::Relaxed); 146 147 let humidity = HUMIDITY.load(core::sync::atomic::Ordering::Relaxed); 147 148 let top_text: String<64> = easy_format::<64>(format_args!(
+20 -17
src/main.rs
··· 6 6 #![no_main] 7 7 use badge_display::display_image::DisplayImage; 8 8 use badge_display::{ 9 - run_the_display, Screen, CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, FORCE_SCREEN_REFRESH, 10 - RECENT_WIFI_NETWORKS, RECENT_WIFI_NETWORKS_VEC, RTC_TIME_STRING, SCREEN_TO_SHOW, WIFI_COUNT, 9 + run_the_display, RecentWifiNetworksVec, Screen, CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, 10 + FORCE_SCREEN_REFRESH, RECENT_WIFI_NETWORKS, RTC_TIME_STRING, SCREEN_TO_SHOW, WIFI_COUNT, 11 11 }; 12 12 use core::fmt::Write; 13 13 use core::str::from_utf8; ··· 266 266 //Input loop 267 267 let cycle = Duration::from_millis(100); 268 268 let mut current_cycle = 0; 269 - //5 minutes 270 - let reset_cycle = 300_000; 269 + let mut time_to_scan = true; 270 + //5 minutes(ish) idk it's late and my math is so bad rn 271 + let reset_cycle = 3_000; 271 272 //Turn off led to signify that the badge is ready 272 273 user_led.set_low(); 273 274 ··· 280 281 CURRENT_IMAGE.store(new_image.as_u8(), core::sync::atomic::Ordering::Relaxed); 281 282 CHANGE_IMAGE.store(true, core::sync::atomic::Ordering::Relaxed); 282 283 Timer::after(Duration::from_millis(500)).await; 283 - current_cycle += 500; 284 284 continue; 285 285 } 286 286 287 287 if btn_a.is_high() { 288 + println!("{:?}", current_cycle); 288 289 info!("Button A pressed"); 289 290 user_led.toggle(); 290 291 Timer::after(Duration::from_millis(500)).await; 291 - current_cycle += 500; 292 292 continue; 293 293 } 294 294 ··· 299 299 }); 300 300 DISPLAY_CHANGED.store(true, core::sync::atomic::Ordering::Relaxed); 301 301 Timer::after(Duration::from_millis(500)).await; 302 - current_cycle += 500; 303 302 continue; 304 303 } 305 304 ··· 310 309 }); 311 310 DISPLAY_CHANGED.store(true, core::sync::atomic::Ordering::Relaxed); 312 311 Timer::after(Duration::from_millis(500)).await; 313 - current_cycle += 500; 314 312 continue; 315 313 } 316 314 317 315 if btn_b.is_high() { 318 316 info!("Button B pressed"); 319 - save.wifi_counted = 0; 320 - save.bssid.clear(); 321 317 322 318 SCREEN_TO_SHOW.lock(|screen| { 323 319 if *screen.borrow() == Screen::Badge { 320 + //IF on badge screen and b pressed reset wifi count 321 + save.wifi_counted = 0; 322 + save.bssid.clear(); 324 323 WIFI_COUNT.store(0, core::sync::atomic::Ordering::Relaxed); 324 + current_cycle = 0; 325 325 } 326 326 }); 327 327 328 - let mut recent_networks = RECENT_WIFI_NETWORKS_VEC::new(); 328 + let mut recent_networks = RecentWifiNetworksVec::new(); 329 329 let mut scanner = control.scan(Default::default()).await; 330 330 while let Some(bss) = scanner.next().await { 331 331 process_bssid(bss.bssid, &mut save.wifi_counted, &mut save.bssid); ··· 333 333 let possible_ssid = core::str::from_utf8(&bss.ssid); 334 334 match possible_ssid { 335 335 Ok(ssid) => { 336 + info!("ssid: {}", ssid); 336 337 let ssid_string = easy_format::<32>(format_args!("{}", ssid.trim())); 337 338 if recent_networks.contains(&ssid_string) { 338 339 continue; ··· 351 352 352 353 FORCE_SCREEN_REFRESH.store(true, core::sync::atomic::Ordering::Relaxed); 353 354 Timer::after(Duration::from_millis(500)).await; 354 - current_cycle += 500; 355 + 355 356 continue; 356 357 } 357 358 ··· 369 370 rtc_time_string.borrow_mut().push_str("No Wifi").unwrap(); 370 371 }); 371 372 } 372 - if current_cycle == 0 { 373 + if time_to_scan { 374 + info!("Scanning for wifi networks"); 375 + time_to_scan = false; 373 376 let mut scanner = control.scan(Default::default()).await; 374 377 while let Some(bss) = scanner.next().await { 375 378 process_bssid(bss.bssid, &mut save.wifi_counted, &mut save.bssid); 376 - let ssid = core::str::from_utf8(&bss.ssid).unwrap(); 377 - info!("ssid: {}", ssid); 378 379 } 380 + WIFI_COUNT.store(save.wifi_counted, core::sync::atomic::Ordering::Relaxed); 379 381 save_postcard_to_flash(ADDR_OFFSET, &mut flash, SAVE_OFFSET, &save).unwrap(); 380 - WIFI_COUNT.store(save.wifi_counted, core::sync::atomic::Ordering::Relaxed); 381 382 info!("wifi_counted: {}", save.wifi_counted); 382 383 } 383 384 if current_cycle >= reset_cycle { 384 385 current_cycle = 0; 386 + time_to_scan = true; 385 387 } 386 388 current_cycle += 1; 387 389 Timer::after(cycle).await; ··· 430 432 let bssid_str = format_bssid(bssid); 431 433 if !bssids.contains(&bssid_str) { 432 434 *wifi_counted += 1; 433 - info!("bssid: {:x}", bssid_str); 435 + WIFI_COUNT.store(*wifi_counted, core::sync::atomic::Ordering::Relaxed); 436 + // info!("bssid: {:x}", bssid_str); 434 437 let result = bssids.push(bssid_str); 435 438 if result.is_err() { 436 439 info!("bssid list full");