@@ -33,6 +33,10 @@ const SLEEP_MODE: SleepMode = SleepMode::Fading;
3333
3434const STARTUP_ANIMATION : bool = true ;
3535
36+ /// Go to sleep after 60s awake
37+ const SLEEP_TIMEOUT : u64 = 60_000_000 ;
38+
39+ /// List maximum current as 500mA in the USB descriptor
3640const MAX_CURRENT : usize = 500 ;
3741
3842/// Maximum brightness out of 255
@@ -232,8 +236,9 @@ fn main() -> ! {
232236 fill_grid_pixels ( & state, & mut matrix) ;
233237
234238 let timer = Timer :: new ( pac. TIMER , & mut pac. RESETS ) ;
235- let mut prev_timer = timer. get_counter ( ) . ticks ( ) ;
239+ let mut animation_timer = timer. get_counter ( ) . ticks ( ) ;
236240 let mut game_timer = timer. get_counter ( ) . ticks ( ) ;
241+ let mut sleep_timer = timer. get_counter ( ) . ticks ( ) ;
237242
238243 let mut startup_percentage = Some ( 0 ) ;
239244 if !STARTUP_ANIMATION {
@@ -286,6 +291,16 @@ fn main() -> ! {
286291 }
287292 last_usb_suspended = usb_suspended;
288293
294+ // Go to sleep after the timer has run out
295+ if timer. get_counter ( ) . ticks ( ) > sleep_timer + SLEEP_TIMEOUT {
296+ sleeping = true ;
297+ }
298+ // Constantly resetting timer during sleep is same as reset it once on waking up.
299+ // This means the timer ends up counting the time spent awake.
300+ if sleeping {
301+ sleep_timer = timer. get_counter ( ) . ticks ( ) ;
302+ }
303+
289304 handle_sleep (
290305 sleeping,
291306 & mut state,
@@ -295,7 +310,7 @@ fn main() -> ! {
295310 ) ;
296311
297312 // Handle period display updates. Don't do it too often
298- let render_again = timer. get_counter ( ) . ticks ( ) > prev_timer + state. animation_period ;
313+ let render_again = timer. get_counter ( ) . ticks ( ) > animation_timer + state. animation_period ;
299314 if matches ! ( state. sleeping, SleepState :: Awake ) && render_again {
300315 // On startup slowly turn the screen on - it's a pretty effect :)
301316 match startup_percentage {
@@ -312,7 +327,7 @@ fn main() -> ! {
312327 state. grid . 0 [ x] . rotate_right ( 1 ) ;
313328 }
314329 }
315- prev_timer = timer. get_counter ( ) . ticks ( ) ;
330+ animation_timer = timer. get_counter ( ) . ticks ( ) ;
316331 }
317332
318333 // Check for new data
0 commit comments