@@ -253,10 +253,17 @@ fn main() -> ! {
253253 sleep_present = true ;
254254 }
255255
256+ let mut usb_initialized = false ;
257+ let mut usb_suspended = true ;
258+
256259 loop {
257260 if sleep_present {
258261 // Go to sleep if the host is sleeping
259- let host_sleeping = sleep. is_low ( ) . unwrap ( ) ;
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) ;
260267 handle_sleep (
261268 host_sleeping,
262269 & mut state,
@@ -289,6 +296,24 @@ fn main() -> ! {
289296
290297 // Check for new data
291298 if usb_dev. poll ( & mut [ & mut serial] ) {
299+ match usb_dev. state ( ) {
300+ // Default: Device has just been created or reset
301+ // Addressed: Device has received an address for the host
302+ UsbDeviceState :: Default | UsbDeviceState :: Addressed => {
303+ usb_initialized = false ;
304+ usb_suspended = false ;
305+ // Must not display anything or windows cannot enumerate properly
306+ }
307+ // Configured and is fully operational
308+ UsbDeviceState :: Configured => {
309+ usb_initialized = true ;
310+ usb_suspended = false ;
311+ }
312+ // Never occurs here. Only if poll() returns false
313+ UsbDeviceState :: Suspend => {
314+ panic ! ( "Never occurs here. Only if poll() returns false" )
315+ }
316+ }
292317 let mut buf = [ 0u8 ; 64 ] ;
293318 match serial. read ( & mut buf) {
294319 Err ( _e) => {
@@ -342,6 +367,21 @@ fn main() -> ! {
342367 }
343368 }
344369 }
370+ } else {
371+ match usb_dev. state ( ) {
372+ // No new data
373+ UsbDeviceState :: Default | UsbDeviceState :: Addressed => {
374+ usb_initialized = false ;
375+ usb_suspended = false ;
376+ }
377+ UsbDeviceState :: Configured => {
378+ usb_initialized = true ;
379+ usb_suspended = false ;
380+ }
381+ UsbDeviceState :: Suspend => {
382+ usb_suspended = true ;
383+ }
384+ }
345385 }
346386
347387 // Handle game state
0 commit comments