-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepadthread.py
282 lines (238 loc) · 7.51 KB
/
gamepadthread.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import sys
from PyQt5.QtCore import pyqtSlot, QThread, pyqtSignal, QObject
import os, struct, array
from fcntl import ioctl
# These constants were borrowed from linux/input.h
axis_names = {
0x00: 'x',
0x01: 'y',
0x02: 'z',
0x03: 'rx',
0x04: 'ry',
0x05: 'rz',
0x06: 'trottle',
0x07: 'rudder',
0x08: 'wheel',
0x09: 'gas',
0x0a: 'brake',
0x10: 'hat0x',
0x11: 'hat0y',
0x12: 'hat1x',
0x13: 'hat1y',
0x14: 'hat2x',
0x15: 'hat2y',
0x16: 'hat3x',
0x17: 'hat3y',
0x18: 'pressure',
0x19: 'distance',
0x1a: 'tilt_x',
0x1b: 'tilt_y',
0x1c: 'tool_width',
0x20: 'volume',
0x28: 'misc',
}
button_names = {
0x120: 'X',
0x121: 'A',
0x122: 'B',
0x123: 'Y',
0x124: 'LB',
0x125: 'RB',
0x126: 'LT',
0x127: 'RT',
0x128: 'BACK',
0x129: 'START',
0x12a: 'base5',
0x12b: 'base6',
0x12f: 'dead',
0x130: 'a',
0x131: 'b',
0x132: 'c',
0x133: 'x',
0x134: 'y',
0x135: 'z',
0x136: 'tl',
0x137: 'tr',
0x138: 'tl2',
0x139: 'tr2',
0x13a: 'select',
0x13b: 'start',
0x13c: 'mode',
0x13d: 'thumbl',
0x13e: 'thumbr',
0x220: 'dpad_up',
0x221: 'dpad_down',
0x222: 'dpad_left',
0x223: 'dpad_right',
# XBox 360 controller uses these codes.
0x2c0: 'dpad_left',
0x2c1: 'dpad_right',
0x2c2: 'dpad_up',
0x2c3: 'dpad_down',
}
class GamepadThread(QObject):
ASignal = pyqtSignal()
BSignal = pyqtSignal()
XSignal = pyqtSignal()
YSignal = pyqtSignal()
xaxisSignal = pyqtSignal(float)
yaxisSignal = pyqtSignal(float)
rxaxisSignal = pyqtSignal(float)
ryaxisSignal = pyqtSignal(float)
axis_map = []
button_map = []
axis_states = {}
button_states = {}
def __init__(self, parent=None):
if getattr(self.__class__, '_has_instance', False):
RuntimeError('Cannot create another instance')
self.__class__._has_instance = True
self.isinitialized = False
self.isconnected = False
super(GamepadThread, self).__init__(parent)
try:
# Open the joystick device.
fn = '/dev/input/js0'
print('Opening %s...' % fn)
self.jsdev = open(fn, 'rb')
self.isconnected = True
except :
(type, value, traceback) = sys.exc_info()
sys.excepthook(type, value, traceback)
if self.isconnected:
self._initialize()
self.isinitialized = True
if self.isinitialized:
self.abort = False
self.thread = QThread()
self.thread.started.connect(self.process)
self.thread.finished.connect(self.stop)
self.moveToThread(self.thread)
def _initialize(self):
# Get the device name.
# buf = bytearray(63)
buf = array.array('b', [0] * 64)
ioctl(self.jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
buf = bytes(buf)
js_name = buf.decode()
js_name = js_name.strip(b'\x00'.decode())
print('Device name: %s initialized' % js_name)
# Get number of axes and buttons.
buf = array.array('B', [0])
ioctl(self.jsdev, 0x80016a11, buf) # JSIOCGAXES
self.num_axes = buf[0]
buf = array.array('B', [0])
ioctl(self.jsdev, 0x80016a12, buf) # JSIOCGBUTTONS
self.num_buttons = buf[0]
# Get the axis map.
buf = array.array('B', [0] * 0x40)
ioctl(self.jsdev, 0x80406a32, buf) # JSIOCGAXMAP
for axis in buf[:self.num_axes]:
axis_name = axis_names.get(axis, 'unknown(0x%02x)' % axis)
self.axis_map.append(axis_name)
self.axis_states[axis_name] = 0.0
# Get the button map.
buf = array.array('H', [0] * 200)
ioctl(self.jsdev, 0x80406a34, buf) # JSIOCGBTNMAP
for btn in buf[:self.num_buttons]:
btn_name = button_names.get(btn, 'unknown(0x%03x)' % btn)
self.button_map.append(btn_name)
self.button_states[btn_name] = 0
def start(self):
self.thread.start(QThread.HighPriority)
@pyqtSlot()
def stop(self):
self.abort = True
def work(self):
evbuf = self.jsdev.read(8)
if evbuf:
time, value, type, number = struct.unpack('IhBB', evbuf)
if not type & 0x80:
if type & 0x01:
button = self.button_map[number]
if button:
self.button_states[button] = value
if value == 0:
if button is 'A':
self.ASignal.emit()
if button is 'B':
self.BSignal.emit()
if button is 'X':
self.XSignal.emit()
if button is 'Y':
self.YSignal.emit()
if type & 0x02:
axis = self.axis_map[number]
if axis:
self.axis_states[axis] = value
fvalue = value / 32767.0
if axis is 'x':
self.xaxisSignal.emit(fvalue)
if axis is 'y':
self.yaxisSignal.emit(fvalue)
if axis is 'z':
self.rxaxisSignal.emit(fvalue)
if axis is 'rz':
self.ryaxisSignal.emit(fvalue)
@pyqtSlot()
def process(self):
while not self.abort:
try:
self.work()
except:
(type, value, traceback) = sys.exc_info()
sys.excepthook(type, value, traceback)
def __del__(self):
self.__class__.has_instance = False
try:
self.ASignal.disconnect()
self.BSignal.disconnect()
self.XSignal.disconnect()
self.YSignal.disconnect()
self.xaxisSignal.disconnect()
self.yaxisSignal.disconnect()
self.rxaxisSignal.disconnect()
self.ryaxisSignal.disconnect()
except TypeError:
pass
self.abort = True
# TODO: Test doesn't work, albeit Gamepadthread works in actual application
# Unit test code
if __name__ == '__main__':
class Test(QObject):
def __init__(self,parent=None):
super(Test, self).__init__(parent)
self.pad = GamepadThread()
self.pad.ASignal.connect(self.on_A)
self.pad.XSignal.connect(self.on_B)
self.pad.YSignal.connect(self.on_Y)
self.pad.XSignal.connect(self.on_X)
self.pad.xaxisSignal.connect(self.on_x)
self.pad.yaxisSignal.connect(self.on_y)
self.pad.rxaxisSignal.connect(self.on_x)
self.pad.ryaxisSignal.connect(self.on_y)
#self.pad.start()
@pyqtSlot()
def on_A(self):
print("A was released")
@pyqtSlot()
def on_B(self):
print("B was released")
@pyqtSlot()
def on_X(self):
print("X was released")
@pyqtSlot()
def on_Y(self):
print("Y was released")
self.pad.stop()
@pyqtSlot(float)
def on_x(self, x):
print(x)
@pyqtSlot(float)
def on_y(self, y):
print(y)
t = Test()
t.pad.start()
while not t.pad.abort:
pass
sys.exit(1)