A personal rust firmware for the Badger 2040 W

big wip but working

+73 -2
+73 -2
src/badge_display/mod.rs
··· 34 34 pub static CURRENT_IMAGE: AtomicU8 = AtomicU8::new(0); 35 35 pub static CHANGE_IMAGE: AtomicBool = AtomicBool::new(true); 36 36 pub static WIFI_COUNT: AtomicU32 = AtomicU32::new(0); 37 + pub static HOUR: AtomicU8 = AtomicU8::new(10); 38 + pub static MINUTE: AtomicU8 = AtomicU8::new(57); 39 + pub static SECOND: AtomicU8 = AtomicU8::new(0); 40 + pub static WRITE_NEW_TIME: AtomicBool = AtomicBool::new(true); 37 41 38 42 #[embassy_executor::task] 39 43 pub async fn run_the_display( ··· 85 89 let cycles_to_skip = 30; 86 90 let mut cycles_since_last_clear = 0; 87 91 92 + let mut time_text: String<16> = String::<16>::new(); 93 + 88 94 loop { 89 - //TODO do the clock here need to 90 - //1. Find the right time for counting 95 + //if cycles are even, update the time values 96 + if cycles_since_last_clear % 2 == 0 { 97 + update_time_values_from_cycles(); 98 + } 99 + 100 + if WRITE_NEW_TIME.load(core::sync::atomic::Ordering::Relaxed) { 101 + let hour = HOUR.load(core::sync::atomic::Ordering::Relaxed); 102 + let minute = MINUTE.load(core::sync::atomic::Ordering::Relaxed); 103 + let second = SECOND.load(core::sync::atomic::Ordering::Relaxed); 104 + 105 + let _ = core::fmt::write( 106 + &mut time_text, 107 + format_args!("{:02}:{:02}:{:02}", hour, minute, second), 108 + ); 109 + 110 + let time_bounds = Rectangle::new(Point::new(0, 24), Size::new(WIDTH, 16)); 111 + time_bounds 112 + .into_styled( 113 + PrimitiveStyleBuilder::default() 114 + .stroke_color(BinaryColor::Off) 115 + .fill_color(BinaryColor::On) 116 + .stroke_width(1) 117 + .build(), 118 + ) 119 + .draw(&mut display) 120 + .unwrap(); 121 + 122 + Text::new(time_text.as_str(), Point::new(8, 32), character_style) 123 + .draw(&mut display) 124 + .unwrap(); 125 + 126 + let result = display 127 + .partial_update(time_bounds.try_into().unwrap()) 128 + .await; 129 + match result { 130 + Ok(_) => {} 131 + Err(_) => { 132 + info!("Error updating display"); 133 + } 134 + } 135 + WRITE_NEW_TIME.store(false, core::sync::atomic::Ordering::Relaxed); 136 + } 137 + 91 138 if cycles_since_last_clear >= cycles_to_skip || first_run { 92 139 let count = WIFI_COUNT.load(core::sync::atomic::Ordering::Relaxed); 93 140 let _ = core::fmt::write(&mut text, format_args!("Count: {}", count)); ··· 152 199 Timer::after(cycle).await; 153 200 } 154 201 } 202 + 203 + fn update_time_values_from_cycles() { 204 + let current_second = SECOND.load(core::sync::atomic::Ordering::Relaxed); 205 + let current_minute = MINUTE.load(core::sync::atomic::Ordering::Relaxed); 206 + let current_hour = HOUR.load(core::sync::atomic::Ordering::Relaxed); 207 + 208 + let new_second = (current_second + 1) % 60; 209 + let new_minute = if new_second == 0 { 210 + WRITE_NEW_TIME.store(true, core::sync::atomic::Ordering::Relaxed); 211 + (current_minute + 1) % 60 212 + } else { 213 + current_minute 214 + }; 215 + let new_hour = if new_minute == 0 && new_second == 0 { 216 + WRITE_NEW_TIME.store(true, core::sync::atomic::Ordering::Relaxed); 217 + (current_hour + 1) % 24 218 + } else { 219 + current_hour 220 + }; 221 + 222 + SECOND.store(new_second, core::sync::atomic::Ordering::Relaxed); 223 + MINUTE.store(new_minute, core::sync::atomic::Ordering::Relaxed); 224 + HOUR.store(new_hour, core::sync::atomic::Ordering::Relaxed); 225 + }