Skip to content

Commit

Permalink
second round code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Abeles committed Sep 13, 2023
1 parent 5bf6505 commit 9b398e0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
38 changes: 31 additions & 7 deletions adafruit_hid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
# imports
from __future__ import annotations
import time
import supervisor

try:
import supervisor
except ImportError:
supervisor = None

try:
from typing import Sequence
Expand All @@ -33,10 +37,18 @@


def find_device(
devices: Sequence[usb_hid.Device], *, usage_page: int, usage: int, timeout: int
devices: Sequence[usb_hid.Device],
*,
usage_page: int,
usage: int,
timeout: int = None,
) -> usb_hid.Device:
"""Search through the provided sequence of devices to find the one with the matching
usage_page and usage. Wait up to timeout seconds for USB to become ready."""
usage_page and usage.
:param timeout: Time in seconds to wait for USB to become ready before timing out.
Defaults to None to wait indefinitely."""

if hasattr(devices, "send_report"):
devices = [devices] # type: ignore
device = None
Expand All @@ -51,8 +63,20 @@ def find_device(
if device is None:
raise ValueError("Could not find matching HID device.")

for _ in range(timeout):
if supervisor.runtime.usb_connected:
return device
if supervisor is None:
# Blinka doesn't have supervisor (see issue Adafruit_Blinka#711), so wait
# one second for USB to become ready
time.sleep(1.0)
raise OSError("Failed to initialize HID device. Is USB connected?")
elif timeout is None:
# default behavior: wait indefinitely for USB to become ready
while not supervisor.runtime.usb_connected:
time.sleep(1.0)
else:
# wait up to timeout seconds for USB to become ready
for _ in range(timeout):
if supervisor.runtime.usb_connected:
return device
time.sleep(1.0)
raise OSError("Failed to initialize HID device. Is USB connected?")

return device
3 changes: 2 additions & 1 deletion adafruit_hid/consumer_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
class ConsumerControl:
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc."""

def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> None:
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
:param timeout: Time in seconds to wait for USB to become ready before timing out.
Defaults to None to wait indefinitely.
Devices can be a sequence of devices that includes a Consumer Control device or a CC device
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
Expand Down
3 changes: 2 additions & 1 deletion adafruit_hid/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class Keyboard:

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

def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> None:
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = None) -> None:
"""Create a Keyboard object that will send keyboard HID reports.
:param timeout: Time in seconds to wait for USB to become ready before timing out.
Defaults to None to wait indefinitely.
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
Expand Down
3 changes: 2 additions & 1 deletion adafruit_hid/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ class Mouse:
MIDDLE_BUTTON = 4
"""Middle mouse button."""

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

0 comments on commit 9b398e0

Please sign in to comment.