Skip to content

Revert "Allow devices to be passed in, instead of always using usb_hid" #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions adafruit_hid/consumer_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,23 @@
# pylint: disable=wrong-import-position
import struct
import time
import usb_hid

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

*New in CircuitPython 3.0.*
"""

def __init__(self, consumer_device=None):
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
If consumer_device is None (the default), find a usb_hid device to use.
But an equivalent device can be supplied instead for other kinds of consumer devices,
such as BLE.
It only needs to implement ``send_report()``.
"""
self._consumer_device = consumer_device
if not self._consumer_device:
import usb_hid
for device in usb_hid.devices:
if device.usage_page == 0x0C and device.usage == 0x01:
self._consumer_device = device
break
if not self._consumer_device:
raise IOError("Could not find an HID Consumer device.")
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.")

# Reuse this bytearray to send consumer reports.
self._report = bytearray(2)
Expand Down Expand Up @@ -85,6 +81,6 @@ def send(self, consumer_code):
consumer_control.send(ConsumerControlCode.SCAN_NEXT_TRACK)
"""
struct.pack_into("<H", self._report, 0, consumer_code)
self._consumer_device.send_report(self._report)
self.hid_consumer.send_report(self._report)
self._report[0] = self._report[1] = 0x0
self._consumer_device.send_report(self._report)
self.hid_consumer.send_report(self._report)
33 changes: 16 additions & 17 deletions adafruit_hid/gamepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
* 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

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

def __init__(self, gamepad_device=None):
"""Create a Gamepad object that will send USB gamepad HID reports.
If gamepad_device is None (the default), find a usb_hid device to use.
But an equivalent device can be supplied instead for other kinds of gamepads,
such as BLE.
It only needs to implement ``send_report()``.
"""
self._gamepad_device = gamepad_device
if self._gamepad_device is None:
import usb_hid
for device in usb_hid.devices:
if device.usage_page == 0x1 and device.usage == 0x05:
self._gamepad_device = device
break
if not self._gamepad_device:
raise IOError("Could not find an HID gamepad device.")
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.")

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

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

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

import time
from micropython import const
import usb_hid

from .keycode import Keycode

_MAX_KEYPRESSES = const(6)

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

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

def __init__(self, keyboard_device=None):
"""Create a Keyboard object that will send keyboard HID reports.
If keyboard_device is None (the default), find a usb_hid device to use.
But an equivalent device can be supplied instead for other kinds of keyboards,
such as BLE.
It only needs to implement `send_report()`.
"""
self._keyboard_device = keyboard_device
if not self._keyboard_device:
import usb_hid
for device in usb_hid.devices:
if device.usage_page == 0x1 and device.usage == 0x06:
self._keyboard_device = device
break
if not self._keyboard_device:
raise IOError("Could not find an HID keyboard device.")
_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.")

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

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

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

def send(self, *keycodes):
"""Press the given keycodes and then release all pressed keys.
Expand All @@ -145,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(_MAX_KEYPRESSES):
for i in range(const(self._MAX_KEYPRESSES)):
if self.report_keys[i] == keycode:
# Already pressed.
return
# Put keycode in first empty slot.
for i in range(_MAX_KEYPRESSES):
for i in range(const(self._MAX_KEYPRESSES)):
if self.report_keys[i] == 0:
self.report_keys[i] = keycode
return
Expand All @@ -165,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(_MAX_KEYPRESSES):
for i in range(const(self._MAX_KEYPRESSES)):
if self.report_keys[i] == keycode:
self.report_keys[i] = 0
30 changes: 12 additions & 18 deletions adafruit_hid/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Author(s): Dan Halbert
"""
import time
import usb_hid

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

def __init__(self, mouse_device=None):
"""Create a Mouse object that will send USB mouse HID reports.
If mouse_device is None (the default), find a usb_hid device to use.
But an equivalent device can be supplied instead for other kinds of nice,
such as BLE.
It only needs to implement ``send_report()``.
"""
self._mouse_device = mouse_device
if not self._mouse_device:
import usb_hid
for device in usb_hid.devices:
if device.usage_page == 0x1 and device.usage == 0x02:
self._mouse_device = device
break
if not self._mouse_device:
raise IOError("Could not find an HID mouse device.")
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.")

# Reuse this bytearray to send mouse reports.
# report[0] buttons pressed (LEFT, MIDDLE, RIGHT)
Expand Down Expand Up @@ -153,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._mouse_device.send_report(self.report)
self.hid_mouse.send_report(self.report)
x -= partial_x
y -= partial_y
wheel -= partial_wheel
Expand All @@ -163,7 +157,7 @@ def _send_no_move(self):
self.report[1] = 0
self.report[2] = 0
self.report[3] = 0
self._mouse_device.send_report(self.report)
self.hid_mouse.send_report(self.report)

@staticmethod
def _limit(dist):
Expand Down