Make example work and redo some library code
This commit is contained in:
parent
abdda5a7c4
commit
1cb7075180
4 changed files with 172 additions and 41 deletions
58
example_switch_macro.py
Normal file
58
example_switch_macro.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# example_switch_macro.py
|
||||
import time
|
||||
from switch_pico_uart import SwitchUARTClient, SwitchButton, SwitchDpad
|
||||
|
||||
PORT = "COM5" # change to your serial port, e.g. /dev/cu.usbserial-0001
|
||||
SEND_INTERVAL = 1 / 500 # optional: match controller_uart_bridge default
|
||||
|
||||
# Convenience list of every SwitchButton (plus DPAD directions).
|
||||
ALL_BUTTONS = [
|
||||
SwitchButton.A,
|
||||
SwitchButton.B,
|
||||
SwitchButton.X,
|
||||
SwitchButton.Y,
|
||||
SwitchButton.L,
|
||||
SwitchButton.R,
|
||||
SwitchButton.ZL,
|
||||
SwitchButton.ZR,
|
||||
SwitchButton.PLUS,
|
||||
SwitchButton.MINUS,
|
||||
SwitchButton.CAPTURE,
|
||||
SwitchButton.LCLICK,
|
||||
SwitchButton.RCLICK,
|
||||
SwitchDpad.DOWN,
|
||||
SwitchDpad.UP,
|
||||
SwitchDpad.LEFT,
|
||||
SwitchDpad.RIGHT,
|
||||
]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
# auto_send keeps the current state flowing in the background, so we don't
|
||||
# need to manually pump frames to the Pico.
|
||||
with SwitchUARTClient(PORT, send_interval=SEND_INTERVAL, auto_send=True) as client:
|
||||
client.neutral()
|
||||
|
||||
# Press every button/DPAD direction one-by-one, holding each briefly.
|
||||
for button in ALL_BUTTONS:
|
||||
client.press_for(0.10, button)
|
||||
time.sleep(0.05) # short gap between presses
|
||||
|
||||
# Push left stick up briefly.
|
||||
client.move_left_stick_for(0.0, -1.0, 0.2)
|
||||
|
||||
# Hold dpad right for one second using hat-friendly press/release.
|
||||
client.press_for(0.5, SwitchDpad.RIGHT)
|
||||
|
||||
# Listen for rumble frames for a few seconds while the background sender runs.
|
||||
end = time.monotonic() + 3
|
||||
while time.monotonic() < end:
|
||||
rumble = client.poll_rumble()
|
||||
if rumble:
|
||||
left, right = rumble
|
||||
print(f"Rumble: L={left:.2f} R={right:.2f}")
|
||||
time.sleep(0.01)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue