-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamepad.py
More file actions
241 lines (208 loc) · 9.27 KB
/
Copy pathGamepad.py
File metadata and controls
241 lines (208 loc) · 9.27 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Released by rdb under the Unlicense (unlicense.org)
# Based on information from:
# https://www.kernel.org/doc/Documentation/input/joystick-api.txt
# Modified to work as a joystick control driver (MrTorstensson)
import os, struct, array
from fcntl import ioctl
from time import sleep
from _thread import *
class Xbox_One:
""" Xbox One controller driver that will set up all buttons and Throttles """
""" and keep latest value for all of them in a callable dictionary """
def __init__(self, JS = "/dev/input/js0"):
""" JS = Gamepad device location in Kernel """
# We'll store the states here.
self.Key_states = {}
self.axis_map = []
self.button_map = []
# These constants were borrowed from linux/input.h and modified for the names on the Xbox One control
axis_names = {
0x00 : 'lsx', #Left Stick X
0x01 : 'lsy', #Left Stick Y
0x02 : 'lt', #Left Throttle
0x03 : 'rsx', #Right Stick X
0x04 : 'rsy', #Right Stick Y
0x05 : 'rt', #Right Throttle
0x10 : 'dpx', # Directional Pad X
0x11 : 'dpy', # Directional Pad X
}
button_names = {
0x130 : 'a', #A button
0x131 : 'b', #B button
0x133 : 'x', #X button
0x134 : 'y', #Y button
0x136 : 'lb', #Left Bump
0x137 : 'rb', #Right Bump
0x13a : 'View', #View Button
0x13b : 'Menu', #Menu Button
0x13c : 'Xbox', #Xbox Tag button
0x13d : 'lsp', #Left Stick Press
0x13e : 'rsp', #Right Stick Press
}
# Check if joystick device exists
if (not os.path.exists(JS)):
print('Game controller not found (%s), is it connected?' % JS)
exit()
# Open the joystick device.
print('Opening %s...' % JS)
self.jsdev = open(JS, 'rb')
# Get the device name.
buf = array.array('B', [0] * 64)
ioctl(self.jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
js_name = buf.tostring()
print('Device name: %s' % js_name)
# Get number of axes and buttons.
buf = array.array('B', [0])
ioctl(self.jsdev, 0x80016a11, buf) # JSIOCGAXES
num_axes = buf[0]
buf = array.array('B', [0])
ioctl(self.jsdev, 0x80016a12, buf) # JSIOCGBUTTONS
num_buttons = buf[0]
# Get the axis map.
buf = array.array('B', [0] * 0x40)
ioctl(self.jsdev, 0x80406a32, buf) # JSIOCGAXMAP
for axis in buf[:num_axes]:
axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
self.axis_map.append(axis_name)
self.Key_states[axis_name] = 0
# Get the button map.
buf = array.array('H', [0] * 200)
ioctl(self.jsdev, 0x80406a34, buf) # JSIOCGBTNMAP
for btn in buf[:num_buttons]:
btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
self.button_map.append(btn_name)
self.Key_states[btn_name] = 0
print('%d axes found: %s' % (num_axes, ', '.join(self.axis_map)))
print('%d buttons found: %s' % (num_buttons, ', '.join(self.button_map)))
self.Running = True
start_new_thread(self.UpdateThread,())
def UpdateThread(self):
# Main event loop
while self.Running:
evbuf = self.jsdev.read(8)
if evbuf:
time, value, type, number = struct.unpack('IhBB', evbuf)
if type & 0x80:
True
# If Button
elif type & 0x01:
# Xbox button will disconnect
if self.button_map[number] == 'Xbox':
print('%s:%s' %('Disconnect', 'Exit'))
self.Running = False
break
self.Key_states[self.button_map[number]] = value
#print('%s:%s' %(self.button_map[number], self.Key_states[self.button_map[number]]))
#If Throttle
elif type & 0x02:
if self.axis_map[number] in ['lt', 'rt']:
self.Key_states[self.axis_map[number]] = int((value+32767)/32767*100/2)
else:
self.Key_states[self.axis_map[number]] = int((value)/32767*100)
#print('%s:%s' %(self.axis_map[number], self.Key_states[self.axis_map[number]]))
print("Xbox_One update stopped, Reinitialize controller to start update")
exit()
def close(self):
self.Running = False
self.jsdev.close()
class Xbox_360:
""" Xbox One controller driver that will set up all buttons and Throttles """
""" and keep latest value for all of them in a callable dictionary """
def __init__(self, JS = "/dev/input/js0"):
""" JS = Gamepad device location in Kernel """
# We'll store the states here.
self.Key_states = {}
self.axis_map = []
self.button_map = []
# These constants were borrowed from linux/input.h and modified for the names on the Xbox One control
axis_names = {
0x00 : 'lsx', #Left Stick X
0x01 : 'lsy', #Left Stick Y
0x02 : 'lt', #Left Throttle
0x03 : 'rsx', #Right Stick X
0x04 : 'rsy', #Right Stick Y
0x05 : 'rt', #Right Throttle
}
button_names = {
0x130 : 'a', #A button
0x131 : 'b', #B button
0x133 : 'x', #X button
0x134 : 'y', #Y button
0x136 : 'lb', #Left Bump
0x137 : 'rb', #Right Bump
0x13a : 'View', #View Button
0x13b : 'Menu', #Menu Button
0x13c : 'Xbox', #Xbox Tag button
0x13d : 'lsp', #Left Stick Press
0x13e : 'rsp', #Right Stick Press
0x2c0 : 'dpad_left', #Direction Pad left
0x2c1 : 'dpad_right', #Direction Pad right
0x2c2 : 'dpad_up', #Direction Pad up
0x2c3 : 'dpad_down', #Direction Pad down
}
# Check if joystick device exists
if (not os.path.exists(JS)):
print('Game controller not found (%s), is it connected?' % JS)
exit()
# Open the joystick device.
print('Opening %s...' % JS)
self.jsdev = open(JS, 'rb')
# Get the device name.
buf = array.array('B', [0] * 64)
ioctl(self.jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
js_name = buf.tostring()
print('Device name: %s' % js_name)
# Get number of axes and buttons.
buf = array.array('B', [0])
ioctl(self.jsdev, 0x80016a11, buf) # JSIOCGAXES
num_axes = buf[0]
buf = array.array('B', [0])
ioctl(self.jsdev, 0x80016a12, buf) # JSIOCGBUTTONS
num_buttons = buf[0]
# Get the axis map.
buf = array.array('B', [0] * 0x40)
ioctl(self.jsdev, 0x80406a32, buf) # JSIOCGAXMAP
for axis in buf[:num_axes]:
axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
self.axis_map.append(axis_name)
self.Key_states[axis_name] = 0
# Get the button map.
buf = array.array('H', [0] * 200)
ioctl(self.jsdev, 0x80406a34, buf) # JSIOCGBTNMAP
for btn in buf[:num_buttons]:
btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
self.button_map.append(btn_name)
self.Key_states[btn_name] = 0
print('%d axes found: %s' % (num_axes, ', '.join(self.axis_map)))
print('%d buttons found: %s' % (num_buttons, ', '.join(self.button_map)))
self.Running = True
start_new_thread(self.UpdateThread,())
def UpdateThread(self):
# Main event loop
while self.Running:
evbuf = self.jsdev.read(8)
if evbuf:
time, value, type, number = struct.unpack('IhBB', evbuf)
if type & 0x80:
True
# If Button
elif type & 0x01:
# Xbox button will disconnect
if self.button_map[number] == 'Xbox':
print('%s:%s' %('Disconnect', 'Exit'))
self.Running = False
break
self.Key_states[self.button_map[number]] = value
#print('%s:%s' %(self.button_map[number], self.Key_states[self.button_map[number]]))
#If Throttle
elif type & 0x02:
if self.axis_map[number] in ['lt', 'rt']:
self.Key_states[self.axis_map[number]] = int((value+32767)/32767*100/2)
else:
self.Key_states[self.axis_map[number]] = int((value)/32767*100)
#print('%s:%s' %(self.axis_map[number], self.Key_states[self.axis_map[number]]))
print("Xbox_One update stopped, Reinitialize controller to start update")
exit()
def close(self):
self.Running = False
self.jsdev.close()