Skip to content

Commit f21d2ad

Browse files
committed
ledmatrix: Sleep after 60s timeout
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 0492467 commit f21d2ad

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

ledmatrix/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,12 @@ What can change the sleep state
204204
- USB Suspend
205205
- Software Triggers
206206
- Sleep/Wake Command via USB Serial
207+
- Idle timer
207208

208209
Both of the hardware triggers change the sleep state if the transition from one state to another.
209210
For example, if USB suspends, the LED matrix turns off. If it resumes, the LEDs come back on.
210211
Same for the `SLEEP#` pin.
211212

212213
The sleep/wake command always changes the state. But it can't be received when USB is suspended.
214+
215+
The idle timer will send the device to sleep after a configured timeout.

ledmatrix/src/main.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const SLEEP_MODE: SleepMode = SleepMode::Fading;
3333

3434
const 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
3640
const 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

Comments
 (0)