-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgamepad.py
More file actions
87 lines (70 loc) · 1.95 KB
/
Copy pathgamepad.py
File metadata and controls
87 lines (70 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# "wigglystuff",
# ]
# ///
import marimo
__generated_with = "0.18.1"
app = marimo.App(width="columns")
with app.setup:
import marimo as mo
from wigglystuff import GamepadWidget
@app.cell
def _():
pad = mo.ui.anywidget(GamepadWidget())
return (pad,)
@app.cell
def _():
mo.md(r"""
## Listen to browser gamepad events
This widget streams button presses, d-pad status, and analog stick axes.
Plug in a controller (or pair a Bluetooth one), press any button, and you should see data below.
""")
return
@app.cell
def _(pad):
pad
return
@app.cell
def _(pad):
axes = pad.axes if pad.axes else [0.0, 0.0, 0.0, 0.0]
left_axes = tuple(round(val, 2) for val in axes[:2])
right_axes = tuple(round(val, 2) for val in axes[2:])
dpad_directions = [
symbol
for flag, symbol in [
(pad.dpad_up, "↑"),
(pad.dpad_down, "↓"),
(pad.dpad_left, "←"),
(pad.dpad_right, "→"),
]
if flag
]
last_button = pad.current_button_press if pad.current_button_press >= 0 else "—"
current_ts = pad.current_timestamp or "—"
previous_ts = pad.previous_timestamp or "—"
mo.vstack(
[
mo.md(
f"**Last button:** `{last_button}` | "
f"**Last change (ms):** `{current_ts}` | "
f"**Previous:** `{previous_ts}`"
),
mo.md(
f"**Sticks** Left: `{left_axes}` Right: `{right_axes}`"
),
mo.md(
"**D-pad:** "
+ (
" ".join(dpad_directions)
if dpad_directions
else "`—` (tap the arrows)"
)
),
]
)
return
if __name__ == "__main__":
app.run()