Skip to content

update dual mouse example to support 8 byte data packets #5

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 2 commits into from
Apr 3, 2025
Merged
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
21 changes: 17 additions & 4 deletions examples/usb_host_descriptors_two_boot_mice.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,23 @@
BUTTONS = ["left", "right", "middle"]

mouse_bufs = []

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(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: {read_count}, must be 4 or 8")
return delta_x, delta_y


while True:
Expand All @@ -92,12 +105,12 @@
)
except usb.core.USBTimeoutError:
continue

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_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}"
Expand Down