Skip to content

Commit 6a91065

Browse files
committed
ledmatrix: Make sure sleep interacts nicely w/ other cmds
Waking up should resume to the newly set brightness. And it should also immediately show the newly set grid, not just ignore it. Waking up command should also reset the sleep timer. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 73dd897 commit 6a91065

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

ledmatrix/src/main.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,32 +361,42 @@ fn main() -> ! {
361361
Ok(count) => {
362362
let random = get_random_byte(&rosc);
363363
match (parse_command(count, &buf), &state.sleeping) {
364-
// While sleeping no command is handled, except waking up
365-
(Some(Command::Sleep(go_sleeping)), _) => {
366-
sleeping = go_sleeping;
364+
// Handle bootloader command without any delay
365+
// No need, it'll reset the device anyways
366+
(Some(c @ Command::BootloaderReset), _) => {
367+
handle_command(&c, &mut state, &mut matrix, random);
368+
}
369+
(Some(command), _) => {
370+
if let Command::Sleep(go_sleeping) = command {
371+
sleeping = go_sleeping;
372+
} else {
373+
// If already sleeping, wake up.
374+
// This means every command will wake the device up.
375+
// Much more convenient than having to send the wakeup commmand.
376+
sleeping = false;
377+
}
378+
// Make sure sleep animation only goes up to newly set brightness,
379+
// if setting the brightness causes wakeup
380+
if let SleepState::Sleeping((ref grid, _)) = state.sleeping {
381+
if let Command::SetBrightness(new_brightness) = command {
382+
state.sleeping =
383+
SleepState::Sleeping((grid.clone(), new_brightness));
384+
}
385+
}
367386
handle_sleep(
368-
go_sleeping,
387+
sleeping,
369388
&mut state,
370389
&mut matrix,
371390
&mut delay,
372391
&mut led_enable,
373392
);
374-
}
375-
(Some(command), _) => {
393+
376394
// If there's a very early command, cancel the startup animation
377395
startup_percentage = None;
378396

379397
// Reset sleep timer when interacting with the device
398+
// Very easy way to keep the device from going to sleep
380399
sleep_timer = timer.get_counter().ticks();
381-
// If already sleeping, wake up
382-
sleeping = false;
383-
handle_sleep(
384-
sleeping,
385-
&mut state,
386-
&mut matrix,
387-
&mut delay,
388-
&mut led_enable,
389-
);
390400

391401
if let Some(response) =
392402
handle_command(&command, &mut state, &mut matrix, random)
@@ -403,9 +413,10 @@ fn main() -> ! {
403413
)
404414
.unwrap();
405415
// let _ = serial.write(text.as_bytes());
416+
406417
fill_grid_pixels(&state, &mut matrix);
407418
}
408-
_ => {}
419+
(None, _) => {}
409420
}
410421
}
411422
}
@@ -477,6 +488,7 @@ fn get_random_byte(rosc: &RingOscillator<Enabled>) -> u8 {
477488
byte
478489
}
479490

491+
// Will do nothing if already in the right state
480492
fn handle_sleep(
481493
go_sleeping: bool,
482494
state: &mut LedmatrixState,

0 commit comments

Comments
 (0)