Skip to content

Commit 318448c

Browse files
committed
Use find_boot_mouse_endpoint
1 parent a011ecf commit 318448c

File tree

1 file changed

+25
-5
lines changed
  • Metro/Metro_RP2350_Minesweeper

1 file changed

+25
-5
lines changed

Metro/Metro_RP2350_Minesweeper/code.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from displayio import Group, OnDiskBitmap, TileGrid, Bitmap, Palette
1414
from adafruit_display_text.bitmap_label import Label
1515
from adafruit_display_text.text_box import TextBox
16+
import adafruit_usb_host_descriptors
1617
from eventbutton import EventButton
1718
import supervisor
1819
import terminalio
@@ -121,19 +122,29 @@ def update_ui():
121122
# wait a second for USB devices to be ready
122123
time.sleep(1)
123124

125+
#good_devices = False
126+
#while not good_devices:
127+
# for device in usb.core.find(find_all=True):
128+
# if device.manufacturer is not None:
129+
# good_devices = True
130+
# break
124131
# scan for connected USB devices
125132
for device in usb.core.find(find_all=True):
126133
# print information about the found devices
127134
print(f"{device.idVendor:04x}:{device.idProduct:04x}")
128135
print(device.manufacturer, device.product)
129136
print(device.serial_number)
130137

138+
mouse_intfc,mouse_endpt = adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device)
139+
if (mouse_intfc is None or mouse_endpt is None):
140+
continue # Not a mouse device
141+
131142
# assume this device is the mouse
132143
mouse = device
133144

134145
# detach from kernel driver if active
135-
if mouse.is_kernel_driver_active(0):
136-
mouse.detach_kernel_driver(0)
146+
if mouse.is_kernel_driver_active(mouse_intfc):
147+
mouse.detach_kernel_driver(mouse_intfc)
137148

138149
# set the mouse configuration so it can be used
139150
mouse.set_configuration()
@@ -142,8 +153,13 @@ def update_ui():
142153
buf = array.array("b", [0] * 4)
143154
try:
144155
# Try to read some data with a short timeout
145-
data = mouse.read(0x81, buf, timeout=100)
146-
print(f"Mouse test read successful: {data} bytes")
156+
data = mouse.read(mouse_endpt, buf, timeout=100)
157+
print(f"Mouse test read successful: {data} bytes - {buf}")
158+
159+
# without this subsequent reads sometimes can't find the endpoint?
160+
# I've never seen the Flush mouse queue print so it must just need the extra read
161+
if mouse.read(mouse_endpt, buf, timeout=10) > 0:
162+
print(f"Flush mouse queue: {buf}")
147163
break
148164
except usb.core.USBTimeoutError:
149165
# Timeout is normal if mouse isn't moving
@@ -154,6 +170,10 @@ def update_ui():
154170
# Continue to try next device or retry
155171
mouse = None
156172

173+
if mouse is None:
174+
display.root_group = displayio.CIRCUITPYTHON_TERMINAL
175+
raise RuntimeError("No mouse found. Please connect a USB mouse.")
176+
157177
buf = array.array("b", [0] * 4)
158178
waiting_for_release = False
159179
left_button = right_button = False
@@ -271,7 +291,7 @@ def hide_group(group):
271291
try:
272292
# try to read data from the mouse, small timeout so the code will move on
273293
# quickly if there is no data
274-
data_len = mouse.read(0x81, buf, timeout=10)
294+
data_len = mouse.read(mouse_endpt, buf, timeout=10)
275295
left_button = buf[0] & 0x01
276296
right_button = buf[0] & 0x02
277297

0 commit comments

Comments
 (0)