Skip to content

Commit 2d1dce6

Browse files
authored
Merge pull request #43 from tannewt/pass_in_devices
Pass in devices
2 parents 41c6909 + ea9b71d commit 2d1dce6

9 files changed

+72
-64
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: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,20 @@
3535
# pylint: disable=wrong-import-position
3636
import struct
3737
import time
38-
import usb_hid
38+
from . import find_device
3939

4040
class ConsumerControl:
4141
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc.
42-
43-
*New in CircuitPython 3.0.*
4442
"""
4543

46-
def __init__(self):
47-
"""Create a ConsumerControl object that will send Consumer Control Device HID reports."""
48-
self.hid_consumer = None
49-
for device in usb_hid.devices:
50-
if device.usage_page == 0x0C and device.usage == 0x01:
51-
self.hid_consumer = device
52-
break
53-
if not self.hid_consumer:
54-
raise IOError("Could not find an HID Consumer device.")
44+
def __init__(self, devices):
45+
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
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``.
50+
"""
51+
self._consumer_device = find_device(devices, usage_page=0x0C, usage=0x01)
5552

5653
# Reuse this bytearray to send consumer reports.
5754
self._report = bytearray(2)
@@ -81,6 +78,6 @@ def send(self, consumer_code):
8178
consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK)
8279
"""
8380
struct.pack_into("<H", self._report, 0, consumer_code)
84-
self.hid_consumer.send_report(self._report)
81+
self._consumer_device.send_report(self._report)
8582
self._report[0] = self._report[1] = 0x0
86-
self.hid_consumer.send_report(self._report)
83+
self._consumer_device.send_report(self._report)

adafruit_hid/gamepad.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@
2828
* Author(s): Dan Halbert
2929
"""
3030

31-
import sys
32-
if sys.implementation.version[0] < 3:
33-
raise ImportError('{0} is not supported in CircuitPython 2.x or lower'.format(__name__))
34-
35-
# pylint: disable=wrong-import-position
3631
import struct
3732
import time
38-
import usb_hid
33+
34+
from . import find_device
3935

4036
class Gamepad:
4137
"""Emulate a generic gamepad controller with 16 buttons,
@@ -48,15 +44,14 @@ class Gamepad:
4844
The joystick values are in the range -127 to 127.
4945
"""
5046

51-
def __init__(self):
52-
"""Create a Gamepad object that will send USB gamepad HID reports."""
53-
self._hid_gamepad = None
54-
for device in usb_hid.devices:
55-
if device.usage_page == 0x1 and device.usage == 0x05:
56-
self._hid_gamepad = device
57-
break
58-
if not self._hid_gamepad:
59-
raise IOError("Could not find an HID gampead device.")
47+
def __init__(self, devices):
48+
"""Create a Gamepad object that will send USB gamepad HID reports.
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``.
53+
"""
54+
self._gamepad_device = find_device(devices, usage_page=0x1, usage=0x05)
6055

6156
# Reuse this bytearray to send mouse reports.
6257
# Typically controllers start numbering buttons at 1 rather than 0.
@@ -158,7 +153,7 @@ def _send(self, always=False):
158153
self._joy_z, self._joy_r_z)
159154

160155
if always or self._last_report != self._report:
161-
self._hid_gamepad.send_report(self._report)
156+
self._gamepad_device.send_report(self._report)
162157
# Remember what we sent, without allocating new storage.
163158
self._last_report[:] = self._report
164159

adafruit_hid/keyboard.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@
3030

3131
import time
3232
from micropython import const
33-
import usb_hid
3433

3534
from .keycode import Keycode
3635

36+
from . import find_device
37+
38+
_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.
42-
_MAX_KEYPRESSES = 6
43-
44-
def __init__(self):
45-
"""Create a Keyboard object that will send USB keyboard HID reports."""
46-
self.hid_keyboard = None
47-
for device in usb_hid.devices:
48-
if device.usage_page == 0x1 and device.usage == 0x06:
49-
self.hid_keyboard = device
50-
break
51-
if not self.hid_keyboard:
52-
raise IOError("Could not find an HID keyboard device.")
44+
45+
def __init__(self, devices):
46+
"""Create a Keyboard object that will send keyboard HID reports.
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``.
51+
"""
52+
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06)
5353

5454
# Reuse this bytearray to send keyboard reports.
5555
self.report = bytearray(8)
@@ -98,7 +98,7 @@ def press(self, *keycodes):
9898
"""
9999
for keycode in keycodes:
100100
self._add_keycode_to_report(keycode)
101-
self.hid_keyboard.send_report(self.report)
101+
self._keyboard_device.send_report(self.report)
102102

103103
def release(self, *keycodes):
104104
"""Send a USB HID report indicating that the given keys have been released.
@@ -114,13 +114,13 @@ def release(self, *keycodes):
114114
"""
115115
for keycode in keycodes:
116116
self._remove_keycode_from_report(keycode)
117-
self.hid_keyboard.send_report(self.report)
117+
self._keyboard_device.send_report(self.report)
118118

119119
def release_all(self):
120120
"""Release all pressed keys."""
121121
for i in range(8):
122122
self.report[i] = 0
123-
self.hid_keyboard.send_report(self.report)
123+
self._keyboard_device.send_report(self.report)
124124

125125
def send(self, *keycodes):
126126
"""Press the given keycodes and then release all pressed keys.
@@ -139,12 +139,12 @@ def _add_keycode_to_report(self, keycode):
139139
else:
140140
# Don't press twice.
141141
# (I'd like to use 'not in self.report_keys' here, but that's not implemented.)
142-
for i in range(const(self._MAX_KEYPRESSES)):
142+
for i in range(_MAX_KEYPRESSES):
143143
if self.report_keys[i] == keycode:
144144
# Already pressed.
145145
return
146146
# Put keycode in first empty slot.
147-
for i in range(const(self._MAX_KEYPRESSES)):
147+
for i in range(_MAX_KEYPRESSES):
148148
if self.report_keys[i] == 0:
149149
self.report_keys[i] = keycode
150150
return
@@ -159,6 +159,6 @@ def _remove_keycode_from_report(self, keycode):
159159
self.report_modifier[0] &= ~modifier
160160
else:
161161
# Check all the slots, just in case there's a duplicate. (There should not be.)
162-
for i in range(const(self._MAX_KEYPRESSES)):
162+
for i in range(_MAX_KEYPRESSES):
163163
if self.report_keys[i] == keycode:
164164
self.report_keys[i] = 0

adafruit_hid/mouse.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
* Author(s): Dan Halbert
2929
"""
3030
import time
31-
import usb_hid
31+
32+
from . import find_device
3233

3334
class Mouse:
3435
"""Send USB HID mouse reports."""
@@ -40,15 +41,14 @@ class Mouse:
4041
MIDDLE_BUTTON = 4
4142
"""Middle mouse button."""
4243

43-
def __init__(self):
44-
"""Create a Mouse object that will send USB mouse HID reports."""
45-
self.hid_mouse = None
46-
for device in usb_hid.devices:
47-
if device.usage_page == 0x1 and device.usage == 0x02:
48-
self.hid_mouse = device
49-
break
50-
if not self.hid_mouse:
51-
raise IOError("Could not find an HID mouse device.")
44+
def __init__(self, devices):
45+
"""Create a Mouse object that will send USB mouse HID reports.
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``.
50+
"""
51+
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02)
5252

5353
# Reuse this bytearray to send mouse reports.
5454
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
@@ -147,7 +147,7 @@ def move(self, x=0, y=0, wheel=0):
147147
self.report[1] = partial_x & 0xff
148148
self.report[2] = partial_y & 0xff
149149
self.report[3] = partial_wheel & 0xff
150-
self.hid_mouse.send_report(self.report)
150+
self._mouse_device.send_report(self.report)
151151
x -= partial_x
152152
y -= partial_y
153153
wheel -= partial_wheel
@@ -157,7 +157,7 @@ def _send_no_move(self):
157157
self.report[1] = 0
158158
self.report[2] = 0
159159
self.report[3] = 0
160-
self.hid_mouse.send_report(self.report)
160+
self._mouse_device.send_report(self.report)
161161

162162
@staticmethod
163163
def _limit(dist):

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)