Skip to content

Commit 03bcf90

Browse files
authored
Merge pull request #10 from RetiredWizard/usbdesccntrls
support gamepads and joysticks
2 parents 84138ca + 28ec72c commit 03bcf90

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

adafruit_usb_host_descriptors.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def get_report_descriptor(device, interface_num, length):
127127
return None
128128

129129

130-
def _is_confirmed_mouse(report_desc):
130+
def _is_confirmed_usage(report_desc, usage_type):
131131
"""
132132
Scans the raw descriptor bytes for:
133133
Usage Page (Generic Desktop) = 0x05, 0x01
@@ -147,15 +147,20 @@ def _is_confirmed_mouse(report_desc):
147147
has_generic_desktop = True
148148

149149
# We look for Usage Mouse (0x09 0x02)
150-
has_mouse_usage = False
150+
has_usage_type = False
151151
for i in range(len(report_desc) - 1):
152-
if report_desc[i] == HID_TAG_USAGE and report_desc[i + 1] == USAGE_MOUSE:
153-
has_mouse_usage = True
152+
if report_desc[i] == HID_TAG_USAGE and report_desc[i + 1] == usage_type:
153+
has_usage_type = True
154154

155-
return has_generic_desktop and has_mouse_usage
155+
return has_generic_desktop and has_usage_type
156156

157157

158-
def _find_endpoint(device, protocol_type: Literal[PROTOCOL_MOUSE, PROTOCOL_KEYBOARD], subclass):
158+
def _find_endpoint(
159+
device,
160+
protocol_type: Literal[PROTOCOL_MOUSE, PROTOCOL_KEYBOARD],
161+
subclass,
162+
usage_type: Literal[USAGE_MOUSE, USAGE_KEYBOARD, USAGE_GAMEPAD, USAGE_JOYSTICK] = USAGE_MOUSE,
163+
):
159164
config_descriptor = get_configuration_descriptor(device, 0)
160165
i = 0
161166
mouse_interface_index = None
@@ -177,7 +182,7 @@ def _find_endpoint(device, protocol_type: Literal[PROTOCOL_MOUSE, PROTOCOL_KEYBO
177182
candidate_found = False
178183
hid_desc_len = 0
179184

180-
# Found mouse or keyboard interface depending on what was requested
185+
# Found mouse or keyboard depending on what was requested
181186
if (
182187
interface_class == INTERFACE_HID
183188
and interface_protocol == protocol_type
@@ -212,7 +217,7 @@ def _find_endpoint(device, protocol_type: Literal[PROTOCOL_MOUSE, PROTOCOL_KEYBO
212217

213218
elif candidate_found:
214219
rep_desc = get_report_descriptor(device, mouse_interface_index, hid_desc_len)
215-
if _is_confirmed_mouse(rep_desc):
220+
if _is_confirmed_usage(rep_desc, usage_type):
216221
return mouse_interface_index, endpoint_address
217222

218223
candidate_found = False # Stop looking at this interface
@@ -249,3 +254,23 @@ def find_boot_keyboard_endpoint(device):
249254
:return: keyboard_interface_index, keyboard_endpoint_address if found, or None, None otherwise
250255
"""
251256
return _find_endpoint(device, PROTOCOL_KEYBOARD, SUBCLASS_BOOT)
257+
258+
259+
def find_gamepad_endpoint(device):
260+
"""
261+
Try to find a report mouse endpoint in the device and return its
262+
interface index, and endpoint address.
263+
:param device: The device to search within
264+
:return: mouse_interface_index, mouse_endpoint_address if found, or None, None otherwise
265+
"""
266+
return _find_endpoint(device, PROTOCOL_MOUSE, SUBCLASS_RESERVED, USAGE_GAMEPAD)
267+
268+
269+
def find_joystick_endpoint(device):
270+
"""
271+
Try to find a report mouse endpoint in the device and return its
272+
interface index, and endpoint address.
273+
:param device: The device to search within
274+
:return: mouse_interface_index, mouse_endpoint_address if found, or None, None otherwise
275+
"""
276+
return _find_endpoint(device, PROTOCOL_MOUSE, SUBCLASS_RESERVED, USAGE_JOYSTICK)

0 commit comments

Comments
 (0)