28
28
29
29
try :
30
30
from typing import Sequence
31
- import usb_hid
32
31
except ImportError :
33
32
pass
34
33
34
+ # usb_hid may not exist on some boards that still provide BLE or other HID devices.
35
+ try :
36
+ from usb_hid import Device
37
+ except ImportError :
38
+ Device = None
39
+
35
40
__version__ = "0.0.0+auto.0"
36
41
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HID.git"
37
42
38
43
39
44
def find_device (
40
- devices : Sequence [usb_hid . Device ],
45
+ devices : Sequence [object ],
41
46
* ,
42
47
usage_page : int ,
43
48
usage : int ,
44
49
timeout : int = None ,
45
- ) -> usb_hid . Device :
50
+ ) -> object :
46
51
"""Search through the provided sequence of devices to find the one with the matching
47
52
usage_page and usage.
48
53
49
54
:param timeout: Time in seconds to wait for USB to become ready before timing out.
50
- Defaults to None to wait indefinitely."""
55
+ Defaults to None to wait indefinitely.
56
+ Ignored if device is not a `usb_hid.Device`; it might be BLE, for instance."""
51
57
52
58
if hasattr (devices , "send_report" ):
53
59
devices = [devices ] # type: ignore
@@ -63,20 +69,22 @@ def find_device(
63
69
if device is None :
64
70
raise ValueError ("Could not find matching HID device." )
65
71
66
- if supervisor is None :
67
- # Blinka doesn't have supervisor (see issue Adafruit_Blinka#711), so wait
68
- # one second for USB to become ready
69
- time .sleep (1.0 )
70
- elif timeout is None :
71
- # default behavior: wait indefinitely for USB to become ready
72
- while not supervisor .runtime .usb_connected :
73
- time .sleep (1.0 )
74
- else :
75
- # wait up to timeout seconds for USB to become ready
76
- for _ in range (timeout ):
77
- if supervisor .runtime .usb_connected :
78
- return device
72
+ # Wait for USB to be connected only if this is a usb_hid.Device.
73
+ if Device and isinstance (device , Device ):
74
+ if supervisor is None :
75
+ # Blinka doesn't have supervisor (see issue Adafruit_Blinka#711), so wait
76
+ # one second for USB to become ready
79
77
time .sleep (1.0 )
80
- raise OSError ("Failed to initialize HID device. Is USB connected?" )
78
+ elif timeout is None :
79
+ # default behavior: wait indefinitely for USB to become ready
80
+ while not supervisor .runtime .usb_connected :
81
+ time .sleep (1.0 )
82
+ else :
83
+ # wait up to timeout seconds for USB to become ready
84
+ for _ in range (timeout ):
85
+ if supervisor .runtime .usb_connected :
86
+ return device
87
+ time .sleep (1.0 )
88
+ raise OSError ("Failed to initialize HID device. Is USB connected?" )
81
89
82
90
return device
0 commit comments