Zion Boggan
repos/Pitch Tracker CV/io_titan/hello_xbox.py
zionboggan.com ↗
51 lines · python
History for this file →
1
"""Hello Xbox - presses A on the Xbox controller via Titan Two.
2
 
3
Requires the bridge.gpc script to be loaded and running on the Titan Two
4
(load once via Gtuner IV, then it runs standalone). Defaults to pressing A
5
for 150 ms.
6
"""
7
from __future__ import annotations
8
 
9
import argparse
10
import sys
11
import time
12
from pathlib import Path
13
 
14
from rich.console import Console
15
 
16
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
17
from io_titan.bridge import TitanBridge, tap
18
 
19
console = Console()
20
 
21
def main() -> int:
22
    ap = argparse.ArgumentParser()
23
    ap.add_argument("--button", default="A", help="Button to tap: A,B,X,Y,LB,RB,LS,RS,UP,DOWN,LEFT,RIGHT,BACK,START")
24
    ap.add_argument("--hold-ms", type=int, default=150)
25
    ap.add_argument("--count", type=int, default=1)
26
    ap.add_argument("--sweep", action="store_true", help="Instead of tapping, sweep the left stick in a circle.")
27
    args = ap.parse_args()
28
 
29
    console.print("[cyan]Opening Titan Two HID...[/cyan]")
30
    with TitanBridge() as t:
31
        console.print("[green]Opened.[/green] Sending to Xbox.")
32
        if args.sweep:
33
            console.print("Sweeping left stick (3 seconds). Watch any stick-driven UI on screen.")
34
            t0 = time.perf_counter()
35
            import math
36
            while time.perf_counter() - t0 < 3.0:
37
                theta = (time.perf_counter() - t0) * 2 * math.pi
38
                t.send(lx=80 * math.cos(theta), ly=80 * math.sin(theta))
39
                time.sleep(0.016)
40
            t.send()
41
            console.print("[green]Sweep complete.[/green]")
42
        else:
43
            for i in range(args.count):
44
                console.print(f"  tap {args.button} ({i+1}/{args.count})")
45
                tap(t, args.button, args.hold_ms / 1000.0)
46
                time.sleep(0.25)
47
        console.print("[green]Done. Disarming and releasing device.[/green]")
48
    return 0
49
 
50
if __name__ == "__main__":
51
    sys.exit(main())