-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbox.py
196 lines (149 loc) · 4.94 KB
/
xbox.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from wpilib import Joystick, Timer
class XboxController(object):
"""
Allows usage of an Xbox controller, with sensible names for xbox
specific buttons and axes.
"""
def __init__(self, port):
"""
:param port: The port on the driver station that the controller is
plugged into.
:type port: int
"""
self.joy = Joystick(port)
self.debounce = DpadDebouncer()
def getLeftX(self):
"""Get the left stick X axis
:returns: -1 to 1
:rtype: float
"""
return self.joy.getRawAxis(0)
def getLeftY(self):
"""Get the left stick Y axis
:returns: -1 to 1
:rtype: float
"""
return self.joy.getRawAxis(1)
#getX = self.getLeftX()
#getY = self.getLeftY()
def getLeftPressed(self):
"""Determines if the left stick is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(9)
def getPOV(self):
"""Get the state of the D-Pad
:returns: The angle of the D-Pad in degrees, or -1 if the D-Pad is not pressed.
:rtype: float
"""
return self.debounce.get(self.joy.getPOV())
def getRightX(self):
"""Get the right stick X axis
:returns: -1 to 1
:rtype: float
"""
return self.joy.getRawAxis(4)
def getRightY(self):
"""Get the right stick Y axis
:returns: -1 to 1
:rtype: float
"""
return self.joy.getRawAxis(5)
def getRightPressed(self):
"""Determines if the right stick is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(10)
def getButtonA(self):
"""Gets whether the A button is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(1)
def getButtonB(self):
"""Gets whether the B button is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(2)
def getButtonX(self):
"""Gets whether the X button is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(3)
def getButtonY(self):
"""Gets whether the X button is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(4)
def getStart(self):
"""Gets whether the Start button is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(8)
def getBack(self):
"""Gets whether the Back button is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(7)
def getLeftBumper(self):
"""Gets whether the left bumper is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(5)
def getRightBumper(self):
"""Gets whether the right bumper is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawButton(6)
def getLeftTrigger(self):
"""Gets whether the left trigger is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawAxis(2) > 0
def getRightTrigger(self):
"""Gets whether the right trigger is pressed
:returns: True if pressed, False otherwise
:rtype: bool
"""
return self.joy.getRawAxis(3) > 0
def getTriggers(self):
return self.joy.getRawAxis(2)
def getRightTriggerRaw(self):
return self.joy.getRawAxis(3)
def getLeftTriggerRaw(self):
return self.joy.getRawAxis(2)
def rumble(self, left=None, right=None):
"""Sets the rumble amount on one/both side(s) of the controller"""
if left is not None:
self.joy.setRumble(Joystick.RumbleType.kLeftRumble_val, left)
if right is not None:
self.joy.setRumble(Joystick.RumbleType.kRightRumble_val, right)
class DpadDebouncer(object):
def __init__(self):
self.debounce_period = 0.5
self.last_input = -1
self.last_timestamp = Timer.getFPGATimestamp()
def set_debounce_period(self, time):
self.debounce_period = time
def get(self, input):
if input == self.last_input:
time = Timer.getFPGATimestamp()
if time - self.last_timestamp <= self.debounce_period:
return -1
else:
self.last_timestamp = time
return input
else:
self.last_input = input
self.last_timestamp = Timer.getFPGATimestamp()
return input