Skip to content

Commit b7f5684

Browse files
committed
Fruitjam Match3: Improve mouse compatibility
1 parent 59524df commit b7f5684

File tree

1 file changed

+35
-20
lines changed
  • Metro/Metro_RP2350_Match3/match3_game

1 file changed

+35
-20
lines changed

Metro/Metro_RP2350_Match3/match3_game/code.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
GameOverException,
3434
)
3535

36+
_MOUSE_SYNC = []
37+
3638
original_autoreload_val = supervisor.runtime.autoreload
3739
supervisor.runtime.autoreload = False
3840

@@ -100,7 +102,8 @@
100102
# if we made it to here then /sd/ exists and has a card
101103
# so use it for save data
102104
save_to = "/sd/set_game_autosave.dat"
103-
except OSError:
105+
except OSError as e:
106+
print(f'no SDcard {e}')
104107
# no SDcard
105108
pass
106109

@@ -263,6 +266,7 @@
263266
f"mouse interface: {mouse_interface_index} "
264267
+ f"endpoint_address: {hex(mouse_endpoint_address)}"
265268
)
269+
_MOUSE_SYNC.append(0)
266270

267271
# detach kernel driver if needed
268272
kernel_driver_active_flags.append(device.is_kernel_driver_active(0))
@@ -332,20 +336,25 @@ def is_right_mouse_clicked(buf):
332336
winner = None
333337

334338

335-
def get_mouse_deltas(buffer, read_count):
339+
def get_mouse_deltas(buffer, read_count, mouse_indx):
336340
"""
337341
Given a mouse packet buffer and a read count of number of bytes read,
338342
return the delta x and y values of the mouse.
339343
:param buffer: the buffer containing the packet data
340344
:param read_count: the number of bytes read from the mouse
341345
:return: tuple containing x and y delta values
342346
"""
343-
if read_count == 4:
347+
global _MOUSE_SYNC
348+
if read_count == 4 or (read_count == 8 and _MOUSE_SYNC[mouse_indx] > 50):
344349
delta_x = buffer[1]
345350
delta_y = buffer[2]
346351
elif read_count == 8:
347352
delta_x = buffer[2]
348353
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
349358
else:
350359
raise ValueError(f"Unsupported mouse packet size: {read_count}, must be 4 or 8")
351360
return delta_x, delta_y
@@ -381,30 +390,36 @@ def atexit_callback():
381390
try:
382391
# read data from the mouse, small timeout so we move on
383392
# quickly if there is no data
393+
data_len = 0
384394
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
402396
)
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+
)
403414

404415
# timeout error is raised if no data was read within the allotted timeout
405416
except usb.core.USBTimeoutError:
406417
pass
407418

419+
# common non-fatal error
420+
except usb.core.USBError:
421+
pass
422+
408423
# update the mouse debouncers
409424
mouse_debouncers[i].update()
410425

0 commit comments

Comments
 (0)