-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamecontrollerdb.py
507 lines (405 loc) · 16.8 KB
/
gamecontrollerdb.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import sys
import pygame
class GameController:
def __init__(self, joystick, mappings):
self.reset()
print(f"Hats on Joystick: {joystick.get_numhats()}")
self.joystick = joystick
self.instance_id = joystick.get_instance_id()
self.mappings = mappings
self.dpad_is_hat = True
self.build_lookups()
def extract_lookup(self, lookup):
for k in self.mappings:
if k.startswith(lookup + ":"):
print(f"Found lookup, {lookup}, at {k}")
# return the value of the mapping after the colon
return k.split(":")[1]
def build_lookups(self):
print(f"Lookups before build: {self.lookups}")
# build the lookups
self.lookups["a"] = int(self.extract_lookup("a")[1:])
self.lookups["b"] = int(self.extract_lookup("b")[1:])
self.lookups["x"] = int(self.extract_lookup("x")[1:])
self.lookups["y"] = int(self.extract_lookup("y")[1:])
self.lookups["r1"] = int(self.extract_lookup("rightshoulder")[1:])
self.lookups["l1"] = int(self.extract_lookup("leftshoulder")[1:])
self.lookups["r2"] = int(self.extract_lookup("righttrigger")[1:])
self.lookups["l2"] = int(self.extract_lookup("lefttrigger")[1:])
self.lookups["l3"] = int(self.extract_lookup("leftstick")[1:])
self.lookups["r3"] = int(self.extract_lookup("rightstick")[1:])
self.lookups["select"] = int(self.extract_lookup("back")[1:])
self.lookups["start"] = int(self.extract_lookup("start")[1:])
self.lookups["lx"] = int(self.extract_lookup("leftx")[1:])
self.lookups["ly"] = int(self.extract_lookup("lefty")[1:])
self.lookups["rx"] = int(self.extract_lookup("rightx")[1:])
self.lookups["ry"] = int(self.extract_lookup("righty")[1:])
# on some controllers dpad is buttons but on most it is a hat
# let's check dpad up to see if it's referencing a hat (h) or button (b)
dpup = str(self.extract_lookup("dpup"))
if str(dpup).startswith("h"):
self.dpad_is_hat = True
# grab the hat number in the character after the h
hat_num = int(dpup[1:2])
print(f"dpad bound to hat: {hat_num}")
self.lookups["up"] = hat_num
self.lookups["down"] = hat_num
self.lookups["left"] = hat_num
self.lookups["right"] = hat_num
else:
# bind the dpad to the buttons
self.dpad_is_hat = False
self.lookups["up"] = int(self.extract_lookup("dpup")[1:])
self.lookups["down"] = int(self.extract_lookup("dpdown")[1:])
self.lookups["left"] = int(self.extract_lookup("dpleft")[1:])
self.lookups["right"] = int(self.extract_lookup("dpright")[1:])
print(f"dpad_up: {dpup}")
print(f"Lookups after build: {self.lookups}")
def reset(self):
self.held = []
self.pressed = []
self.released = []
self.l_trigger = 0.0
self.r_trigger = 0.0
self.l_thumb = (0.0, 0.0)
self.r_thumb = (0.0, 0.0)
self.hats = []
self.__events_to_handle = [
pygame.JOYAXISMOTION,
pygame.JOYBALLMOTION,
pygame.JOYHATMOTION,
pygame.JOYBUTTONDOWN,
pygame.JOYBUTTONUP,
]
self.lookups = {
"a": None,
"b": None,
"x": None,
"y": None,
"up": None,
"down": None,
"left": None,
"right": None,
"l1": None,
"r1": None,
"l3": None,
"r3": None,
"select": None,
"start": None,
"l_thumb": None,
"r_thumb": None,
"zl": None,
"zr": None,
}
def handle_events(self, event: pygame.event.Event):
# we only care about events that are related to the joystick
if event.type not in self.__events_to_handle:
return
# we only care about events that are related to this joystick
if event.instance_id != self.instance_id:
return
# handle the event
print(f"Handling event: {event}")
# logic for handling a generic hat input each frame
def __update_hat_input(self, button):
hat_pressed = False
# check if the button is mapped
if self.lookups[button] is not None:
# get the data from the hat
x, y = self.hats[self.lookups[button]]
# print(f"Button: {button}, x: {x}, y: {y}")
if button == "up" and y == 1:
hat_pressed = True
# check if the hat is pressed in the direction of the button
if button == "down" and y == -1:
hat_pressed = True
if button == "left" and x == -1:
hat_pressed = True
if button == "right" and x == 1:
hat_pressed = True
# check if the button is pressed
if hat_pressed:
# the button was pressed so ...
# check if the button is already pressed
if button in self.held:
# the button was already pressed
# make sure it's not in the pressed list
# any longer
if button in self.pressed:
self.pressed.remove(button)
else:
# the button was just pressed
self.pressed.append(button)
self.held.append(button)
else:
# check if the button was previously pressed
if button in self.held:
# the button was previously pressed
# so it is now released
self.held.remove(button)
if button in self.pressed:
self.pressed.remove(button)
self.released.append(button)
else:
# the button was not previously held and still is not
# make sure it's not in the released list any longer
if button in self.released:
self.released.remove(button)
# logic for handling a generic button input each frame
def __update_button_input(self, button):
# check if the button is mapped
if self.lookups[button] is not None:
# check if the button is pressed
if self.joystick.get_button(self.lookups[button]):
# the button was pressed so ...
# check if the button is already pressed
if button in self.held:
# the button was already pressed
# make sure it's not in the pressed list
# any longer
if button in self.pressed:
self.pressed.remove(button)
else:
# the button was just pressed
self.pressed.append(button)
self.held.append(button)
else:
# check if the button was previously pressed
if button in self.held:
# the button was previously pressed
# so it is now released
self.held.remove(button)
if button in self.pressed:
self.pressed.remove(button)
self.released.append(button)
else:
# the button was not previously held and still is not
# make sure it's not in the released list any longer
if button in self.released:
self.released.remove(button)
# to be called once per frame
def update(self):
# update the list of hats
self.hats = [
self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())
]
self.__update_button_input("a")
self.__update_button_input("b")
self.__update_button_input("x")
self.__update_button_input("y")
self.__update_button_input("l1")
self.__update_button_input("r1")
self.__update_button_input("l3")
self.__update_button_input("r3")
self.__update_button_input("select")
self.__update_button_input("start")
# update sticks and triggers
self.l_thumb = (
self.joystick.get_axis(self.lookups["lx"]),
self.joystick.get_axis(self.lookups["ly"]),
)
self.r_thumb = (
self.joystick.get_axis(self.lookups["rx"]),
self.joystick.get_axis(self.lookups["ry"]),
)
self.l_trigger = self.joystick.get_axis(self.lookups["l2"])
self.r_trigger = self.joystick.get_axis(self.lookups["r2"])
# check if the dpad is buttons or a hat
if not self.dpad_is_hat:
# perform button update of dpad
self.__update_button_input("up")
self.__update_button_input("down")
self.__update_button_input("left")
self.__update_button_input("right")
else:
# perform hat update of dpad
self.__update_hat_input("up")
self.__update_hat_input("down")
self.__update_hat_input("left")
self.__update_hat_input("right")
# if the hat is not (0,0) lets print it out for now
hat_count = 0
for hat in self.hats:
if hat != (0, 0):
print(f"hat {hat_count}: {hat}")
hat_count += 1
# returns the string representation of the platform the script is running on
# in the format expected by the gamecontrollerdb.txt file. Returns 'unknown' if
# the platform is not recognized.
def parse_file(field, value):
platform = get_platform()
lines = None
# load the gamecontrollerdb.txt file
with open("gamecontrollerdb.txt", "r") as file:
lines = file.readlines()
# iterate through the lines of the file
if lines is None:
return None
for line in lines:
# skip comments
if line.startswith("#"):
continue
# skip any blank lines
if line.strip() == "":
continue
# split the line into fields
fields = line.split(",")
fields_count = len(fields)
# make sure it has at least 4 fields, the minimum required
# for there to be at least a guid, name, a single mapping and platform.
# there is also an extra comma at the end of the line, so we need to
# account for that
if fields_count < 5:
continue
l_guid = fields[0].strip()
l_name = fields[1].strip()
l_mappings = fields[2:-2]
l_platform = fields[-2].strip()
# check if the platform matches
if l_platform.endswith(platform) == False:
continue
# check if we are looking for a guid or a name
match_found = False
if field == "guid":
if l_guid == value:
match_found = True
elif field == "name":
if l_name == value:
match_found = True
# if we found a match, return the mappings
if match_found:
print(f"Match found: {l_name} [{l_guid}] on {l_platform}")
return l_mappings
# print(f"Processing line. Fields count: {fields_count}, guid: {l_guid}, name: {l_name}, mappings: {l_mappings}, platform: {l_platform}")
# sys.exit(0)
return None
def mappings_by_name(name):
return parse_file("name", name)
def mappings_by_guid(guid):
return parse_file("guid", guid)
def get_platform():
platform = sys.platform
if platform.startswith("win32"):
return "Windows"
elif platform.startswith("darwin"):
return "Mac OS X"
elif platform.startswith("linux"):
return "Linux"
else:
# default to windows if we can't determine the platform
return "unknown"
if __name__ == "__main__":
print("Running tests on : " + get_platform())
# #
# print(mappings_by_guid('03000000c82d00000161000000010000'))
# print(mappings_by_name('8BitDo SN30 Pro'))
# start pygame
pygame.init()
pygame.joystick.init()
joysticks = [
pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())
]
font = pygame.font.Font(None, 36)
print(f"Joysticks found: {len(joysticks)}")
for joystick in joysticks:
print(f"Joystick name: {joystick.get_name()}")
print(f"Joystick guid: {joystick.get_guid()}")
joy_map = mappings_by_guid(joystick.get_guid())
print(f"Joystick mappings: {joy_map}")
if joy_map is not None:
game_controller = GameController(joystick, joy_map)
print(f"Game controller created: {game_controller}")
# make a window to draw to
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
while True:
# clear the screen
screen.fill((0, 0, 0))
# draw text on the screen for held, pressed, released
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
# game_controller.handle_events(event)
# fill the screen with black and draw it
# update the game controller
game_controller.update()
x_positions = {
"a": 120,
"b": 140,
"x": 160,
"y": 180,
"l1": 200,
"r1": 230,
"up": 250,
"down": 250,
"left": 320,
"right": 320,
"l3": 400,
"r3": 440,
"select": 480,
"start": 560,
}
y_positions = {
"pressed": 0,
"held": 40,
"released": 80,
"left stick": 120,
"right stick": 160,
"left trigger": 200,
"right trigger": 240,
}
white = (255, 255, 255)
blue = (0, 0, 255)
green = (0, 255, 0)
orange = (255, 165, 0)
for k, v in y_positions.items():
text = font.render(k, True, white)
screen.blit(text, (0, v))
# draw analog stick and trigger values
text = font.render(f"{game_controller.l_thumb}", True, white)
screen.blit(text, (200, y_positions["left stick"]))
text = font.render(f"{game_controller.r_thumb}", True, white)
screen.blit(text, (200, y_positions["right stick"]))
text = font.render(f"{game_controller.l_trigger}", True, white)
screen.blit(text, (200, y_positions["left trigger"]))
text = font.render(f"{game_controller.r_trigger}", True, white)
screen.blit(text, (200, y_positions["right trigger"]))
for check in [
"a",
"b",
"x",
"y",
"up",
"down",
"left",
"right",
"r1",
"l1",
"l3",
"r3",
"select",
"start",
]:
if check in game_controller.pressed:
print(f"{check} button pressed")
# draw the letter on the screen
text = font.render(check, True, green)
screen.blit(text, (x_positions[check], y_positions["pressed"]))
if check in game_controller.held:
# this is a bit obnoxious on held in the terminal
# maybe think about checking in pressed and writing
# held there to let the user know it is now being
# held.
#
# print(f"{check} button held")
# draw the letter on the screen
text = font.render(check, True, blue)
screen.blit(text, (x_positions[check], y_positions["held"]))
if check in game_controller.released:
print(f"{check} button released")
# draw the letter on the screen
text = font.render(check, True, orange)
screen.blit(text, (x_positions[check], y_positions["released"]))
pygame.display.flip()
# 60 fps
clock.tick(60)