-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsample.py
48 lines (43 loc) · 1.29 KB
/
sample.py
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
from __future__ import print_function
import xbox
# Format floating point number to string format -x.xxx
def fmtFloat(n):
return '{:6.3f}'.format(n)
# Print one or more values without a line feed
def show(*args):
for arg in args:
print(arg, end="")
# Print true or false value based on a boolean, without linefeed
def showIf(boolean, ifTrue, ifFalse=" "):
if boolean:
show(ifTrue)
else:
show(ifFalse)
# Instantiate the controller
joy = xbox.Joystick()
# Show various axis and button states until Back button is pressed
print("Xbox controller sample: Press Back button to exit")
while not joy.Back():
# Show connection status
show("Connected:")
showIf(joy.connected(), "Y", "N")
# Left analog stick
show(" Left X/Y:", fmtFloat(joy.leftX()), "/", fmtFloat(joy.leftY()))
# Right trigger
show(" RightTrg:", fmtFloat(joy.rightTrigger()))
# A/B/X/Y buttons
show(" Buttons:")
showIf(joy.A(), "A")
showIf(joy.B(), "B")
showIf(joy.X(), "X")
showIf(joy.Y(), "Y")
# Dpad U/D/L/R
show(" Dpad:")
showIf(joy.dpadUp(), "U")
showIf(joy.dpadDown(), "D")
showIf(joy.dpadLeft(), "L")
showIf(joy.dpadRight(), "R")
# Move cursor back to start of line
show(chr(13))
# Close out when done
joy.close()