@@ -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