@@ -122,12 +122,12 @@ def update_ui():
122122# wait a second for USB devices to be ready
123123time .sleep (1 )
124124
125- # good_devices = False
126- # while not good_devices:
127- # for device in usb.core.find(find_all=True):
128- # if device.manufacturer is not None:
129- # good_devices = True
130- # break
125+ good_devices = False
126+ while not good_devices :
127+ for device in usb .core .find (find_all = True ):
128+ if device .manufacturer is not None :
129+ good_devices = True
130+ break
131131# scan for connected USB devices
132132for device in usb .core .find (find_all = True ):
133133 # print information about the found devices
@@ -155,11 +155,6 @@ def update_ui():
155155 # Try to read some data with a short timeout
156156 data = mouse .read (mouse_endpt , buf , timeout = 100 )
157157 print (f"Mouse test read successful: { data } bytes - { buf } " )
158-
159- # without this subsequent reads sometimes can't find the endpoint?
160- # I've never seen the Flush mouse queue print so it must just need the extra read
161- if mouse .read (mouse_endpt , buf , timeout = 10 ) > 0 :
162- print (f"Flush mouse queue: { buf } " )
163158 break
164159 except usb .core .USBTimeoutError :
165160 # Timeout is normal if mouse isn't moving
@@ -291,7 +286,19 @@ def hide_group(group):
291286 try :
292287 # try to read data from the mouse, small timeout so the code will move on
293288 # quickly if there is no data
294- data_len = mouse .read (mouse_endpt , buf , timeout = 10 )
289+ while True :
290+ try :
291+ # read data from the mouse endpoint
292+ data_len = mouse .read (mouse_endpt , buf , timeout = 10 )
293+ if data_len > 0 :
294+ break
295+ except usb .core .USBTimeoutError :
296+ # if we get a timeout error, it means there is no data available
297+ pass
298+ except usb .core .USBError as exc :
299+ # if we get a USBError, it may mean the mouse is not ready yet
300+ pass
301+
295302 left_button = buf [0 ] & 0x01
296303 right_button = buf [0 ] & 0x02
297304
0 commit comments