-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystickInput.py
78 lines (58 loc) · 1.69 KB
/
joystickInput.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
import inputs
EVENT_ABB = (
('ABS_X', 'LSX'),
('ABS_Y', 'LSY'),
('ABS_RX', 'RSX'),
('ABS_RY', 'RSY'),
('BTN_START', 'START'),
('BTN_SELECT', 'SELECT'),
('BTN_NORTH', 'Y'),
('BTN_EAST', 'B'),
('BTN_SOUTH', 'A'),
('BTN_WEST', 'X'),
('ABS_HAT0X', 'HX'),
('ABS_HAT0Y', 'HY'),
('BTN_THUMBL', 'LS'),
('BTN_THUMBR', 'RS'),
('BTN_TL', 'LB'),
('BTN_TR', 'RB'),
('ABS_Z', 'LT'),
('ABS_RZ', 'RT')
)
class XBoxController(object):
def __init__(self, gamepad=None, abbrevs=EVENT_ABB):
self.states = {}
self.abbrevs = dict(abbrevs)
for _, value in self.abbrevs.items():
self.states[value] = 0
self.gamepad = gamepad
if not gamepad:
self._get_gamepad()
def _get_gamepad(self):
try:
self.gamepad = inputs.devices.gamepads[0]
except IndexError:
raise inputs.UnpluggedError("No gamepad found.")
def process_event(self, event):
if event.ev_type == 'Absolute' or event.ev_type == 'Key':
abbv = self.abbrevs[event.code]
self.states[abbv] = event.state
# self.output_state()
def output_state(self):
output_string = ""
for key, value in self.states.items():
output_string += key + ':' + str(value) + ' '
print(output_string)
def process_events(self):
try:
events = self.gamepad.read()
except EOFError:
events = []
for event in events:
self.process_event(event)
def main():
controller = XBoxController()
while True:
controller.process_events()
if __name__ == "__main__":
main()