Merge pull request #3111 from RetiredWizard/fruitjamminemouse

Add check to skip over keyboard
This commit is contained in:
foamyguy 2025-08-20 15:05:51 -05:00 committed by GitHub
commit 4aef934371
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

49
Metro/Metro_RP2350_Minesweeper/code.py Executable file → Normal file
View file

@ -13,6 +13,7 @@ import time
from displayio import Group, OnDiskBitmap, TileGrid, Bitmap, Palette from displayio import Group, OnDiskBitmap, TileGrid, Bitmap, Palette
from adafruit_display_text.bitmap_label import Label from adafruit_display_text.bitmap_label import Label
from adafruit_display_text.text_box import TextBox from adafruit_display_text.text_box import TextBox
import adafruit_usb_host_descriptors
from eventbutton import EventButton from eventbutton import EventButton
import supervisor import supervisor
import terminalio import terminalio
@ -121,6 +122,13 @@ mouse = None
# wait a second for USB devices to be ready # wait a second for USB devices to be ready
time.sleep(1) time.sleep(1)
good_devices = False
wait_time = time.monotonic() + 10 # wait up to 20 seconds for a good device to be found
while not good_devices and time.monotonic() < wait_time:
for device in usb.core.find(find_all=True):
if device.manufacturer is not None:
good_devices = True
break
# scan for connected USB devices # scan for connected USB devices
for device in usb.core.find(find_all=True): for device in usb.core.find(find_all=True):
# print information about the found devices # print information about the found devices
@ -128,16 +136,39 @@ for device in usb.core.find(find_all=True):
print(device.manufacturer, device.product) print(device.manufacturer, device.product)
print(device.serial_number) print(device.serial_number)
mouse_intfc,mouse_endpt = adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device)
if (mouse_intfc is None or mouse_endpt is None):
continue # Not a mouse device
# assume this device is the mouse # assume this device is the mouse
mouse = device mouse = device
# detach from kernel driver if active # detach from kernel driver if active
if mouse.is_kernel_driver_active(0): if mouse.is_kernel_driver_active(mouse_intfc):
mouse.detach_kernel_driver(0) mouse.detach_kernel_driver(mouse_intfc)
# set the mouse configuration so it can be used # set the mouse configuration so it can be used
mouse.set_configuration() mouse.set_configuration()
# Verify mouse works by reading from it
buf = array.array("b", [0] * 4)
try:
# Try to read some data with a short timeout
data = mouse.read(mouse_endpt, buf, timeout=100)
print(f"Mouse test read successful: {data} bytes - {buf}")
break
except usb.core.USBTimeoutError:
# Timeout is normal if mouse isn't moving
print("Mouse connected but not sending data (normal)")
break
except Exception as e: # pylint: disable=broad-except
print(f"Mouse test read failed: {e}")
# Continue to try next device or retry
mouse = None
if mouse is None:
raise RuntimeError("No mouse found. Please connect a USB mouse.")
buf = array.array("b", [0] * 4) buf = array.array("b", [0] * 4)
waiting_for_release = False waiting_for_release = False
left_button = right_button = False left_button = right_button = False
@ -255,7 +286,19 @@ while True:
try: try:
# try to read data from the mouse, small timeout so the code will move on # try to read data from the mouse, small timeout so the code will move on
# quickly if there is no data # quickly if there is no data
data_len = mouse.read(0x81, buf, timeout=10) while True:
try:
# read data from the mouse endpoint
data_len = mouse.read(mouse_endpt, buf, timeout=10)
if data_len > 0:
break
except usb.core.USBTimeoutError:
# if we get a timeout error, it means there is no data available
pass
except usb.core.USBError as exc:
# if we get a USBError, We may be getting no endpoint msgs which can be waited out
pass
left_button = buf[0] & 0x01 left_button = buf[0] & 0x01
right_button = buf[0] & 0x02 right_button = buf[0] & 0x02