-
Notifications
You must be signed in to change notification settings - Fork 4
/
nsfs17.py
executable file
·845 lines (785 loc) · 30.4 KB
/
nsfs17.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#!/usr/bin/python3
"""
MIT License
Copyright (c) 2020 [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-----------------------------------------------------------------------------
Adapt various USB joystick controllers for use with a Nintendo Switch (NS)
console. All controllers are active but are seen by the console as one
controller so co-pilot mode is always active.
Read from joysticks and write to NSGadget device. The following joysticks are
supported
* Hori HoriPad gamepad
* Xbox One gamepads
* PS4 gamepad
* Logitech Extreme 3D Pro flightstick
* Thrustmaster T.16000M flightstick
* Dragon Rise arcade joysticks
NSGadget is an Adafruit Trinket M0 emulating an NS compatible gamepad. The
connection to between the Pi and NSGadget is 2 Mbits/sec UART.
"""
import os
import time
import sys
import signal
import getopt
from struct import unpack
import threading
import array
from fcntl import ioctl
import serial
from gpiozero import Button
from nsgpadserial import NSGamepadSerial, NSButton, NSDPad
import mido
# Map the 4 direction buttons (up, right, down, left) to NS direction pad values
BUTTONS_MAP_DPAD = array.array('B', [
# U = Up button, R = right button, etc
# LDRU
NSDPad.CENTERED, # 0000
NSDPad.UP, # 0001
NSDPad.RIGHT, # 0010
NSDPad.UP_RIGHT, # 0011
NSDPad.DOWN, # 0100
NSDPad.CENTERED, # 0101
NSDPad.DOWN_RIGHT, # 0110
NSDPad.CENTERED, # 0111
NSDPad.LEFT, # 1000
NSDPad.UP_LEFT, # 1001
NSDPad.CENTERED, # 1010
NSDPad.CENTERED, # 1011
NSDPad.DOWN_LEFT, # 1100
NSDPad.CENTERED, # 1101
NSDPad.CENTERED, # 1110
NSDPad.CENTERED # 1111
])
NSG = NSGamepadSerial()
try:
# Raspberry Pi UART on pins 14,15
NS_SERIAL = serial.Serial('/dev/ttyAMA0', 2000000, timeout=0)
print("Found ttyAMA0")
except:
try:
# CP210x is capable of 2,000,000 bits/sec
NS_SERIAL = serial.Serial('/dev/ttyUSB0', 2000000, timeout=0)
print("Found ttyUSB0")
except:
print("NSGadget serial port not found")
sys.exit(1)
NSG.begin(NS_SERIAL)
def read_horipad(jsdev):
"""
The Hori HoriPad is a Nintendo Switch compatible gamepad.
Buttons and axes are mapped straight through so this is
the easiest. Runs as a thread
"""
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
if value:
NSG.press(number)
else:
NSG.release(number)
if type == 0x02: # axis event
axis = ((value + 32768) >> 8)
# Axes 0,1 left stick X,Y
if number == 0:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
# Axes 2,3 right stick X,Y
elif number == 2:
NSG.rightXAxis(axis)
elif number == 3:
NSG.rightYAxis(axis)
# Axes 4,5 directional pad X,Y
elif number == 4:
NSG.dPadXAxis(axis)
elif number == 5:
NSG.dPadYAxis(axis)
def read_hori_wheel(jsdev):
"""
The Hori Hori Maro Wheel is a Nintendo Switch compatible racing wheel.
Runs as a thread.
"""
BUTTON_MAP = array.array('B', [
NSButton.A,
NSButton.B,
NSButton.X,
NSButton.Y,
NSButton.LEFT_TRIGGER,
NSButton.RIGHT_TRIGGER,
NSButton.MINUS,
NSButton.PLUS,
NSButton.HOME,
NSButton.LEFT_STICK,
NSButton.RIGHT_STICK])
last_left_throttle = 0
last_right_throttle = 0
last_wheel = 128
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
button_out = BUTTON_MAP[number]
if value:
NSG.press(button_out)
else:
NSG.release(button_out)
if type == 0x02: # axis event
axis = ((value + 32768) >> 8)
# Axes 0,1 left stick X,Y
if number == 0:
if last_wheel != axis:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
elif number == 2:
if last_left_throttle != axis:
last_left_throttle = axis
if axis > 64:
NSG.press(NSButton.LEFT_THROTTLE)
else:
NSG.release(NSButton.LEFT_THROTTLE)
# Axes 3,4 right stick X,Y
elif number == 3:
NSG.rightXAxis(axis)
elif number == 4:
NSG.rightYAxis(axis)
elif number == 5:
if last_right_throttle != axis:
last_right_throttle = axis
if axis > 64:
NSG.press(NSButton.RIGHT_THROTTLE)
else:
NSG.release(NSButton.RIGHT_THROTTLE)
# Axes 6,7 directional pad X,Y
elif number == 6:
NSG.dPadXAxis(axis)
elif number == 7:
NSG.dPadYAxis(axis)
def read_xbox1(jsdev):
"""
The Xbox One controller has fewer buttons and the throttles are analog instead of buttons.
Runs as a thread
axis 0: left stick X
1: left stick Y
2: left throttle
3: right stick X
4: right stick Y
5: right throttle
6: dPad X
7: dPad Y
button 0: A NS B
1: B NS A
2: X NS Y
3: Y NS X
4: left trigger NS left trigger
5: right trigger NS right trigger
6: windows NS minus
7: lines NS plus
8: logo NS home
9: left stick button NS left stick
10: right stick button NS right stick
windows lines
Y
X B
A
"""
BUTTON_MAP = array.array('B', [
NSButton.B,
NSButton.A,
NSButton.Y,
NSButton.X,
NSButton.LEFT_TRIGGER,
NSButton.RIGHT_TRIGGER,
NSButton.MINUS,
NSButton.PLUS,
NSButton.HOME,
NSButton.LEFT_STICK,
NSButton.RIGHT_STICK])
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
button_out = BUTTON_MAP[number]
if value:
NSG.press(button_out)
else:
NSG.release(button_out)
if type == 0x02: # axis event
axis = ((value + 32768) >> 8)
# Axes 0,1 left stick X,Y
if number == 0:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
# Xbox throttle 0..255 but NS throttle is a button on/ff
elif number == 2:
if axis > 128:
NSG.press(NSButton.LEFT_THROTTLE)
else:
NSG.release(NSButton.LEFT_THROTTLE)
# Axes 3,4 right stick X,Y
elif number == 3:
NSG.rightXAxis(axis)
elif number == 4:
NSG.rightYAxis(axis)
# Xbox throttle 0..255 but NS throttle is a button on/ff
elif number == 5:
if axis > 128:
NSG.press(NSButton.RIGHT_THROTTLE)
else:
NSG.release(NSButton.RIGHT_THROTTLE)
# Axes 6,7 directional pad X,Y
elif number == 6:
NSG.dPadXAxis(axis)
elif number == 7:
NSG.dPadYAxis(axis)
def read_ps4ds(jsdev):
"""
The Sony Playstation 4 controller has fewer buttons. The throttles are
analog (see axes) and binary (see buttons). Runs as a thread.
axis 0: left stick X
1: left stick Y
2: left throttle
3: right stick X
4: right stick Y
5: right throttle
6: dPad X
7: dPad Y
button 0: cross NS B
1: circle NS A
2: triangle NS X
3: square NS Y
4: left trigger NS left trigger
5: right trigger NS right trigger
6: left throttle NS left throttle
7: right throttle NS right throttle
8: share NS minus
9: options NS plus
10: logo NS home
11: left stick button NS left stick button
12: right stick button NS rgith stick button
share options
triangle
square circle
cross
"""
BUTTON_MAP = array.array('B', [
NSButton.B,
NSButton.A,
NSButton.Y,
NSButton.X,
NSButton.LEFT_TRIGGER,
NSButton.RIGHT_TRIGGER,
NSButton.LEFT_THROTTLE,
NSButton.RIGHT_THROTTLE,
NSButton.MINUS,
NSButton.PLUS,
NSButton.HOME,
NSButton.LEFT_STICK,
NSButton.RIGHT_STICK])
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
button_out = BUTTON_MAP[number]
if value:
NSG.press(button_out)
else:
NSG.release(button_out)
if type == 0x02: # axis event
axis = ((value + 32768) >> 8)
# Axes 0,1 left stick X,Y
if number == 0:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
# axis 2 Xbox throttle 0..255 but NS throttle is a button on/ff
# Axes 3,4 right stick X,Y
elif number == 3:
NSG.rightXAxis(axis)
elif number == 4:
NSG.rightYAxis(axis)
# axis 5 Xbox throttle 0..255 but NS throttle is a button on/ff
# Axes 6,7 directional pad X,Y
elif number == 6:
NSG.dPadXAxis(axis)
elif number == 7:
NSG.dPadYAxis(axis)
def read_dragon_rise(jsdev, right_side):
"""
Map two Dragon Rise arcade joysticks to one NS gamepad
The Dragon Rise arcade joystick has 1 stick and up to 10 buttons. Two are required
to make 1 gamepad with 2 sticks plus 18 buttons. The right_side flag determines
which joystick is the left or right side of the gamepad.
"""
BUTTON_MAP_LEFT = array.array('B', [
NSButton.LEFT_THROTTLE,
NSButton.LEFT_TRIGGER,
NSButton.MINUS,
255, # DPAD Up
255, # DPAD Right
255, # DPAD Down
255, # DPAD Left
NSButton.LEFT_STICK,
NSButton.CAPTURE,
NSButton.CAPTURE,
NSButton.CAPTURE,
NSButton.CAPTURE])
BUTTON_MAP_RIGHT = array.array('B', [
NSButton.RIGHT_THROTTLE,
NSButton.RIGHT_TRIGGER,
NSButton.PLUS,
NSButton.A,
NSButton.B,
NSButton.X,
NSButton.Y,
NSButton.RIGHT_STICK,
NSButton.HOME,
NSButton.HOME,
NSButton.HOME,
NSButton.HOME])
dpad_bits = 0
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
if right_side:
button_out = BUTTON_MAP_RIGHT[number]
else:
button_out = BUTTON_MAP_LEFT[number]
if button_out == 255:
if value:
dpad_bits |= (1 << (number - 3))
else:
dpad_bits &= ~(1 << (number - 3))
NSG.dPad(BUTTONS_MAP_DPAD[dpad_bits])
else:
if value:
NSG.press(button_out)
else:
NSG.release(button_out)
if type == 0x02: # axis event
# NS wants values 0..128..255 where 128 is center position
axis = ((value + 32768) >> 8)
if right_side:
# Axes 0,1 right stick X,Y
if number == 0:
NSG.rightXAxis(axis)
elif number == 1:
NSG.rightYAxis(axis)
else:
# Axes 0,1 left stick X,Y
if number == 0:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
def read_le3dp(jsdev):
"""
The Logitech Extreme 3D Pro joystick (also known as a flight stick)
has a large X,Y,twist joystick with an 8-way hat switch on top.
This maps the large X,Y axes to the gamepad right thumbstick and
the hat switch to the gamepad left thumbstick. There are six
buttons on the top of the stick and six on the base. The twist
used to control the stick buttons. Each gamepad thumbstick is
also a button. For example, clicking the right thumbstick enables
stealth mode in Zelda:BOTW.
Map LE3DP button numbers to NS gamepad buttons
LE3DP buttons
0 = front trigger
1 = side thumb rest button
2 = top large left
3 = top large right
4 = top small left
5 = top small right
Button array (2 rows, 3 columns) on base
7 9 11
6 8 10
"""
BUTTON_MAP = array.array('B', [
NSButton.A, # Front trigger
NSButton.B, # Side thumb trigger
NSButton.X, # top large left
NSButton.Y, # top large right
NSButton.LEFT_TRIGGER, # top small left
NSButton.RIGHT_TRIGGER, # top small right
NSButton.MINUS,
NSButton.PLUS,
NSButton.CAPTURE,
NSButton.HOME,
NSButton.LEFT_THROTTLE,
NSButton.RIGHT_THROTTLE])
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
button_out = BUTTON_MAP[number]
if value:
NSG.press(button_out)
else:
NSG.release(button_out)
if type == 0x02: # axis event
axis = ((value + 32768) >> 8)
# Axes 0,1 -> NS left thumbstick X,Y
if number == 0:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
# Axis 2 twist
# Axis 3 throttle lever
# Axes 4,5 hat switch -> NS right thumbstick X,Y
elif number == 4:
NSG.rightXAxis(axis)
elif number == 5:
NSG.rightYAxis(axis)
def read_t16k(jsdev):
"""
Map T16K button numbers to NS gamepad buttons
The Thrustmaster T.16000M ambidextrous joystick (also known as a flight stick)
has a large X,Y,twist joystick with an 8-way hat switch on top.
This function maps the large X,Y axes to the gamepad right thumbstick and
the hat switch to the gamepad left thumbstick. There are four
buttons on the top of the stick and 12 on the base. The twist
used to control the stick buttons. Each gamepad thumbstick is
also a button. For example, clicking the right thumbstick enables
stealth mode in Zelda:BOTW.
Map T16K button numbers to NS gamepad buttons
T16K buttons
0 = trigger
1 = top center
2 = top left
3 = top right
Button array on base, left side
4
9 5
8 6
7
Button array on base, right side
10
11 15
12 14
13
"""
BUTTON_MAP = array.array('B', [
NSButton.A, # Trigger
NSButton.B, # Top center
NSButton.X, # Top Left
NSButton.Y, # Top Right
NSButton.LEFT_TRIGGER, # Base left 4
NSButton.RIGHT_TRIGGER, # Base left 5
NSButton.MINUS, # Base left 6
NSButton.PLUS, # Base left 7
NSButton.CAPTURE, # Base left 8
NSButton.HOME, # Base left 9
NSButton.LEFT_THROTTLE, # Base right 10
NSButton.RIGHT_THROTTLE,# Base right 11
NSButton.LEFT_THROTTLE, # Base right 12
NSButton.RIGHT_THROTTLE,# Base right 13
NSButton.LEFT_THROTTLE, # Base right 14
NSButton.RIGHT_THROTTLE])# Base right 15
while True:
try:
evbuf = jsdev.read(8)
except:
jsdev.close()
break
if evbuf:
timestamp, value, type, number = unpack('IhBB', evbuf)
if type == 0x01: # button event
button_out = BUTTON_MAP[number]
if value:
NSG.press(button_out)
else:
NSG.release(button_out)
if type == 0x02: # axis event
# NS wants values 0..128..255 where 128 is center position
axis = ((value + 32768) >> 8)
# Axes 0,1 -> NS left thumbstick X,Y
if number == 0:
NSG.leftXAxis(axis)
elif number == 1:
NSG.leftYAxis(axis)
# Axis 2 twist
# Axis 3 throttle lever
# Axes 4,5 hat switch -> right thumbstick X,Y
elif number == 4:
NSG.rightXAxis(axis)
elif number == 5:
NSG.rightYAxis(axis)
class DpadBits(object):
""" Convert 4 direction buttons to direction pad values """
def __init__(self):
self.dpad_bits = 0
def set_bit(self, bit_num):
""" Set bit in direction pad bit map. Update NSGadget direction pad. """
self.dpad_bits |= (1 << bit_num)
return BUTTONS_MAP_DPAD[self.dpad_bits]
def clear_bit(self, bit_num):
""" Clear bit in direction pad bit map. Update NSGadget direction pad. """
self.dpad_bits &= ~(1 << bit_num)
return BUTTONS_MAP_DPAD[self.dpad_bits]
def gpio_handler():
""" Thread to handle buttons connected to GPIO pins. """
all_buttons = {}
dpad_bits = DpadBits()
def gpio_pressed(button):
""" Called when button connected to GPIO pin is pressed/closed """
print('pressed', button.pin)
if button.pin in all_buttons:
ns_button = all_buttons[button.pin]
if ns_button < 128:
NSG.press(ns_button)
else:
NSG.dPad(dpad_bits.set_bit(255 - ns_button))
else:
print('Invalid button');
def gpio_released(button):
""" Called when button connected to GPIO pin is released/opened """
print('released', button.pin)
if button.pin in all_buttons:
ns_button = all_buttons[button.pin]
if ns_button < 128:
NSG.release(ns_button)
else:
NSG.dPad(dpad_bits.clear_bit(255 - ns_button))
else:
print('Invalid button');
gpio_ns_map = (
# Left side (blue joy-con) buttons
{'gpio_number': 4, 'ns_button': NSButton.LEFT_THROTTLE},
{'gpio_number': 17, 'ns_button': NSButton.LEFT_TRIGGER},
{'gpio_number': 27, 'ns_button': NSButton.MINUS},
{'gpio_number': 22, 'ns_button': NSButton.CAPTURE},
{'gpio_number': 5, 'ns_button': 255},
{'gpio_number': 6, 'ns_button': 254},
{'gpio_number': 13, 'ns_button': 253},
{'gpio_number': 19, 'ns_button': 252},
{'gpio_number': 26, 'ns_button': NSButton.LEFT_STICK},
# Right side (red joy-con) buttons
{'gpio_number': 23, 'ns_button': NSButton.RIGHT_THROTTLE},
{'gpio_number': 24, 'ns_button': NSButton.RIGHT_TRIGGER},
{'gpio_number': 25, 'ns_button': NSButton.PLUS},
{'gpio_number': 8, 'ns_button': NSButton.HOME},
{'gpio_number': 7, 'ns_button': NSButton.A},
{'gpio_number': 12, 'ns_button': NSButton.B},
{'gpio_number': 16, 'ns_button': NSButton.X},
{'gpio_number': 20, 'ns_button': NSButton.Y},
{'gpio_number': 21, 'ns_button': NSButton.RIGHT_STICK}
)
# For each GPIO to NS button entry, allocate gpiozero Button object
# and update all_buttons dictionary. The when_pressed and when_released
# callback functions use all_buttons to find the corresponding
# NS button value.
for element in gpio_ns_map:
element['button'] = Button(element['gpio_number'])
all_buttons[element['button'].pin] = element['ns_button']
element['button'].when_pressed = gpio_pressed
element['button'].when_released = gpio_released
signal.pause()
def read_midi_notes(midi_input_name, first_note):
"""
Read MIDI note on/off and generate NS gamepad events.
first_note -- First/lowest MIDI note number on keyboard
"""
NOTE_MAP = [
# Honk horn
{'buttons': [NSButton.RIGHT_TRIGGER, NSButton.B]}, # Note On 53
# Radio on/off
{'buttons': [NSButton.LEFT_TRIGGER, NSButton.RIGHT_TRIGGER], 'dpad': NSDPad.UP}, # Note On 54
0, # Note On 55
# Radio prev station/if radio off, left turn signal
{'buttons': [NSButton.LEFT_TRIGGER, NSButton.RIGHT_TRIGGER], 'dpad': NSDPad.LEFT}, # Note On 56
0, # Note On 57
# Radio nextious station/if radio off, right turn signal
{'buttons': [NSButton.LEFT_TRIGGER, NSButton.RIGHT_TRIGGER], 'dpad': NSDPad.RIGHT}, # Note On 58
0, # Note On 59
0, # Note On 60
0, # Note On 61
0, # Note On 62
0, # Note On 63
0, # Note On 64
0, # Note On 65
0, # Note On 66
0, # Note On 67
0, # Note On 68
0, # Note On 69
0, # Note On 70
0, # Note On 71
0, # Note On 72
0, # Note On 73
0, # Note On 74
# Unload
{'buttons': [NSButton.RIGHT_TRIGGER, NSButton.X]}, # Note On 75
# Open cover
{'buttons': [NSButton.LEFT_TRIGGER], 'dpad': NSDPad.LEFT}, # Note On 76
# Tool on/off
{'buttons': [NSButton.LEFT_TRIGGER, NSButton.Y]}, # Note On 77
# Head lights
{'buttons': [NSButton.RIGHT_TRIGGER, NSButton.A]}, # Note On 78
# OK, next
{'buttons': [NSButton.A]}, # Note On 79
# Fold/unfold tool
{'buttons': [NSButton.LEFT_TRIGGER, NSButton.B]}, # Note On 80
# Jump, attach/detach tool
{'buttons': [NSButton.B]}, # Note On 81
# Raise/lower tool
{'buttons': [NSButton.LEFT_TRIGGER, NSButton.A]}, # Note On 82
# Crouch, select tool
{'buttons': [NSButton.X]}, # Note On 83
# Enter/exit
{'buttons': [NSButton.Y]}, # Note On 84
]
with mido.open_input(midi_input_name) as inport:
for msg in inport:
if msg.type == 'note_on':
index = msg.note - first_note
print(index)
if index >= 0 and NOTE_MAP[index]:
controls = NOTE_MAP[index]
print(controls)
if 'buttons' in controls:
for btn in controls['buttons']:
print('press ', btn)
NSG.press(btn)
if 'dpad' in controls:
print('dpad ', controls['dpad'])
NSG.dPad(controls['dpad'])
elif msg.type == 'note_off':
index = msg.note - first_note
if index >= 0 and NOTE_MAP[index]:
controls = NOTE_MAP[index]
if 'dpad' in controls:
print('dpad centered')
NSG.dPad(NSDPad.CENTERED)
if 'buttons' in controls:
for btn in reversed(controls['buttons']):
print('release ', btn)
NSG.release(btn)
def main():
""" joystick ioctl code based on https://gist.github.com/rdb/8864666 """
threading.Thread(target=gpio_handler, args=(), daemon=True).start()
# Look for new MIDI input devices
for midi_input_name in mido.get_input_names():
if 'AKM320' in midi_input_name:
print(midi_input_name)
thr_id = threading.Thread(target=read_midi_notes, args=(midi_input_name, 53,), daemon=True)
thr_id.start()
dr_count = 0
joysticks = {}
joysticks_to_del = list()
while True:
# For all known joysticks make sure its thread is still alive. If not, forget the joystick
# The thread ends when the joystick is unplugged.
for jsname in joysticks:
if not joysticks[jsname].is_alive():
print("joystick %s removed" % jsname)
joysticks_to_del.append(jsname)
for jsname in joysticks_to_del:
del joysticks[jsname]
joysticks_to_del.clear()
# Look for new joysticks
# For all joysticks in /dev/input/ and not do not have a thread, start a thread
# Each thread reads from its associated joystick and writes to the NS gadget device.
for fn in os.listdir('/dev/input'):
if fn.startswith('js'):
jsname = '/dev/input/' + fn
if not jsname in joysticks:
try:
jsdev = open(jsname, 'rb')
except:
break
buf = array.array('B', [0] * 64)
ioctl(jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)
jslongname = buf.tobytes().rstrip(b'\x00').decode('utf-8').upper()
if 'HORIPAD' in jslongname:
print("Found HoriPad")
thr_id = threading.Thread(target=read_horipad, args=(jsdev,), daemon=True)
thr_id.start()
joysticks[jsname] = thr_id
elif 'DRAGONRISE INC.' in jslongname:
print("Found Dragon Rise")
# At least 2 DR arcade joysticks are required to make a gamepad.
if dr_count & 1:
thr_id = threading.Thread(target=read_dragon_rise, args=(jsdev, True,), daemon=True)
thr_id.start()
else:
thr_id = threading.Thread(target=read_dragon_rise, args=(jsdev, False,), daemon=True)
thr_id.start()
dr_count += 1
joysticks[jsname] = thr_id
elif 'LOGITECH EXTREME 3D' in jslongname:
print("Found Logitech Extreme 3D Pro")
thr_id = threading.Thread(target=read_le3dp, args=(jsdev,), daemon=True)
thr_id.start()
joysticks[jsname] = thr_id
elif 'THRUSTMASTER T.16000M' in jslongname:
print("Found Thrustmaster T.16000M")
thr_id = threading.Thread(target=read_t16k, args=(jsdev,), daemon=True)
thr_id.start()
joysticks[jsname] = thr_id
elif 'MICROSOFT X-BOX ONE' in jslongname:
print("Found Xbox One")
thr_id = threading.Thread(target=read_xbox1, args=(jsdev,), daemon=True)
thr_id.start()
joysticks[jsname] = thr_id
elif 'SONY INTERACTIVE ENTERTAINMENT WIRELESS CONTROLLER' in jslongname:
print("Found Sony PS4DS")
thr_id = threading.Thread(target=read_ps4ds, args=(jsdev,), daemon=True)
thr_id.start()
joysticks[jsname] = thr_id
elif 'GENERIC X-BOX PAD' in jslongname:
print("Found Hori Mario Wheel")
thr_id = threading.Thread(target=read_hori_wheel, args=(jsdev,), daemon=True)
thr_id.start()
joysticks[jsname] = thr_id
else:
jsdev.close()
time.sleep(0.1)
if __name__ == "__main__":
main()