Skip to content

Commit 4aef934

Browse files
authored
Merge pull request #3111 from RetiredWizard/fruitjamminemouse
Add check to skip over keyboard
2 parents c810b4e + 8feb030 commit 4aef934

File tree

1 file changed

+46
-3
lines changed
  • Metro/Metro_RP2350_Minesweeper

1 file changed

+46
-3
lines changed

Metro/Metro_RP2350_Minesweeper/code.py

100755100644
Lines changed: 46 additions & 3 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,23 +122,53 @@ def update_ui():
121122
# wait a second for USB devices to be ready
122123
time.sleep(1)
123124

125+
good_devices = False
126+
wait_time = time.monotonic() + 10 # wait up to 20 seconds for a good device to be found
127+
while not good_devices and time.monotonic() < wait_time:
128+
for device in usb.core.find(find_all=True):
129+
if device.manufacturer is not None:
130+
good_devices = True
131+
break
124132
# scan for connected USB devices
125133
for device in usb.core.find(find_all=True):
126134
# print information about the found devices
127135
print(f"{device.idVendor:04x}:{device.idProduct:04x}")
128136
print(device.manufacturer, device.product)
129137
print(device.serial_number)
130138

139+
mouse_intfc,mouse_endpt = adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device)
140+
if (mouse_intfc is None or mouse_endpt is None):
141+
continue # Not a mouse device
142+
131143
# assume this device is the mouse
132144
mouse = device
133145

134146
# detach from kernel driver if active
135-
if mouse.is_kernel_driver_active(0):
136-
mouse.detach_kernel_driver(0)
147+
if mouse.is_kernel_driver_active(mouse_intfc):
148+
mouse.detach_kernel_driver(mouse_intfc)
137149

138150
# set the mouse configuration so it can be used
139151
mouse.set_configuration()
140152

153+
# Verify mouse works by reading from it
154+
buf = array.array("b", [0] * 4)
155+
try:
156+
# Try to read some data with a short timeout
157+
data = mouse.read(mouse_endpt, buf, timeout=100)
158+
print(f"Mouse test read successful: {data} bytes - {buf}")
159+
break
160+
except usb.core.USBTimeoutError:
161+
# Timeout is normal if mouse isn't moving
162+
print("Mouse connected but not sending data (normal)")
163+
break
164+
except Exception as e: # pylint: disable=broad-except
165+
print(f"Mouse test read failed: {e}")
166+
# Continue to try next device or retry
167+
mouse = None
168+
169+
if mouse is None:
170+
raise RuntimeError("No mouse found. Please connect a USB mouse.")
171+
141172
buf = array.array("b", [0] * 4)
142173
waiting_for_release = False
143174
left_button = right_button = False
@@ -255,7 +286,19 @@ def hide_group(group):
255286
try:
256287
# try to read data from the mouse, small timeout so the code will move on
257288
# quickly if there is no data
258-
data_len = mouse.read(0x81, buf, timeout=10)
289+
while True:
290+
try:
291+
# read data from the mouse endpoint
292+
data_len = mouse.read(mouse_endpt, buf, timeout=10)
293+
if data_len > 0:
294+
break
295+
except usb.core.USBTimeoutError:
296+
# if we get a timeout error, it means there is no data available
297+
pass
298+
except usb.core.USBError as exc:
299+
# if we get a USBError, We may be getting no endpoint msgs which can be waited out
300+
pass
301+
259302
left_button = buf[0] & 0x01
260303
right_button = buf[0] & 0x02
261304

0 commit comments

Comments
 (0)