Skip to content

Commit 9b398e0

Browse files
author
Bob Abeles
committed
second round code review changes
1 parent 5bf6505 commit 9b398e0

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

adafruit_hid/__init__.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
# imports
2121
from __future__ import annotations
2222
import time
23-
import supervisor
23+
24+
try:
25+
import supervisor
26+
except ImportError:
27+
supervisor = None
2428

2529
try:
2630
from typing import Sequence
@@ -33,10 +37,18 @@
3337

3438

3539
def find_device(
36-
devices: Sequence[usb_hid.Device], *, usage_page: int, usage: int, timeout: int
40+
devices: Sequence[usb_hid.Device],
41+
*,
42+
usage_page: int,
43+
usage: int,
44+
timeout: int = None,
3745
) -> usb_hid.Device:
3846
"""Search through the provided sequence of devices to find the one with the matching
39-
usage_page and usage. Wait up to timeout seconds for USB to become ready."""
47+
usage_page and usage.
48+
49+
:param timeout: Time in seconds to wait for USB to become ready before timing out.
50+
Defaults to None to wait indefinitely."""
51+
4052
if hasattr(devices, "send_report"):
4153
devices = [devices] # type: ignore
4254
device = None
@@ -51,8 +63,20 @@ def find_device(
5163
if device is None:
5264
raise ValueError("Could not find matching HID device.")
5365

54-
for _ in range(timeout):
55-
if supervisor.runtime.usb_connected:
56-
return device
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
5769
time.sleep(1.0)
58-
raise OSError("Failed to initialize HID device. Is USB connected?")
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
79+
time.sleep(1.0)
80+
raise OSError("Failed to initialize HID device. Is USB connected?")
81+
82+
return device

adafruit_hid/consumer_control.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
class ConsumerControl:
3131
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc."""
3232

33-
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
33+
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> None:
3434
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
3535
3636
:param timeout: Time in seconds to wait for USB to become ready before timing out.
37+
Defaults to None to wait indefinitely.
3738
3839
Devices can be a sequence of devices that includes a Consumer Control device or a CC device
3940
itself. A device is any object that implements ``send_report()``, ``usage_page`` and

adafruit_hid/keyboard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ class Keyboard:
3838

3939
# No more than _MAX_KEYPRESSES regular keys may be pressed at once.
4040

41-
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
41+
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> None:
4242
"""Create a Keyboard object that will send keyboard HID reports.
4343
4444
:param timeout: Time in seconds to wait for USB to become ready before timing out.
45+
Defaults to None to wait indefinitely.
4546
4647
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
4748
itself. A device is any object that implements ``send_report()``, ``usage_page`` and

adafruit_hid/mouse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ class Mouse:
2727
MIDDLE_BUTTON = 4
2828
"""Middle mouse button."""
2929

30-
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
30+
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> None:
3131
"""Create a Mouse object that will send USB mouse HID reports.
3232
3333
:param timeout: Time in seconds to wait for USB to become ready before timing out.
34+
Defaults to None to wait indefinitely.
3435
3536
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
3637
itself. A device is any object that implements ``send_report()``, ``usage_page`` and

0 commit comments

Comments
 (0)