-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjapi.py
More file actions
249 lines (211 loc) · 6.34 KB
/
japi.py
File metadata and controls
249 lines (211 loc) · 6.34 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
242
243
244
245
246
247
248
249
import os
import struct
import 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: 'trigger',
0x121: 'thumb',
0x122: 'thumb2',
0x123: 'top',
0x124: 'top2',
0x125: 'pinkie',
0x126: 'base',
0x127: 'base2',
0x128: 'base3',
0x129: 'base4',
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'
}
def getAvailableJoysticks():
"""
Return: a list of joystick's name
"""
rtn_list = []
for fn in os.listdir('/dev/input/by-id'):
# need: usb-Microsoft_Controller_7EED867F0E86-joystick
# not: usb-Microsoft_Controller_7EED867F0E86-event-joystick
if fn.endswith('-joystick') and not fn.endswith('-event-joystick'):
rtn_list.append(fn)
return rtn_list
class Joystick:
# The hardware link, like: /dev/input/by-id/{id}
device_path = None
# The joystick name + symbol code, listed on /dev/input/by-id/
uuid = None
# It can be None if joystick init with device_path or name,
# the position in the input list, it update once only in the __init__
idx = None
# We'll store the states here.
axis_states = {}
button_states = {}
axis_map = []
button_map = []
# The number of axes and buttons.
num_axes = None
num_buttons = None
# The 'file' object, open in __init__
_jsdev = None
# The joystick name(up to 64 bytes) provided by the joystick
_name = None
def __init__(self, *args):
"""
Args:
symbol -- the index of the joysticks list or
a full device path or
a joystick name
"""
symbol = args[0]
if type(symbol) == int:
self.idx = symbol
self.uuid = getAvailableJoysticks()[self.idx]
self.device_path = '/dev/input/by-id/' + self.uuid
elif type(symbol) == str:
if '/' in symbol:
self.device_path = symbol
self.uuid = self.device_path.split('/')[-1]
else:
self.uuid = symbol
self.device_path = '/dev/input/by-id/' + self.uuid
else:
raise TypeError('not support symbol type for init joystick')
_jsdev = open(self.device_path, 'rb')
self._jsdev = _jsdev
# Get the device name.
#buf = bytearray(63)
buf = array.array('b', [0] * 64)
ioctl(_jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
self._name = buf.tostring()
# Get number of axes and buttons.
buf = array.array('B', [0])
ioctl(_jsdev, 0x80016a11, buf) # JSIOCGAXES
self.num_axes = buf[0]
buf = array.array('B', [0])
ioctl(_jsdev, 0x80016a12, buf) # JSIOCGBUTTONS
self.num_buttons = buf[0]
# Get the axis map.
buf = array.array('B', [0] * 0x40)
ioctl(_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(_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 read(self):
"""
wait until receive new joystick signal
Return: A list of JoystickEvent
"""
evbuf = self._jsdev.read(8)
updated = []
if evbuf:
time, value, type, number = struct.unpack('IhBB', evbuf)
if type & 0x80:
updated.append(JoystickEvent('initial', None, None))
if type & 0x01:
button = self.button_map[number]
if button:
self.button_states[button] = value
updated.append(JoystickEvent('button', button, value))
if type & 0x02:
axis = self.axis_map[number]
if axis:
fvalue = value / 32767.0
self.axis_states[axis] = fvalue
updated.append(JoystickEvent('axis', axis, fvalue))
return updated
def close(self):
"""
Flush and close the Joystick IO object.
Return: True if the IO object is closed successfully
"""
try:
self._jsdev.close()
self._jsdev = None
except:
pass
return self.is_open()
def is_open(self):
"""
Return: True if the IO object is closed
"""
return self._jsdev is not None
class JoystickEvent:
def __init__(self, t, n, v):
"""
Args:
t -- the type of the event, it must be 'initial', 'button' or 'axis'
n -- the channel name
v -- the new value of the channel
"""
self.type = t
self.name = n # it can be None if it is a initial event
self.value = v # it can be None if it is a initial event
def __str__(self):
return str(self.__dict__)
def __repr__(self):
return '%s.%s(%s, %s, %s)' % (
self.__module__,
self.__class__.__name__,
self.type,
self.name,
self.value)