Skip to content

Commit 573d988

Browse files
author
Bob Abeles
committed
Increase host timeout from 1 second to 20.
1 parent 30097f7 commit 573d988

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

adafruit_hid/consumer_control.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
4444
self._report = bytearray(2)
4545

4646
# Do a no-op to test if HID device is ready.
47-
# If not, wait a bit and try once more.
48-
try:
49-
self.send(0x0)
50-
except OSError:
51-
time.sleep(1)
52-
self.send(0x0)
47+
# Some hosts take awhile to enumerate after POR, so retry a few times.
48+
for _ in range(20):
49+
try:
50+
self.send(0x0)
51+
return
52+
except OSError:
53+
time.sleep(1.0)
54+
raise OSError("HID device init timeout.")
5355

5456
def send(self, consumer_code: int) -> None:
5557
"""Send a report to do the specified consumer control action,

adafruit_hid/keyboard.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
6666
self._led_status = b"\x00"
6767

6868
# Do a no-op to test if HID device is ready.
69-
# If not, wait a bit and try once more.
70-
try:
71-
self.release_all()
72-
except OSError:
73-
time.sleep(1)
74-
self.release_all()
75-
69+
# Some hosts take awhile to enumerate after POR, so retry a few times.
70+
for _ in range(20):
71+
try:
72+
self.release_all()
73+
return
74+
except OSError:
75+
time.sleep(1.0)
76+
raise OSError("HID device init timeout.")
77+
7678
def press(self, *keycodes: int) -> None:
7779
"""Send a report indicating that the given keys have been pressed.
7880

adafruit_hid/mouse.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ def __init__(self, devices: Sequence[usb_hid.Device]):
4646
self.report = bytearray(4)
4747

4848
# Do a no-op to test if HID device is ready.
49-
# If not, wait a bit and try once more.
50-
try:
51-
self._send_no_move()
52-
except OSError:
53-
time.sleep(1)
54-
self._send_no_move()
49+
# Some hosts take awhile to enumerate after POR, so retry a few times.
50+
for _ in range(20):
51+
try:
52+
self._send_no_move()
53+
return
54+
except OSError:
55+
time.sleep(1.0)
56+
raise OSError("HID device init timeout.")
5557

5658
def press(self, buttons: int) -> None:
5759
"""Press the given mouse buttons.

0 commit comments

Comments
 (0)