-
Notifications
You must be signed in to change notification settings - Fork 10
/
arcade1.py
executable file
·346 lines (312 loc) · 10.4 KB
/
arcade1.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from uinputmapper.cinput import *
"""
Configuration for many EV_ABS(axis) and EV_KEY(digital buttons) directional controllers
... as EV_KEY MakeCode Arcade keyboard device
To optimize for speed on slower devices like the RPi Zero, please:
1. Remove redundant mappings which do no apply to your controller
2. Set min and max variables for your controller's axes manually below
"""
# Global variables
# Attempts to dettermine min and max for EV_ABS events more quickly and automatically if True.
# If False, a full stroke in both direction of one axis it required to determine min and max.
# Min and max values below can be set manually according to evtest to skip auto-calibration altogether.
autoCalibrateOn = True
# Deadzone in percentage before uinput-mapper reacts to EV_ABS events.
# Used to dampen reactions to axis movements around center values
deadzoneFactor = 0.25
# Variables for EV_ABS controller no. 1
invertYUP1 = False # For inverting Y axis if True, e.g. Nimbus SteelSeries controller
invertXLEFT1 = False # For inverting X axis if True
min1 = 0 # Seed value = 0 for auto-calibration. If set manual, find min and max using evtest or ../input-read -v -p /dev/input/eventX
max1 = 0 # Seed value = 0 for auto-calibration
mid1 = (min1 + max1)/2
deadzonePos1 = mid1 + (max1 - mid1)*deadzoneFactor
deadzoneNeg1 = mid1 - (mid1 - min1)*deadzoneFactor
# Variables for EV_ABS HAT controller no. 1
hmin1 = 0
hmax1 = 0
hmid1 = (hmin1 + hmax1)/2
# Auto-calibration function
# Determines min, mid and max value intervals and calculates deadzones
def AutoCalibrate(z, min, mid, max, deadzonePos, deadzoneNeg):
global autoCalibrateOn
if autoCalibrateOn:
# Finds most EV_ABS value intervals in 2^x increments in the int16 range from -32767 to 32768
for i in range(1, 17):
if abs(z) > (pow(2, i) - 1) - pow(2, i-2):
continue
else:
if i <= 2:
min = -1
max = 1
elif min >= 0:
min = 0
max = pow(2, i) - 1
elif max <= 0:
min = -pow(2, i) + 1
max = 0
else:
min = -pow(2, i-1) + 1
max = pow(2, i-1) - 1
break
mid = (min + max)/2
deadzonePos = mid + (max - mid)*deadzoneFactor
deadzoneNeg = mid - (mid - min)*deadzoneFactor
return min, mid, max, deadzonePos, deadzoneNeg
# Directional EV_ABS to EV_KEY conversion functions for controller no. 1
# For EV_ABS values less than center
def digitizeNeg1(n):
global min1, mid1, max1, deadzonePos1, deadzoneNeg1
# Calls calibration function until value interval is found
if n < min1:
min1 = n
min1, mid1, max1, deadzonePos1, deadzoneNeg1 = AutoCalibrate(\
n, min1, mid1, max1, deadzonePos1, deadzoneNeg1)
# Triggers corresponding EV_Key 'code' in config below for wanted EV_ABS range
if n < deadzoneNeg1:
return 1
return 0
# For EV_ABS values greater than center
def digitizePos1(p):
global min1, mid1, max1, deadzonePos1, deadzoneNeg1
# Calls calibration function until value interval is found
if p > max1:
max1 = p
min1, mid1, max1, deadzonePos1, deadzoneNeg1 = AutoCalibrate(\
p, min1, mid1, max1, deadzonePos1, deadzoneNeg1)
# Triggers corresponding EV_Key 'code' in config below for wanted EV_ABS range
if p > deadzonePos1:
return 1
return 0
# Directional EV_ABS HAT to EV_KEY conversion functions for controller no. 1
# For EV_ABS HAT values less than center
def hat0Neg1(n):
global hmin1, hmid1, hmax1
# Calls calibration function until value interval is found
if n > hmax1:
hmax1 = n
dummy = 0
hmin1, hmid1, hmax1, dummy, dummy = AutoCalibrate(\
n, hmin1, hmid1, hmax1, dummy, dummy)
# Triggers corresponding EV_Key 'code' in config below for wanted EV_ABS HAT range
if n < hmid1:
return 1
return 0
# For EV_ABS HAT values greater than center
def hat0Pos1(p):
global hmin1, hmid1, hmax1
# Calls calibration function until value interval is found
if p < hmin1:
hmin1 = p
dummy = 0
hmin1, hmid1, hmax1, dummy, dummy = AutoCalibrate(\
p, hmin1, hmid1, hmax1, dummy, dummy)
# Triggers corresponding EV_Key 'code' in config below for wanted EV_ABS HAT range
if p > hmid1:
return 1
return 0
# Button mapping config
# Uses two inputs per controller, to map and convert from EV_ABS to EV_KEY in both directions
config = {
# EV_ABS to EV_KEY mappings for 1st direction of controller 1
(0, EV_ABS): {
# X-axis initially towards less than/negative of center(normally left)
ABS_X: {
'type': (0, EV_KEY),
'code': 30, # Output key 'a'
'value': lambda x: digitizeNeg1(x) if not invertXLEFT1 else digitizePos1(x)
# lambda calls function with joystick value dependent on if axis is inverted or not
},
# Y-axis initially towards less than/negative of center(normally up)
ABS_Y: {
'type': (0, EV_KEY),
'code': 17, # Output key 'w'
'value': lambda y: digitizeNeg1(y) if not invertYUP1 else digitizePos1(y)
# lambda calls function with joystick value dependent on if axis is inverted or not
},
ABS_HAT0X: {
# HAT0X-axis initially towards greater than/positive of center(normally right)
'type': (0, EV_KEY),
'code': 32, # etc.
'value': hat0Pos1 # Joystick value variable x is implied for function hat0Pos
},
# HAT0Y-axis initially towards greater than/positive of center(normally down)
ABS_HAT0Y: {
'type': (0, EV_KEY),
'code': 31,
'value': hat0Pos1
}
},
# EV_ABS to EV_KEY mappings for 2nd direction of controller 1
(1, EV_ABS): {
# X-axis initially towards greater than/positive of center(normally right)
ABS_X: {
'type': (0, EV_KEY),
'code': 32,
'value': lambda x: digitizePos1(x) if not invertXLEFT1 else digitizeNeg1(x)
},
# Y-axis initially towards greater than/positive of center(normally down)
ABS_Y: {
'type': (0, EV_KEY),
'code': 31,
'value': lambda y: digitizePos1(y) if not invertYUP1 else digitizeNeg1(y)
},
# HAT0X-axis initially towards less than/negative of center(normally left)
ABS_HAT0X: {
'type': (0, EV_KEY),
'code': 30,
'value': hat0Neg1
},
# HAT0Y-axis initially towards less than/negative of center(normally up)
ABS_HAT0Y: {
'type': (0, EV_KEY),
'code': 17,
'value': hat0Neg1
},
# Start button conversion for some controllers without start button
ABS_Z: {
'type': (0, EV_KEY),
'code': 1,
'value': hat0Pos1
},
# Select button conversion for some controllers without select button
ABS_RZ: {
'type': (0, EV_KEY),
'code': 59,
'value': hat0Pos1
}
},
# First EV_KEY to EV_KEY mappings for controller 1
(0, EV_KEY): {
BTN_DPAD_UP: {
'type': (0, EV_KEY),
'code': 17,
'value': None
},
BTN_DPAD_DOWN: {
'type': (0, EV_KEY),
'code': 31,
'value': None
},
BTN_DPAD_LEFT: {
'type': (0, EV_KEY),
'code': 30,
'value': None
},
BTN_DPAD_RIGHT: {
'type': (0, EV_KEY),
'code': 32,
'value': None
},
BTN_SOUTH: { # (BTN_A synonym)
'type': (0, EV_KEY),
'code': 29,
'value': None
},
BTN_B: {
'type': (0, EV_KEY),
'code': 42,
'value': None
},
BTN_START: {
'type': (0, EV_KEY),
'code': 1,
'value': None
},
BTN_SELECT: {
'type': (0, EV_KEY),
'code': 59,
'value': None
},
BTN_MODE: {
'type': (0, EV_KEY),
'code': 60,
'value': None
}
},
# Second EV_KEY to EV_KEY mappings for controller 1
(1, EV_KEY): {
BTN_THUMB: {
'type': (0, EV_KEY),
'code': 29,
'value': None
},
BTN_THUMB2: {
'type': (0, EV_KEY),
'code': 42,
'value': None
},
BTN_BASE4: {
'type': (0, EV_KEY),
'code': 1,
'value': None
},
BTN_BASE3: {
'type': (0, EV_KEY),
'code': 59,
'value': None
},
KEY_HOMEPAGE: {
'type': (0, EV_KEY),
'code': 60,
'value': None
}
},
# Maps keyboard player 2 to WASD keys
(2, EV_KEY): {
KEY_A: {
'type': (0, EV_KEY),
'code': 105,
'value': None
},
KEY_D: {
'type': (0, EV_KEY),
'code': 106,
'value': None
},
KEY_W: {
'type': (0, EV_KEY),
'code': 103,
'value': None
},
KEY_S: {
'type': (0, EV_KEY),
'code': 108,
'value': None
},
KEY_LEFTCTRL: {
'type': (0, EV_KEY),
'code': 100,
'value': None
},
KEY_LEFTSHIFT: {
'type': (0, EV_KEY),
'code': 57,
'value': None
},
KEY_ESC: {
'type': (0, EV_KEY),
'code': 1,
'value': None
},
KEY_F1: {
'type': (0, EV_KEY),
'code': 59,
'value': None
},
KEY_F2: {
'type': (0, EV_KEY),
'code': 60,
'value': None
}
}
}
# Name for created output /dev/input/eventX
names = {
0: 'MakeCode_Arcade'
}
# Start uinput-mapper on configuration and with the output event name above
def config_merge(c, n):
c.clear() # Clear all initial incoming controller mappings
c.update(config) # Replace with above mapping configuration
n.update(names) # Update output event with the above name