@@ -27,7 +27,8 @@ use static_cell::StaticCell;
2727
2828const NUM_LEDS : usize = 9 ;
2929const LED_PINS : [ u8 ; NUM_LEDS ] = [ 5 , 6 , 7 , 8 , 9 , 10 , 20 , 21 , 0 ] ;
30- const DEFAULT_MAX_DURATION : Duration = Duration :: from_secs ( 60 * 90 ) ; // 90 minutes
30+ const BUILT_IN_TIMER_DURATION : Duration = Duration :: from_secs ( 60 * 9 ) ; // 90 minutes
31+ const BUILT_IN_TIMER_UPDATE_INTERVAL : Duration = Duration :: from_secs ( 60 ) ;
3132
3233extern crate alloc;
3334
@@ -87,6 +88,8 @@ async fn main(spawner: Spawner) {
8788 esp_hal_embassy:: init ( timer0. alarm0 ) ;
8889 info ! ( "Embassy initialized!" ) ;
8990
91+ let startup_instant = Instant :: now ( ) ;
92+
9093 // WARN: This panic pin needs to match the emergency hardcoded panic pin
9194 let panic_pin = Output :: new ( peripherals. GPIO3 , Level :: High , OutputConfig :: default ( ) ) ;
9295 // Store the pin globally for panic handler access
@@ -108,6 +111,9 @@ async fn main(spawner: Spawner) {
108111 ] ;
109112 let leds = LEDS . init ( Mutex :: new ( LEDs :: new ( led_pins) ) ) ;
110113
114+ // Initialize the LEDs to display the built-in timer
115+ leds. lock ( ) . await . display_builtin_timer ( startup_instant) ;
116+
111117 let meeting_sign_state = MEETING_SIGN_STATE . init ( MeetingSignStatePubSubChannel :: new ( ) ) ;
112118
113119 // We only care about the latest state, so we use immediate publishers
@@ -151,7 +157,11 @@ async fn main(spawner: Spawner) {
151157 Either :: First ( state) => match state {
152158 MeetingSignState :: NoUart => {
153159 info ! ( "State changed to NoUart." ) ;
154- leds. lock ( ) . await . set_ratio_low ( ProgressRatio ( u8:: MAX / 2 ) ) ;
160+
161+ // Change timeout since we have not received UART commands
162+ loop_timeout_duration = BUILT_IN_TIMER_UPDATE_INTERVAL ;
163+
164+ leds. lock ( ) . await . display_builtin_timer ( startup_instant) ;
155165 }
156166 MeetingSignState :: Uart ( instruction) => {
157167 info ! ( "State changed to Uart." ) ;
@@ -162,23 +172,25 @@ async fn main(spawner: Spawner) {
162172 MeetingSignInstruction :: Off => {
163173 leds. lock ( ) . await . set_ratio_low ( ProgressRatio ( 0 ) )
164174 }
165- MeetingSignInstruction :: Diagnostic => todo ! ( ) ,
166175 }
167- leds. lock ( ) . await . set_ratio_low ( ProgressRatio ( u8:: MAX ) ) ;
176+
177+ // Change timeout since we have received UART commands
178+ loop_timeout_duration = UART_COMMUNICATION_TIMEOUT ;
168179 }
169180 } ,
170181 Either :: Second ( _) => {
171182 info ! (
172183 "No state change detected within {}s, displaying LEDs according to builtin timer..." ,
173184 loop_timeout_duration. as_secs( )
174185 ) ;
175- loop_timeout_duration = Duration :: from_secs ( 60 ) ; // Increase the timeout now
176186
177- leds. lock ( ) . await . set_pattern_array ( & [
178- false , true , true , false , false , false , true , true , false ,
179- ] ) ;
180- Timer :: after ( Duration :: from_secs ( 1 ) ) . await ;
181- leds. lock ( ) . await . display_builtin_timer ( ) ;
187+ // Change timeout since we have not received UART commands
188+ loop_timeout_duration = BUILT_IN_TIMER_UPDATE_INTERVAL ;
189+
190+ // leds.lock().await.set_pattern_array(&[
191+ // false, true, true, false, false, false, true, true, false,
192+ // ]);
193+ leds. lock ( ) . await . display_builtin_timer ( startup_instant) ;
182194 }
183195 }
184196 }
@@ -218,17 +230,23 @@ impl<'a> LEDs<'a> {
218230 }
219231 }
220232
221- pub fn display_builtin_timer ( & mut self ) {
222- let on_duration = Instant :: now ( ) . elapsed ( ) ;
223- if on_duration >= DEFAULT_MAX_DURATION {
233+ pub fn display_builtin_timer ( & mut self , startup_instant : Instant ) {
234+ let on_duration = startup_instant . elapsed ( ) ;
235+ if on_duration >= BUILT_IN_TIMER_DURATION {
224236 // If the timer has expired, turn off all LEDs
225237 for led in self . led_outs . iter_mut ( ) {
226238 led. set_low ( ) ;
227239 }
228240 } else {
229241 // Calculate the portion of time elapsed
230- let ratio = ProgressRatio :: from_durations ( & on_duration, & DEFAULT_MAX_DURATION )
242+ debug ! (
243+ "Calculating ratio = {}s / {}s" ,
244+ on_duration. as_secs( ) ,
245+ BUILT_IN_TIMER_DURATION . as_secs( )
246+ ) ;
247+ let ratio = ProgressRatio :: from_durations ( & on_duration, & BUILT_IN_TIMER_DURATION )
231248 . expect ( "Failed to calculate ratio from durations" ) ;
249+ debug ! ( "Setting LEDs based on ratio: {:?}" , ratio) ;
232250 // Set LEDs based on the portion
233251 self . set_ratio_low ( ratio) ;
234252 }
0 commit comments