Skip to content

Commit 8586bee

Browse files
dhalberttannewt
authored andcommitted
Allow devices to be passed in, instead of always using usb_hid
1 parent a23b805 commit 8586bee

File tree

4 files changed

+77
-60
lines changed

4 files changed

+77
-60
lines changed

adafruit_hid/consumer_control.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,27 @@
3535
# pylint: disable=wrong-import-position
3636
import struct
3737
import time
38-
import usb_hid
3938

4039
class ConsumerControl:
4140
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc.
42-
43-
*New in CircuitPython 3.0.*
4441
"""
4542

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.")
43+
def __init__(self, consumer_device=None):
44+
"""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()``.
49+
"""
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.")
5559

5660
# Reuse this bytearray to send consumer reports.
5761
self._report = bytearray(2)
@@ -81,6 +85,6 @@ def send(self, consumer_code):
8185
consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK)
8286
"""
8387
struct.pack_into("<H", self._report, 0, consumer_code)
84-
self.hid_consumer.send_report(self._report)
88+
self._consumer_device.send_report(self._report)
8589
self._report[0] = self._report[1] = 0x0
86-
self.hid_consumer.send_report(self._report)
90+
self._consumer_device.send_report(self._report)

adafruit_hid/gamepad.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@
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
3933

4034
class Gamepad:
4135
"""Emulate a generic gamepad controller with 16 buttons,
@@ -48,15 +42,22 @@ class Gamepad:
4842
The joystick values are in the range -127 to 127.
4943
"""
5044

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.")
45+
def __init__(self, gamepad_device=None):
46+
"""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()``.
51+
"""
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.")
6061

6162
# Reuse this bytearray to send mouse reports.
6263
# Typically controllers start numbering buttons at 1 rather than 0.
@@ -158,7 +159,7 @@ def _send(self, always=False):
158159
self._joy_z, self._joy_r_z)
159160

160161
if always or self._last_report != self._report:
161-
self._hid_gamepad.send_report(self._report)
162+
self._gamepad_device.send_report(self._report)
162163
# Remember what we sent, without allocating new storage.
163164
self._last_report[:] = self._report
164165

adafruit_hid/keyboard.py

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

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

3534
from .keycode import Keycode
3635

36+
_MAX_KEYPRESSES = const(6)
3737

3838
class Keyboard:
3939
"""Send HID keyboard reports."""
4040

4141
# 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.")
42+
43+
def __init__(self, keyboard_device=None):
44+
"""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()`.
49+
"""
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.")
5359

5460
# Reuse this bytearray to send keyboard reports.
5561
self.report = bytearray(8)
@@ -98,7 +104,7 @@ def press(self, *keycodes):
98104
"""
99105
for keycode in keycodes:
100106
self._add_keycode_to_report(keycode)
101-
self.hid_keyboard.send_report(self.report)
107+
self._keyboard_device.send_report(self.report)
102108

103109
def release(self, *keycodes):
104110
"""Send a USB HID report indicating that the given keys have been released.
@@ -114,13 +120,13 @@ def release(self, *keycodes):
114120
"""
115121
for keycode in keycodes:
116122
self._remove_keycode_from_report(keycode)
117-
self.hid_keyboard.send_report(self.report)
123+
self._keyboard_device.send_report(self.report)
118124

119125
def release_all(self):
120126
"""Release all pressed keys."""
121127
for i in range(8):
122128
self.report[i] = 0
123-
self.hid_keyboard.send_report(self.report)
129+
self._keyboard_device.send_report(self.report)
124130

125131
def send(self, *keycodes):
126132
"""Press the given keycodes and then release all pressed keys.
@@ -139,12 +145,12 @@ def _add_keycode_to_report(self, keycode):
139145
else:
140146
# Don't press twice.
141147
# (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)):
148+
for i in range(_MAX_KEYPRESSES):
143149
if self.report_keys[i] == keycode:
144150
# Already pressed.
145151
return
146152
# Put keycode in first empty slot.
147-
for i in range(const(self._MAX_KEYPRESSES)):
153+
for i in range(_MAX_KEYPRESSES):
148154
if self.report_keys[i] == 0:
149155
self.report_keys[i] = keycode
150156
return
@@ -159,6 +165,6 @@ def _remove_keycode_from_report(self, keycode):
159165
self.report_modifier[0] &= ~modifier
160166
else:
161167
# Check all the slots, just in case there's a duplicate. (There should not be.)
162-
for i in range(const(self._MAX_KEYPRESSES)):
168+
for i in range(_MAX_KEYPRESSES):
163169
if self.report_keys[i] == keycode:
164170
self.report_keys[i] = 0

adafruit_hid/mouse.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
* Author(s): Dan Halbert
2929
"""
3030
import time
31-
import usb_hid
3231

3332
class Mouse:
3433
"""Send USB HID mouse reports."""
@@ -40,15 +39,22 @@ class Mouse:
4039
MIDDLE_BUTTON = 4
4140
"""Middle mouse button."""
4241

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.")
42+
def __init__(self, mouse_device=None):
43+
"""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()``.
48+
"""
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.")
5258

5359
# Reuse this bytearray to send mouse reports.
5460
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
@@ -147,7 +153,7 @@ def move(self, x=0, y=0, wheel=0):
147153
self.report[1] = partial_x & 0xff
148154
self.report[2] = partial_y & 0xff
149155
self.report[3] = partial_wheel & 0xff
150-
self.hid_mouse.send_report(self.report)
156+
self._mouse_device.send_report(self.report)
151157
x -= partial_x
152158
y -= partial_y
153159
wheel -= partial_wheel
@@ -157,7 +163,7 @@ def _send_no_move(self):
157163
self.report[1] = 0
158164
self.report[2] = 0
159165
self.report[3] = 0
160-
self.hid_mouse.send_report(self.report)
166+
self._mouse_device.send_report(self.report)
161167

162168
@staticmethod
163169
def _limit(dist):

0 commit comments

Comments
 (0)