|
33 | 33 | GameOverException, |
34 | 34 | ) |
35 | 35 |
|
36 | | -_MOUSE_SYNC = [] |
37 | | - |
38 | 36 | original_autoreload_val = supervisor.runtime.autoreload |
39 | 37 | supervisor.runtime.autoreload = False |
40 | 38 |
|
|
103 | 101 | # so use it for save data |
104 | 102 | save_to = "/sd/set_game_autosave.dat" |
105 | 103 | except OSError as e: |
106 | | - print(f'no SDcard {e}') |
107 | 104 | # no SDcard |
108 | 105 | pass |
109 | 106 |
|
|
247 | 244 | mouse_bufs = [] |
248 | 245 | # debouncers list for debouncing mouse left clicks |
249 | 246 | mouse_debouncers = [] |
| 247 | +mouse_sync = [] |
250 | 248 |
|
251 | 249 | # scan for connected USB devices |
252 | 250 | for device in usb.core.find(find_all=True): |
253 | 251 | # check if current device is has a boot mouse endpoint |
254 | | - mouse_interface_index, mouse_endpoint_address = ( |
255 | | - adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device) |
256 | | - ) |
257 | | - if mouse_interface_index is not None and mouse_endpoint_address is not None: |
258 | | - # if it does have a boot mouse endpoint then add information to the |
259 | | - # usb info lists |
260 | | - mouse_interface_indexes.append(mouse_interface_index) |
261 | | - mouse_endpoint_addresses.append(mouse_endpoint_address) |
262 | | - |
263 | | - # add the mouse device instance to list |
264 | | - mice.append(device) |
265 | | - print( |
266 | | - f"mouse interface: {mouse_interface_index} " |
267 | | - + f"endpoint_address: {hex(mouse_endpoint_address)}" |
| 252 | + try: |
| 253 | + mouse_interface_index, mouse_endpoint_address = ( |
| 254 | + adafruit_usb_host_descriptors.find_boot_mouse_endpoint(device) |
268 | 255 | ) |
269 | | - _MOUSE_SYNC.append(0) |
| 256 | + if mouse_interface_index is not None and mouse_endpoint_address is not None: |
| 257 | + # if it does have a boot mouse endpoint then add information to the |
| 258 | + # usb info lists |
| 259 | + mouse_interface_indexes.append(mouse_interface_index) |
| 260 | + mouse_endpoint_addresses.append(mouse_endpoint_address) |
| 261 | + |
| 262 | + # add the mouse device instance to list |
| 263 | + mice.append(device) |
| 264 | + print( |
| 265 | + f"mouse interface: {mouse_interface_index} " |
| 266 | + + f"endpoint_address: {hex(mouse_endpoint_address)}" |
| 267 | + ) |
| 268 | + mouse_sync.append(0) |
| 269 | + except usb.core.USBError as e: |
| 270 | + # The mouse might have glitched and may not be detected but at least we don't crash |
| 271 | + print(e) |
270 | 272 |
|
271 | 273 | # detach kernel driver if needed |
272 | 274 | kernel_driver_active_flags.append(device.is_kernel_driver_active(0)) |
@@ -336,28 +338,27 @@ def is_right_mouse_clicked(buf): |
336 | 338 | winner = None |
337 | 339 |
|
338 | 340 |
|
339 | | -def get_mouse_deltas(buffer, read_count, mouse_indx): |
| 341 | +def get_mouse_deltas(buffer, read_count, sync): |
340 | 342 | """ |
341 | 343 | Given a mouse packet buffer and a read count of number of bytes read, |
342 | 344 | return the delta x and y values of the mouse. |
343 | 345 | :param buffer: the buffer containing the packet data |
344 | 346 | :param read_count: the number of bytes read from the mouse |
345 | 347 | :return: tuple containing x and y delta values |
346 | 348 | """ |
347 | | - global _MOUSE_SYNC |
348 | | - if read_count == 4 or (read_count == 8 and _MOUSE_SYNC[mouse_indx] > 50): |
| 349 | + if read_count == 4 or (read_count == 8 and sync > 50): |
349 | 350 | delta_x = buffer[1] |
350 | 351 | delta_y = buffer[2] |
351 | 352 | elif read_count == 8: |
352 | 353 | delta_x = buffer[2] |
353 | 354 | delta_y = buffer[4] |
354 | 355 | if delta_y != 0: |
355 | | - _MOUSE_SYNC[mouse_indx] = -999 |
356 | | - elif delta_y == 0 and _MOUSE_SYNC[mouse_indx] > -1: |
357 | | - _MOUSE_SYNC[mouse_indx] += 1 |
| 356 | + sync = -999 |
| 357 | + elif delta_y == 0 and sync > -1: |
| 358 | + sync += 1 |
358 | 359 | else: |
359 | 360 | raise ValueError(f"Unsupported mouse packet size: {read_count}, must be 4 or 8") |
360 | | - return delta_x, delta_y |
| 361 | + return delta_x, delta_y, sync |
361 | 362 |
|
362 | 363 |
|
363 | 364 | def atexit_callback(): |
@@ -394,7 +395,8 @@ def atexit_callback(): |
394 | 395 | data_len = mouse.read( |
395 | 396 | mouse_endpoint_addresses[i], mouse_bufs[i], timeout=20 |
396 | 397 | ) |
397 | | - mouse_deltas = get_mouse_deltas(mouse_bufs[i], data_len, i) |
| 398 | + mouse_deltas = get_mouse_deltas(mouse_bufs[i], data_len, mouse_sync[i]) |
| 399 | + mouse_sync[i] = mouse_deltas[2] |
398 | 400 | # if we got data, then update the mouse cursor on the display |
399 | 401 | # using min and max to keep it within the bounds of the display |
400 | 402 | mouse_tg.x = max( |
|
0 commit comments