|
13 | 13 | from displayio import Group, OnDiskBitmap, TileGrid, Bitmap, Palette
|
14 | 14 | from adafruit_display_text.bitmap_label import Label
|
15 | 15 | from adafruit_display_text.text_box import TextBox
|
| 16 | +import adafruit_usb_host_descriptors |
16 | 17 | from eventbutton import EventButton
|
17 | 18 | import supervisor
|
18 | 19 | import terminalio
|
@@ -121,23 +122,53 @@ def update_ui():
|
121 | 122 | # wait a second for USB devices to be ready
|
122 | 123 | time.sleep(1)
|
123 | 124 |
|
| 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 |
124 | 132 | # scan for connected USB devices
|
125 | 133 | for device in usb.core.find(find_all=True):
|
126 | 134 | # print information about the found devices
|
127 | 135 | print(f"{device.idVendor:04x}:{device.idProduct:04x}")
|
128 | 136 | print(device.manufacturer, device.product)
|
129 | 137 | print(device.serial_number)
|
130 | 138 |
|
| 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 | + |
131 | 143 | # assume this device is the mouse
|
132 | 144 | mouse = device
|
133 | 145 |
|
134 | 146 | # 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) |
137 | 149 |
|
138 | 150 | # set the mouse configuration so it can be used
|
139 | 151 | mouse.set_configuration()
|
140 | 152 |
|
| 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 | + |
141 | 172 | buf = array.array("b", [0] * 4)
|
142 | 173 | waiting_for_release = False
|
143 | 174 | left_button = right_button = False
|
@@ -255,7 +286,19 @@ def hide_group(group):
|
255 | 286 | try:
|
256 | 287 | # try to read data from the mouse, small timeout so the code will move on
|
257 | 288 | # 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 | + |
259 | 302 | left_button = buf[0] & 0x01
|
260 | 303 | right_button = buf[0] & 0x02
|
261 | 304 |
|
|
0 commit comments