Add configurable mouse movement and improve HID handling

This commit is contained in:
2026-08-26 20:52:20 +02:00
parent 2a0869743f
commit be37343ec1
6 changed files with 48 additions and 29 deletions
+15 -14
View File
@@ -77,16 +77,8 @@ class HID:
self._usb.active(True)
def _open_interface(self, interface_descriptor):
# Endpoint descriptors follow the nine-byte interface descriptor.
offset = 9
while offset + 1 < len(interface_descriptor):
length = interface_descriptor[offset]
descriptor_type = interface_descriptor[offset + 1]
if length < 2:
break
if descriptor_type == 0x05:
self._usb.ep_open(interface_descriptor[offset:offset + length])
offset += length
# MicroPython opens the endpoints described by this accepted interface;
# USBDevice 1.29 does not expose an ep_open() method to Python.
self._ready = True
def _reset(self):
@@ -95,9 +87,18 @@ class HID:
self._pending = []
def _control_transfer(self, stage, request):
if (stage == 1 and request[1] == _GET_DESCRIPTOR and
# The report descriptor buffer is supplied at SETUP, but the same
# request must be accepted through its DATA and ACK stages.
if (request[1] == _GET_DESCRIPTOR and
request[3] == _DESCRIPTOR_TYPE_REPORT):
return REPORT_DESCRIPTOR
return REPORT_DESCRIPTOR if stage == 1 else True
# Desktop HID drivers commonly issue these class requests while
# initialising. There is no protocol/idle state to maintain here, but
# they must not be stalled.
if request[1] in (0x0A, 0x0B): # SET_IDLE, SET_PROTOCOL
return True
return False
def _transfer_done(self, endpoint, result, transferred):
@@ -126,8 +127,8 @@ class HID:
self._send(bytes((1, 0, 0, 0, 0, 0, 0, 0, 0)))
def move(self, x, y):
x = max(-1, min(1, int(x)))
y = max(-1, min(1, int(y)))
x = max(-127, min(127, int(x)))
y = max(-127, min(127, int(y)))
self._send(bytes((2, 0, x & 0xFF, y & 0xFF, 0)))
def press_volume_up(self):