Skip to content

Commit 0492467

Browse files
committed
ledmatrix: Change sleep only if state changed or should sleep
This means the software sleep control will work again. Hardware sleep control won't override it. See README for details. Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 76822e9 commit 0492467

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

ledmatrix/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,29 @@ run the stop command.
184184
```sh
185185
inputmodule-control led-amtrix --stop-game
186186
```
187+
188+
## Sleep Behavior
189+
190+
Currently sleeping means all LEDs and the LED controller are turned off.
191+
Transitions of sleep state slowly fade the LEDs on or off.
192+
193+
Optionally the firmware can be configured, at build-time, to turn the LEDs
194+
on/off immediately. Or display "SLEEP" instead of turning the LEDs off, which
195+
is useful for debugging whether the device is sleeping or not powered.
196+
197+
198+
###### Changing Sleep State
199+
200+
What can change the sleep state
201+
202+
- Hardware triggers
203+
- `SLEEP#` pin
204+
- USB Suspend
205+
- Software Triggers
206+
- Sleep/Wake Command via USB Serial
207+
208+
Both of the hardware triggers change the sleep state if the transition from one state to another.
209+
For example, if USB suspends, the LED matrix turns off. If it resumes, the LEDs come back on.
210+
Same for the `SLEEP#` pin.
211+
212+
The sleep/wake command always changes the state. But it can't be received when USB is suspended.

ledmatrix/src/main.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,45 @@ fn main() -> ! {
254254
}
255255

256256
let mut usb_initialized = false;
257-
let mut usb_suspended = true;
257+
let mut usb_suspended = false;
258+
let mut last_usb_suspended = usb_suspended;
259+
let mut sleeping = false;
260+
let mut last_host_sleep = sleep.is_low().unwrap();
258261

259262
loop {
260263
if sleep_present {
261264
// Go to sleep if the host is sleeping
262-
// Or if USB is suspended. Only if it was previously initialized,
263-
// since the OS puts the device into suspend before it's fully
264-
// initialized for the first time. But we don't want to show the
265-
// sleep animation during startup.
266-
let host_sleeping = sleep.is_low().unwrap() || (usb_suspended && usb_initialized);
267-
handle_sleep(
268-
host_sleeping,
269-
&mut state,
270-
&mut matrix,
271-
&mut delay,
272-
&mut led_enable,
273-
);
265+
let host_sleeping = sleep.is_low().unwrap();
266+
let host_sleep_changed = host_sleeping != last_host_sleep;
267+
// Change sleep state either if SLEEP# has changed
268+
// Or if it currently sleeping. Don't change if not sleeping
269+
// because then sleep is controlled by timing or by API.
270+
if host_sleep_changed || host_sleeping {
271+
sleeping = host_sleeping;
272+
}
273+
last_host_sleep = host_sleeping;
274+
}
275+
276+
// Change sleep state either if SLEEP# has changed
277+
// Or if it currently sleeping. Don't change if not sleeping
278+
// because then sleep is controlled by timing or by API.
279+
let usb_suspended_changed = usb_suspended != last_usb_suspended;
280+
// Only if USB was previously initialized,
281+
// since the OS puts the device into suspend before it's fully
282+
// initialized for the first time. But we don't want to show the
283+
// sleep animation during startup.
284+
if usb_initialized && (usb_suspended_changed || usb_suspended) {
285+
sleeping = usb_suspended;
274286
}
287+
last_usb_suspended = usb_suspended;
288+
289+
handle_sleep(
290+
sleeping,
291+
&mut state,
292+
&mut matrix,
293+
&mut delay,
294+
&mut led_enable,
295+
);
275296

276297
// Handle period display updates. Don't do it too often
277298
let render_again = timer.get_counter().ticks() > prev_timer + state.animation_period;
@@ -326,6 +347,7 @@ fn main() -> ! {
326347
let random = get_random_byte(&rosc);
327348
match (parse_command(count, &buf), &state.sleeping) {
328349
(Some(Command::Sleep(go_sleeping)), _) => {
350+
sleeping = go_sleeping;
329351
handle_sleep(
330352
go_sleeping,
331353
&mut state,

0 commit comments

Comments
 (0)