Skip to content

Commit

Permalink
Merge pull request #43 from tannewt/pass_in_devices
Browse files Browse the repository at this point in the history
Pass in devices
  • Loading branch information
tannewt authored Jan 7, 2020
2 parents 41c6909 + ea9b71d commit 2d1dce6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 64 deletions.
12 changes: 12 additions & 0 deletions adafruit_hid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
25 changes: 11 additions & 14 deletions adafruit_hid/consumer_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -81,6 +78,6 @@ def send(self, consumer_code):
consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK)
"""
struct.pack_into("<H", self._report, 0, consumer_code)
self.hid_consumer.send_report(self._report)
self._consumer_device.send_report(self._report)
self._report[0] = self._report[1] = 0x0
self.hid_consumer.send_report(self._report)
self._consumer_device.send_report(self._report)
27 changes: 11 additions & 16 deletions adafruit_hid/gamepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
* Author(s): Dan Halbert
"""

import sys
if sys.implementation.version[0] < 3:
raise ImportError('{0} is not supported in CircuitPython 2.x or lower'.format(__name__))

# pylint: disable=wrong-import-position
import struct
import time
import usb_hid

from . import find_device

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

def __init__(self):
"""Create a Gamepad object that will send USB gamepad HID reports."""
self._hid_gamepad = None
for device in usb_hid.devices:
if device.usage_page == 0x1 and device.usage == 0x05:
self._hid_gamepad = device
break
if not self._hid_gamepad:
raise IOError("Could not find an HID gampead device.")
def __init__(self, devices):
"""Create a Gamepad object that will send USB gamepad HID reports.
Devices can be a list of devices that includes a gamepad device or a gamepad device
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
``usage``.
"""
self._gamepad_device = find_device(devices, usage_page=0x1, usage=0x05)

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

if always or self._last_report != self._report:
self._hid_gamepad.send_report(self._report)
self._gamepad_device.send_report(self._report)
# Remember what we sent, without allocating new storage.
self._last_report[:] = self._report

Expand Down
36 changes: 18 additions & 18 deletions adafruit_hid/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@

import time
from micropython import const
import usb_hid

from .keycode import Keycode

from . import find_device

_MAX_KEYPRESSES = const(6)

class Keyboard:
"""Send HID keyboard reports."""

# No more than _MAX_KEYPRESSES regular keys may be pressed at once.
_MAX_KEYPRESSES = 6

def __init__(self):
"""Create a Keyboard object that will send USB keyboard HID reports."""
self.hid_keyboard = None
for device in usb_hid.devices:
if device.usage_page == 0x1 and device.usage == 0x06:
self.hid_keyboard = device
break
if not self.hid_keyboard:
raise IOError("Could not find an HID keyboard device.")

def __init__(self, devices):
"""Create a Keyboard object that will send keyboard HID reports.
Devices can be a list of devices that includes a keyboard device or a keyboard device
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
``usage``.
"""
self._keyboard_device = find_device(devices, usage_page=0x1, usage=0x06)

# Reuse this bytearray to send keyboard reports.
self.report = bytearray(8)
Expand Down Expand Up @@ -98,7 +98,7 @@ def press(self, *keycodes):
"""
for keycode in keycodes:
self._add_keycode_to_report(keycode)
self.hid_keyboard.send_report(self.report)
self._keyboard_device.send_report(self.report)

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

def release_all(self):
"""Release all pressed keys."""
for i in range(8):
self.report[i] = 0
self.hid_keyboard.send_report(self.report)
self._keyboard_device.send_report(self.report)

def send(self, *keycodes):
"""Press the given keycodes and then release all pressed keys.
Expand All @@ -139,12 +139,12 @@ def _add_keycode_to_report(self, keycode):
else:
# Don't press twice.
# (I'd like to use 'not in self.report_keys' here, but that's not implemented.)
for i in range(const(self._MAX_KEYPRESSES)):
for i in range(_MAX_KEYPRESSES):
if self.report_keys[i] == keycode:
# Already pressed.
return
# Put keycode in first empty slot.
for i in range(const(self._MAX_KEYPRESSES)):
for i in range(_MAX_KEYPRESSES):
if self.report_keys[i] == 0:
self.report_keys[i] = keycode
return
Expand All @@ -159,6 +159,6 @@ def _remove_keycode_from_report(self, keycode):
self.report_modifier[0] &= ~modifier
else:
# Check all the slots, just in case there's a duplicate. (There should not be.)
for i in range(const(self._MAX_KEYPRESSES)):
for i in range(_MAX_KEYPRESSES):
if self.report_keys[i] == keycode:
self.report_keys[i] = 0
24 changes: 12 additions & 12 deletions adafruit_hid/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
* Author(s): Dan Halbert
"""
import time
import usb_hid

from . import find_device

class Mouse:
"""Send USB HID mouse reports."""
Expand All @@ -40,15 +41,14 @@ class Mouse:
MIDDLE_BUTTON = 4
"""Middle mouse button."""

def __init__(self):
"""Create a Mouse object that will send USB mouse HID reports."""
self.hid_mouse = None
for device in usb_hid.devices:
if device.usage_page == 0x1 and device.usage == 0x02:
self.hid_mouse = device
break
if not self.hid_mouse:
raise IOError("Could not find an HID mouse device.")
def __init__(self, devices):
"""Create a Mouse object that will send USB mouse HID reports.
Devices can be a list of devices that includes a keyboard device or a keyboard device
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
``usage``.
"""
self._mouse_device = find_device(devices, usage_page=0x1, usage=0x02)

# Reuse this bytearray to send mouse reports.
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
Expand Down Expand Up @@ -147,7 +147,7 @@ def move(self, x=0, y=0, wheel=0):
self.report[1] = partial_x & 0xff
self.report[2] = partial_y & 0xff
self.report[3] = partial_wheel & 0xff
self.hid_mouse.send_report(self.report)
self._mouse_device.send_report(self.report)
x -= partial_x
y -= partial_y
wheel -= partial_wheel
Expand All @@ -157,7 +157,7 @@ def _send_no_move(self):
self.report[1] = 0
self.report[2] = 0
self.report[3] = 0
self.hid_mouse.send_report(self.report)
self._mouse_device.send_report(self.report)

@staticmethod
def _limit(dist):
Expand Down
3 changes: 2 additions & 1 deletion examples/hid_joywing_gamepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from micropython import const
import adafruit_seesaw
from adafruit_hid.gamepad import Gamepad
import usb_hid

def range_map(value, in_min, in_max, out_min, out_max):
return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
Expand All @@ -31,7 +32,7 @@ def range_map(value, in_min, in_max, out_min, out_max):
last_game_x = 0
last_game_y = 0

g = Gamepad()
g = Gamepad(usb_hid.devices)

while True:
x = ss.analog_read(2)
Expand Down
3 changes: 2 additions & 1 deletion examples/hid_keyboard_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import usb_hid

kbd = Keyboard()
kbd = Keyboard(usb_hid.devices)

# define buttons. these can be any physical switches/buttons, but the values
# here work out-of-the-box with a CircuitPlayground Express' A and B buttons.
Expand Down
3 changes: 2 additions & 1 deletion examples/hid_simple_gamepad.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import board
import digitalio
import analogio
import usb_hid

from adafruit_hid.gamepad import Gamepad

gp = Gamepad()
gp = Gamepad(usb_hid.devices)

# Create some buttons. The physical buttons are connected
# to ground on one side and these and these pins on the other.
Expand Down
3 changes: 2 additions & 1 deletion examples/hid_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import time
import board
import digitalio
import usb_hid
from adafruit_hid.mouse import Mouse

mouse = Mouse()
mouse = Mouse(usb_hid.devices)

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

0 comments on commit 2d1dce6

Please sign in to comment.