Skip to content

Commit 5bf6505

Browse files
author
Bob Abeles
committed
Review feedback
1 parent 31e52ee commit 5bf6505

File tree

4 files changed

+36
-47
lines changed

4 files changed

+36
-47
lines changed

adafruit_hid/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
# imports
2121
from __future__ import annotations
22+
import time
23+
import supervisor
2224

2325
try:
2426
from typing import Sequence
@@ -31,17 +33,26 @@
3133

3234

3335
def find_device(
34-
devices: Sequence[usb_hid.Device], *, usage_page: int, usage: int
36+
devices: Sequence[usb_hid.Device], *, usage_page: int, usage: int, timeout: int
3537
) -> usb_hid.Device:
3638
"""Search through the provided sequence of devices to find the one with the matching
37-
usage_page and usage."""
39+
usage_page and usage. Wait up to timeout seconds for USB to become ready."""
3840
if hasattr(devices, "send_report"):
3941
devices = [devices] # type: ignore
40-
for device in devices:
42+
device = None
43+
for dev in devices:
4144
if (
42-
device.usage_page == usage_page
43-
and device.usage == usage
44-
and hasattr(device, "send_report")
45+
dev.usage_page == usage_page
46+
and dev.usage == usage
47+
and hasattr(dev, "send_report")
4548
):
49+
device = dev
50+
break
51+
if device is None:
52+
raise ValueError("Could not find matching HID device.")
53+
54+
for _ in range(timeout):
55+
if supervisor.runtime.usb_connected:
4656
return device
47-
raise ValueError("Could not find matching HID device.")
57+
time.sleep(1.0)
58+
raise OSError("Failed to initialize HID device. Is USB connected?")

adafruit_hid/consumer_control.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
# pylint: disable=wrong-import-position
2020
import struct
21-
import time
2221
from . import find_device
2322

2423
try:
@@ -31,28 +30,22 @@
3130
class ConsumerControl:
3231
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc."""
3332

34-
def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
33+
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
3534
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
3635
36+
:param timeout: Time in seconds to wait for USB to become ready before timing out.
37+
3738
Devices can be a sequence of devices that includes a Consumer Control device or a CC device
3839
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
3940
``usage``.
4041
"""
41-
self._consumer_device = find_device(devices, usage_page=0x0C, usage=0x01)
42+
self._consumer_device = find_device(
43+
devices, usage_page=0x0C, usage=0x01, timeout=timeout
44+
)
4245

4346
# Reuse this bytearray to send consumer reports.
4447
self._report = bytearray(2)
4548

46-
# Do a no-op to test if HID device is ready.
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.")
55-
5649
def send(self, consumer_code: int) -> None:
5750
"""Send a report to do the specified consumer control action,
5851
and then stop the action (so it will not repeat).

adafruit_hid/keyboard.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* Author(s): Scott Shawcroft, Dan Halbert
1010
"""
1111

12-
import time
1312
from micropython import const
1413
import usb_hid
1514

@@ -39,14 +38,18 @@ class Keyboard:
3938

4039
# No more than _MAX_KEYPRESSES regular keys may be pressed at once.
4140

42-
def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
41+
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
4342
"""Create a Keyboard object that will send keyboard HID reports.
4443
44+
:param timeout: Time in seconds to wait for USB to become ready before timing out.
45+
4546
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
4647
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
4748
``usage``.
4849
"""
49-
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06)
50+
self._keyboard_device = find_device(
51+
devices, usage_page=0x1, usage=0x06, timeout=timeout
52+
)
5053

5154
# Reuse this bytearray to send keyboard reports.
5255
self.report = bytearray(8)
@@ -65,16 +68,6 @@ def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
6568
# No keyboard LEDs on.
6669
self._led_status = b"\x00"
6770

68-
# Do a no-op to test if HID device is ready.
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-
7871
def press(self, *keycodes: int) -> None:
7972
"""Send a report indicating that the given keys have been pressed.
8073

adafruit_hid/mouse.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
99
* Author(s): Dan Halbert
1010
"""
11-
import time
12-
1311
from . import find_device
1412

1513
try:
@@ -29,14 +27,18 @@ class Mouse:
2927
MIDDLE_BUTTON = 4
3028
"""Middle mouse button."""
3129

32-
def __init__(self, devices: Sequence[usb_hid.Device]):
30+
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
3331
"""Create a Mouse object that will send USB mouse HID reports.
3432
33+
:param timeout: Time in seconds to wait for USB to become ready before timing out.
34+
3535
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
3636
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
3737
``usage``.
3838
"""
39-
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02)
39+
self._mouse_device = find_device(
40+
devices, usage_page=0x1, usage=0x02, timeout=timeout
41+
)
4042

4143
# Reuse this bytearray to send mouse reports.
4244
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
@@ -45,16 +47,6 @@ def __init__(self, devices: Sequence[usb_hid.Device]):
4547
# report[3] wheel movement
4648
self.report = bytearray(4)
4749

48-
# Do a no-op to test if HID device is ready.
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.")
57-
5850
def press(self, buttons: int) -> None:
5951
"""Press the given mouse buttons.
6052

0 commit comments

Comments
 (0)