Skip to content

Commit e10533c

Browse files
authored
Merge pull request #42 from adafruit/revert-40-alternate-devices-for-ble
Revert "Allow devices to be passed in, instead of always using usb_hid"
2 parents cd7474c + 6ed148d commit e10533c

File tree

4 files changed

+60
-77
lines changed

4 files changed

+60
-77
lines changed

adafruit_hid/consumer_control.py

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

3940
class ConsumerControl:
4041
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc.
42+
43+
*New in CircuitPython 3.0.*
4144
"""
4245

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.")
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.")
5955

6056
# Reuse this bytearray to send consumer reports.
6157
self._report = bytearray(2)
@@ -85,6 +81,6 @@ def send(self, consumer_code):
8581
consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK)
8682
"""
8783
struct.pack_into("<H", self._report, 0, consumer_code)
88-
self._consumer_device.send_report(self._report)
84+
self.hid_consumer.send_report(self._report)
8985
self._report[0] = self._report[1] = 0x0
90-
self._consumer_device.send_report(self._report)
86+
self.hid_consumer.send_report(self._report)

adafruit_hid/gamepad.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@
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
3136
import struct
3237
import time
38+
import usb_hid
3339

3440
class Gamepad:
3541
"""Emulate a generic gamepad controller with 16 buttons,
@@ -42,22 +48,15 @@ class Gamepad:
4248
The joystick values are in the range -127 to 127.
4349
"""
4450

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.")
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.")
6160

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

161160
if always or self._last_report != self._report:
162-
self._gamepad_device.send_report(self._report)
161+
self._hid_gamepad.send_report(self._report)
163162
# Remember what we sent, without allocating new storage.
164163
self._last_report[:] = self._report
165164

adafruit_hid/keyboard.py

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

3131
import time
3232
from micropython import const
33+
import usb_hid
3334

3435
from .keycode import Keycode
3536

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-
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.")
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.")
5953

6054
# Reuse this bytearray to send keyboard reports.
6155
self.report = bytearray(8)
@@ -104,7 +98,7 @@ def press(self, *keycodes):
10498
"""
10599
for keycode in keycodes:
106100
self._add_keycode_to_report(keycode)
107-
self._keyboard_device.send_report(self.report)
101+
self.hid_keyboard.send_report(self.report)
108102

109103
def release(self, *keycodes):
110104
"""Send a USB HID report indicating that the given keys have been released.
@@ -120,13 +114,13 @@ def release(self, *keycodes):
120114
"""
121115
for keycode in keycodes:
122116
self._remove_keycode_from_report(keycode)
123-
self._keyboard_device.send_report(self.report)
117+
self.hid_keyboard.send_report(self.report)
124118

125119
def release_all(self):
126120
"""Release all pressed keys."""
127121
for i in range(8):
128122
self.report[i] = 0
129-
self._keyboard_device.send_report(self.report)
123+
self.hid_keyboard.send_report(self.report)
130124

131125
def send(self, *keycodes):
132126
"""Press the given keycodes and then release all pressed keys.
@@ -145,12 +139,12 @@ def _add_keycode_to_report(self, keycode):
145139
else:
146140
# Don't press twice.
147141
# (I'd like to use 'not in self.report_keys' here, but that's not implemented.)
148-
for i in range(_MAX_KEYPRESSES):
142+
for i in range(const(self._MAX_KEYPRESSES)):
149143
if self.report_keys[i] == keycode:
150144
# Already pressed.
151145
return
152146
# Put keycode in first empty slot.
153-
for i in range(_MAX_KEYPRESSES):
147+
for i in range(const(self._MAX_KEYPRESSES)):
154148
if self.report_keys[i] == 0:
155149
self.report_keys[i] = keycode
156150
return
@@ -165,6 +159,6 @@ def _remove_keycode_from_report(self, keycode):
165159
self.report_modifier[0] &= ~modifier
166160
else:
167161
# Check all the slots, just in case there's a duplicate. (There should not be.)
168-
for i in range(_MAX_KEYPRESSES):
162+
for i in range(const(self._MAX_KEYPRESSES)):
169163
if self.report_keys[i] == keycode:
170164
self.report_keys[i] = 0

adafruit_hid/mouse.py

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

3233
class Mouse:
3334
"""Send USB HID mouse reports."""
@@ -39,22 +40,15 @@ class Mouse:
3940
MIDDLE_BUTTON = 4
4041
"""Middle mouse button."""
4142

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.")
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.")
5852

5953
# Reuse this bytearray to send mouse reports.
6054
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
@@ -153,7 +147,7 @@ def move(self, x=0, y=0, wheel=0):
153147
self.report[1] = partial_x & 0xff
154148
self.report[2] = partial_y & 0xff
155149
self.report[3] = partial_wheel & 0xff
156-
self._mouse_device.send_report(self.report)
150+
self.hid_mouse.send_report(self.report)
157151
x -= partial_x
158152
y -= partial_y
159153
wheel -= partial_wheel
@@ -163,7 +157,7 @@ def _send_no_move(self):
163157
self.report[1] = 0
164158
self.report[2] = 0
165159
self.report[3] = 0
166-
self._mouse_device.send_report(self.report)
160+
self.hid_mouse.send_report(self.report)
167161

168162
@staticmethod
169163
def _limit(dist):

0 commit comments

Comments
 (0)