Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #117: Rework host USB ready timeout #118

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions adafruit_hid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

# imports
from __future__ import annotations
import time
import supervisor

try:
from typing import Sequence
Expand All @@ -31,17 +33,26 @@


def find_device(
devices: Sequence[usb_hid.Device], *, usage_page: int, usage: int
devices: Sequence[usb_hid.Device], *, usage_page: int, usage: int, timeout: int
) -> usb_hid.Device:
"""Search through the provided sequence of devices to find the one with the matching
usage_page and usage."""
usage_page and usage. Wait up to timeout seconds for USB to become ready."""
if hasattr(devices, "send_report"):
devices = [devices] # type: ignore
for device in devices:
device = None
for dev in devices:
if (
device.usage_page == usage_page
and device.usage == usage
and hasattr(device, "send_report")
dev.usage_page == usage_page
and dev.usage == usage
and hasattr(dev, "send_report")
):
device = dev
break
if device is None:
raise ValueError("Could not find matching HID device.")

for _ in range(timeout):
if supervisor.runtime.usb_connected:
return device
raise ValueError("Could not find matching HID device.")
time.sleep(1.0)
raise OSError("Failed to initialize HID device. Is USB connected?")
17 changes: 6 additions & 11 deletions adafruit_hid/consumer_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

# pylint: disable=wrong-import-position
import struct
import time
from . import find_device

try:
Expand All @@ -31,26 +30,22 @@
class ConsumerControl:
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc."""

def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> 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.

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
``usage``.
"""
self._consumer_device = find_device(devices, usage_page=0x0C, usage=0x01)
self._consumer_device = find_device(
devices, usage_page=0x0C, usage=0x01, timeout=timeout
)

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

# Do a no-op to test if HID device is ready.
# If not, wait a bit and try once more.
try:
self.send(0x0)
except OSError:
time.sleep(1)
self.send(0x0)

def send(self, consumer_code: int) -> None:
"""Send a report to do the specified consumer control action,
and then stop the action (so it will not repeat).
Expand Down
17 changes: 6 additions & 11 deletions adafruit_hid/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* Author(s): Scott Shawcroft, Dan Halbert
"""

import time
from micropython import const
import usb_hid

Expand Down Expand Up @@ -39,14 +38,18 @@ class Keyboard:

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

def __init__(self, devices: Sequence[usb_hid.Device]) -> None:
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> 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.

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
``usage``.
"""
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06)
self._keyboard_device = find_device(
devices, usage_page=0x1, usage=0x06, timeout=timeout
)

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

# Do a no-op to test if HID device is ready.
# If not, wait a bit and try once more.
try:
self.release_all()
except OSError:
time.sleep(1)
self.release_all()

def press(self, *keycodes: int) -> None:
"""Send a report indicating that the given keys have been pressed.

Expand Down
18 changes: 6 additions & 12 deletions adafruit_hid/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

* Author(s): Dan Halbert
"""
import time

from . import find_device

try:
Expand All @@ -29,14 +27,18 @@ class Mouse:
MIDDLE_BUTTON = 4
"""Middle mouse button."""

def __init__(self, devices: Sequence[usb_hid.Device]):
def __init__(self, devices: Sequence[usb_hid.Device], timeout: int = 45) -> 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.

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
``usage``.
"""
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02)
self._mouse_device = find_device(
devices, usage_page=0x1, usage=0x02, timeout=timeout
)

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

# Do a no-op to test if HID device is ready.
# If not, wait a bit and try once more.
try:
self._send_no_move()
except OSError:
time.sleep(1)
self._send_no_move()

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

Expand Down
Loading