|
33 | 33 | GameOverException, |
34 | 34 | ) |
35 | 35 |
|
| 36 | +_MOUSE_SYNC = [] |
| 37 | + |
36 | 38 | original_autoreload_val = supervisor.runtime.autoreload |
37 | 39 | supervisor.runtime.autoreload = False |
38 | 40 |
|
|
100 | 102 | # if we made it to here then /sd/ exists and has a card |
101 | 103 | # so use it for save data |
102 | 104 | save_to = "/sd/set_game_autosave.dat" |
103 | | - except OSError: |
| 105 | + except OSError as e: |
| 106 | + print(f'no SDcard {e}') |
104 | 107 | # no SDcard |
105 | 108 | pass |
106 | 109 |
|
|
263 | 266 | f"mouse interface: {mouse_interface_index} " |
264 | 267 | + f"endpoint_address: {hex(mouse_endpoint_address)}" |
265 | 268 | ) |
| 269 | + _MOUSE_SYNC.append(0) |
266 | 270 |
|
267 | 271 | # detach kernel driver if needed |
268 | 272 | kernel_driver_active_flags.append(device.is_kernel_driver_active(0)) |
@@ -332,20 +336,25 @@ def is_right_mouse_clicked(buf): |
332 | 336 | winner = None |
333 | 337 |
|
334 | 338 |
|
335 | | -def get_mouse_deltas(buffer, read_count): |
| 339 | +def get_mouse_deltas(buffer, read_count, mouse_indx): |
336 | 340 | """ |
337 | 341 | Given a mouse packet buffer and a read count of number of bytes read, |
338 | 342 | return the delta x and y values of the mouse. |
339 | 343 | :param buffer: the buffer containing the packet data |
340 | 344 | :param read_count: the number of bytes read from the mouse |
341 | 345 | :return: tuple containing x and y delta values |
342 | 346 | """ |
343 | | - if read_count == 4: |
| 347 | + global _MOUSE_SYNC |
| 348 | + if read_count == 4 or (read_count == 8 and _MOUSE_SYNC[mouse_indx] > 50): |
344 | 349 | delta_x = buffer[1] |
345 | 350 | delta_y = buffer[2] |
346 | 351 | elif read_count == 8: |
347 | 352 | delta_x = buffer[2] |
348 | 353 | delta_y = buffer[4] |
| 354 | + 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 |
349 | 358 | else: |
350 | 359 | raise ValueError(f"Unsupported mouse packet size: {read_count}, must be 4 or 8") |
351 | 360 | return delta_x, delta_y |
@@ -381,30 +390,36 @@ def atexit_callback(): |
381 | 390 | try: |
382 | 391 | # read data from the mouse, small timeout so we move on |
383 | 392 | # quickly if there is no data |
| 393 | + data_len = 0 |
384 | 394 | data_len = mouse.read( |
385 | | - mouse_endpoint_addresses[i], mouse_bufs[i], timeout=10 |
386 | | - ) |
387 | | - mouse_deltas = get_mouse_deltas(mouse_bufs[i], data_len) |
388 | | - # if we got data, then update the mouse cursor on the display |
389 | | - # using min and max to keep it within the bounds of the display |
390 | | - mouse_tg.x = max( |
391 | | - 0, |
392 | | - min( |
393 | | - display.width // scale_factor - 1, mouse_tg.x + mouse_deltas[0] // 2 |
394 | | - ), |
395 | | - ) |
396 | | - mouse_tg.y = max( |
397 | | - 0, |
398 | | - min( |
399 | | - display.height // scale_factor - 1, |
400 | | - mouse_tg.y + mouse_deltas[1] // 2, |
401 | | - ), |
| 395 | + mouse_endpoint_addresses[i], mouse_bufs[i], timeout=20 |
402 | 396 | ) |
| 397 | + if data_len > 0: |
| 398 | + mouse_deltas = get_mouse_deltas(mouse_bufs[i], data_len, i) |
| 399 | + # if we got data, then update the mouse cursor on the display |
| 400 | + # using min and max to keep it within the bounds of the display |
| 401 | + mouse_tg.x = max( |
| 402 | + 0, |
| 403 | + min( |
| 404 | + display.width // scale_factor - 1, mouse_tg.x + mouse_deltas[0] // 2 |
| 405 | + ), |
| 406 | + ) |
| 407 | + mouse_tg.y = max( |
| 408 | + 0, |
| 409 | + min( |
| 410 | + display.height // scale_factor - 1, |
| 411 | + mouse_tg.y + mouse_deltas[1] // 2, |
| 412 | + ), |
| 413 | + ) |
403 | 414 |
|
404 | 415 | # timeout error is raised if no data was read within the allotted timeout |
405 | 416 | except usb.core.USBTimeoutError: |
406 | 417 | pass |
407 | 418 |
|
| 419 | + # common non-fatal error |
| 420 | + except usb.core.USBError: |
| 421 | + pass |
| 422 | + |
408 | 423 | # update the mouse debouncers |
409 | 424 | mouse_debouncers[i].update() |
410 | 425 |
|
|
0 commit comments