diff --git a/adafruit_hid/__init__.py b/adafruit_hid/__init__.py index d30fb32..65e459b 100644 --- a/adafruit_hid/__init__.py +++ b/adafruit_hid/__init__.py @@ -39,3 +39,15 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HID.git" + +def find_device(devices, *, usage_page, usage): + """Search through the provided list of devices to find the one with the matching usage_page and + usage.""" + if hasattr(devices, "send_report"): + devices = [devices] + for device in devices: + if (device.usage_page == usage_page and + device.usage == usage and + hasattr(device, "send_report")): + return device + raise ValueError("Could not find matching HID device.") diff --git a/adafruit_hid/consumer_control.py b/adafruit_hid/consumer_control.py index 76d3fa0..b22da45 100644 --- a/adafruit_hid/consumer_control.py +++ b/adafruit_hid/consumer_control.py @@ -35,23 +35,20 @@ # pylint: disable=wrong-import-position import struct import time -import usb_hid +from . import find_device class ConsumerControl: """Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc. - - *New in CircuitPython 3.0.* """ - def __init__(self): - """Create a ConsumerControl object that will send Consumer Control Device HID reports.""" - self.hid_consumer = None - for device in usb_hid.devices: - if device.usage_page == 0x0C and device.usage == 0x01: - self.hid_consumer = device - break - if not self.hid_consumer: - raise IOError("Could not find an HID Consumer device.") + def __init__(self, devices): + """Create a ConsumerControl object that will send Consumer Control Device HID reports. + + Devices can be a list 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) # Reuse this bytearray to send consumer reports. self._report = bytearray(2) @@ -81,6 +78,6 @@ def send(self, consumer_code): consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK) """ struct.pack_into("