Skip to content

Commit ea9b71d

Browse files
committed
Require devices to be passed in so that they can be from USB or BLE.
1 parent 8586bee commit ea9b71d

9 files changed

+51
-60
lines changed

adafruit_hid/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@
3939

4040
__version__ = "0.0.0-auto.0"
4141
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HID.git"
42+
43+
def find_device(devices, *, usage_page, usage):
44+
"""Search through the provided list of devices to find the one with the matching usage_page and
45+
usage."""
46+
if hasattr(devices, "send_report"):
47+
devices = [devices]
48+
for device in devices:
49+
if (device.usage_page == usage_page and
50+
device.usage == usage and
51+
hasattr(device, "send_report")):
52+
return device
53+
raise ValueError("Could not find matching HID device.")

adafruit_hid/consumer_control.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,20 @@
3535
# pylint: disable=wrong-import-position
3636
import struct
3737
import time
38+
from . import find_device
3839

3940
class ConsumerControl:
4041
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc.
4142
"""
4243

43-
def __init__(self, consumer_device=None):
44+
def __init__(self, devices):
4445
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
45-
If consumer_device is None (the default), find a usb_hid device to use.
46-
But an equivalent device can be supplied instead for other kinds of consumer devices,
47-
such as BLE.
48-
It only needs to implement ``send_report()``.
46+
47+
Devices can be a list of devices that includes a Consumer Control device or a CC device
48+
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
49+
``usage``.
4950
"""
50-
self._consumer_device = consumer_device
51-
if not self._consumer_device:
52-
import usb_hid
53-
for device in usb_hid.devices:
54-
if device.usage_page == 0x0C and device.usage == 0x01:
55-
self._consumer_device = device
56-
break
57-
if not self._consumer_device:
58-
raise IOError("Could not find an HID Consumer device.")
51+
self._consumer_device = find_device(devices, usage_page=0x0C, usage=0x01)
5952

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

adafruit_hid/gamepad.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import struct
3232
import time
3333

34+
from . import find_device
35+
3436
class Gamepad:
3537
"""Emulate a generic gamepad controller with 16 buttons,
3638
numbered 1-16, and two joysticks, one controlling
@@ -42,22 +44,14 @@ class Gamepad:
4244
The joystick values are in the range -127 to 127.
4345
"""
4446

45-
def __init__(self, gamepad_device=None):
47+
def __init__(self, devices):
4648
"""Create a Gamepad object that will send USB gamepad HID reports.
47-
If gamepad_device is None (the default), find a usb_hid device to use.
48-
But an equivalent device can be supplied instead for other kinds of gamepads,
49-
such as BLE.
50-
It only needs to implement ``send_report()``.
49+
50+
Devices can be a list of devices that includes a gamepad device or a gamepad device
51+
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
52+
``usage``.
5153
"""
52-
self._gamepad_device = gamepad_device
53-
if self._gamepad_device is None:
54-
import usb_hid
55-
for device in usb_hid.devices:
56-
if device.usage_page == 0x1 and device.usage == 0x05:
57-
self._gamepad_device = device
58-
break
59-
if not self._gamepad_device:
60-
raise IOError("Could not find an HID gamepad device.")
54+
self._gamepad_device = find_device(devices, usage_page=0x1, usage=0x05)
6155

6256
# Reuse this bytearray to send mouse reports.
6357
# Typically controllers start numbering buttons at 1 rather than 0.

adafruit_hid/keyboard.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,23 @@
3333

3434
from .keycode import Keycode
3535

36+
from . import find_device
37+
3638
_MAX_KEYPRESSES = const(6)
3739

3840
class Keyboard:
3941
"""Send HID keyboard reports."""
4042

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

43-
def __init__(self, keyboard_device=None):
45+
def __init__(self, devices):
4446
"""Create a Keyboard object that will send keyboard HID reports.
45-
If keyboard_device is None (the default), find a usb_hid device to use.
46-
But an equivalent device can be supplied instead for other kinds of keyboards,
47-
such as BLE.
48-
It only needs to implement `send_report()`.
47+
48+
Devices can be a list of devices that includes a keyboard device or a keyboard device
49+
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
50+
``usage``.
4951
"""
50-
self._keyboard_device = keyboard_device
51-
if not self._keyboard_device:
52-
import usb_hid
53-
for device in usb_hid.devices:
54-
if device.usage_page == 0x1 and device.usage == 0x06:
55-
self._keyboard_device = device
56-
break
57-
if not self._keyboard_device:
58-
raise IOError("Could not find an HID keyboard device.")
52+
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06)
5953

6054
# Reuse this bytearray to send keyboard reports.
6155
self.report = bytearray(8)

adafruit_hid/mouse.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"""
3030
import time
3131

32+
from . import find_device
33+
3234
class Mouse:
3335
"""Send USB HID mouse reports."""
3436

@@ -39,22 +41,14 @@ class Mouse:
3941
MIDDLE_BUTTON = 4
4042
"""Middle mouse button."""
4143

42-
def __init__(self, mouse_device=None):
44+
def __init__(self, devices):
4345
"""Create a Mouse object that will send USB mouse HID reports.
44-
If mouse_device is None (the default), find a usb_hid device to use.
45-
But an equivalent device can be supplied instead for other kinds of nice,
46-
such as BLE.
47-
It only needs to implement ``send_report()``.
46+
47+
Devices can be a list of devices that includes a keyboard device or a keyboard device
48+
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
49+
``usage``.
4850
"""
49-
self._mouse_device = mouse_device
50-
if not self._mouse_device:
51-
import usb_hid
52-
for device in usb_hid.devices:
53-
if device.usage_page == 0x1 and device.usage == 0x02:
54-
self._mouse_device = device
55-
break
56-
if not self._mouse_device:
57-
raise IOError("Could not find an HID mouse device.")
51+
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02)
5852

5953
# Reuse this bytearray to send mouse reports.
6054
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)

examples/hid_joywing_gamepad.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from micropython import const
88
import adafruit_seesaw
99
from adafruit_hid.gamepad import Gamepad
10+
import usb_hid
1011

1112
def range_map(value, in_min, in_max, out_min, out_max):
1213
return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
@@ -31,7 +32,7 @@ def range_map(value, in_min, in_max, out_min, out_max):
3132
last_game_x = 0
3233
last_game_y = 0
3334

34-
g = Gamepad()
35+
g = Gamepad(usb_hid.devices)
3536

3637
while True:
3738
x = ss.analog_read(2)

examples/hid_keyboard_shortcuts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import digitalio
44
from adafruit_hid.keyboard import Keyboard
55
from adafruit_hid.keycode import Keycode
6+
import usb_hid
67

7-
kbd = Keyboard()
8+
kbd = Keyboard(usb_hid.devices)
89

910
# define buttons. these can be any physical switches/buttons, but the values
1011
# here work out-of-the-box with a CircuitPlayground Express' A and B buttons.

examples/hid_simple_gamepad.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import board
22
import digitalio
33
import analogio
4+
import usb_hid
45

56
from adafruit_hid.gamepad import Gamepad
67

7-
gp = Gamepad()
8+
gp = Gamepad(usb_hid.devices)
89

910
# Create some buttons. The physical buttons are connected
1011
# to ground on one side and these and these pins on the other.

examples/hid_simpletest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import time
22
import board
33
import digitalio
4+
import usb_hid
45
from adafruit_hid.mouse import Mouse
56

6-
mouse = Mouse()
7+
mouse = Mouse(usb_hid.devices)
78

89
# define buttons. these can be any physical switches/buttons, but the values
910
# here work out-of-the-box with a CircuitPlayground Express' A and B buttons.

0 commit comments

Comments
 (0)