From b18147f894bb7f0d367c7ff360bd7b5eda5510d6 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 3 Apr 2025 15:37:59 -0500 Subject: [PATCH 1/2] update dual mouse example to support 8 byte data packets --- .../usb_host_descriptors_two_boot_mice.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/examples/usb_host_descriptors_two_boot_mice.py b/examples/usb_host_descriptors_two_boot_mice.py index 14c9a59..3827a40 100644 --- a/examples/usb_host_descriptors_two_boot_mice.py +++ b/examples/usb_host_descriptors_two_boot_mice.py @@ -78,26 +78,42 @@ BUTTONS = ["left", "right", "middle"] mouse_bufs = [] +mouse_read_counts = [0, 0] for mouse_tg in mouse_tgs: # Buffer to hold data read from the mouse # Boot mice have 4 byte reports mouse_bufs.append(array.array("b", [0] * 4)) +def get_mouse_deltas(mouse_index): + if mouse_read_counts[mouse_index] == 4: + delta_x = mouse_bufs[mouse_index][1] + delta_y = mouse_bufs[mouse_index][2] + elif mouse_read_counts[mouse_index] == 8: + delta_x = mouse_bufs[mouse_index][2] + delta_y = mouse_bufs[mouse_index][4] + else: + raise ValueError( + f"Unsupported mouse packet size: {mouse_packet_sizes[mouse_index]}, must be 4 or 8" + ) + return delta_x, delta_y + + while True: for mouse_index, mouse in enumerate(mice): try: count = mouse.read( mouse_endpoint_addresses[mouse_index], mouse_bufs[mouse_index], timeout=10 ) + mouse_read_counts[mouse_index] = count except usb.core.USBTimeoutError: continue - + mouse_deltas = get_mouse_deltas(mouse_index) mouse_tgs[mouse_index].x = max( - 0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_bufs[mouse_index][1]) + 0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_deltas[0]) ) mouse_tgs[mouse_index].y = max( - 0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_bufs[mouse_index][2]) + 0, min(display.height - 1, mouse_tgs[mouse_index].y + mouse_deltas[1]) ) out_str = f"{mouse_tgs[mouse_index].x},{mouse_tgs[mouse_index].y}" From fea74c64afb56a615133d32527a2d368ea9f65d7 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 3 Apr 2025 15:49:50 -0500 Subject: [PATCH 2/2] refactor get_mouse_deltas() to accept buffer and read_count instead of index --- .../usb_host_descriptors_two_boot_mice.py | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/examples/usb_host_descriptors_two_boot_mice.py b/examples/usb_host_descriptors_two_boot_mice.py index 3827a40..2cc9369 100644 --- a/examples/usb_host_descriptors_two_boot_mice.py +++ b/examples/usb_host_descriptors_two_boot_mice.py @@ -78,24 +78,22 @@ BUTTONS = ["left", "right", "middle"] mouse_bufs = [] -mouse_read_counts = [0, 0] + for mouse_tg in mouse_tgs: # Buffer to hold data read from the mouse # Boot mice have 4 byte reports - mouse_bufs.append(array.array("b", [0] * 4)) + mouse_bufs.append(array.array("b", [0] * 8)) -def get_mouse_deltas(mouse_index): - if mouse_read_counts[mouse_index] == 4: - delta_x = mouse_bufs[mouse_index][1] - delta_y = mouse_bufs[mouse_index][2] - elif mouse_read_counts[mouse_index] == 8: - delta_x = mouse_bufs[mouse_index][2] - delta_y = mouse_bufs[mouse_index][4] +def get_mouse_deltas(buffer, read_count): + if read_count == 4: + delta_x = buffer[1] + delta_y = buffer[2] + elif read_count == 8: + delta_x = buffer[2] + delta_y = buffer[4] else: - raise ValueError( - f"Unsupported mouse packet size: {mouse_packet_sizes[mouse_index]}, must be 4 or 8" - ) + raise ValueError(f"Unsupported mouse packet size: {read_count}, must be 4 or 8") return delta_x, delta_y @@ -105,10 +103,9 @@ def get_mouse_deltas(mouse_index): count = mouse.read( mouse_endpoint_addresses[mouse_index], mouse_bufs[mouse_index], timeout=10 ) - mouse_read_counts[mouse_index] = count except usb.core.USBTimeoutError: continue - mouse_deltas = get_mouse_deltas(mouse_index) + mouse_deltas = get_mouse_deltas(mouse_bufs[mouse_index], count) mouse_tgs[mouse_index].x = max( 0, min(display.width - 1, mouse_tgs[mouse_index].x + mouse_deltas[0]) )