-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1069 lines (904 loc) · 41.9 KB
/
main.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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import time
import psutil
from collections import deque
import multiprocessing
import mediapipe as mp
import datetime
import cv2
from blessed import Terminal
cap = cv2.VideoCapture(0)
from nxbt import Nxbt, PRO_CONTROLLER
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
calibrate = True
prevCoords = []
calibratedCoords = []
rt1Button = 0
rt2Button = 0
aButton = 0
lt1Button = 0
lt2Button = 0
numPlayers = 1
arrows = []
joystickPositionArr = []
joycon = cv2.resize(cv2.imread("./joycon.jpg"), dsize=[110, 150])
class Controller:
def __init__(self):
self.left_trigger = 0
self.left_bumper = 0
self.right_trigger = 0
self.right_bumper = 0
self.joystick = 0
self.joystickY = 0
self.Abutton = 0
self.player = 0
def __add__(self, other):
ret = Controller()
ret.left_trigger = self.left_trigger + other.left_trigger
ret.left_bumper = self.left_bumper + other.left_bumper
ret.right_trigger = self.right_trigger + other.right_trigger
ret.right_bumper = self.right_bumper + other.right_bumper
ret.joystick = self.joystick + other.joystick
ret.joystickY = self.joystickY + other.joystickY
ret.Abutton = self.Abutton + other.Abutton
return ret
def __truediv__(self, other):
ret = Controller()
ret.left_trigger = self.left_trigger / other
ret.left_bumper = self.left_bumper / other
ret.right_trigger = self.right_trigger / other
ret.right_bumper = self.right_bumper / other
ret.joystick = self.joystick / other
ret.joystickY = self.joystickY / other
ret.Abutton = self.Abutton / other
return ret
def __str__(self):
ret = ""
ret += "Left Trigger: " + str(self.left_trigger) + "\n"
ret += "Left Bumper: " + str(self.left_bumper) + "\n"
ret += "Right Trigger: " + str(self.right_trigger) + "\n"
ret += "Right Bumper: " + str(self.right_bumper) + "\n"
ret += "Joystick: " + str(self.joystick) + "\n"
ret += "JoystickY: " + str(self.joystickY) + "\n"
ret += "A Button: " + str(self.Abutton) + "\n"
return ret
# switch_controller = SwitchController()
# print(switch_controller.connected)
def getDelta(newCoords, oldCoords):
deltaArray = [] # array of delta coords
deltaAverageX = 0
deltaAverageY = 0
deltaAverageZ = 0
deltaAverage = 0
if (
oldCoords == [] or newCoords == [] or oldCoords == None or newCoords == None): # base case if we dont have previous hand coords
deltaArray.append(100)
return 2
else: # Regular case where we take new coords and subtract from prev coords
for i in range(len(newCoords)):
for j in range(len(newCoords[i].landmark)):
print("--------------------")
deltax = abs(newCoords[i].landmark[j].x - oldCoords[i].landmark[j].x)
deltay = abs(newCoords[i].landmark[j].y - oldCoords[i].landmark[j].y)
deltaz = abs(newCoords[i].landmark[j].z - oldCoords[i].landmark[j].z)
deltaArray.append({"deltax": deltax, "deltay": deltay, "deltaz": deltaz})
for i in deltaArray:
print(i)
deltaAverageX += i["deltax"]
deltaAverageY += i["deltay"]
deltaAverageZ += i["deltaz"]
deltaAverageX /= float(len(deltaArray))
deltaAverageY /= float(len(deltaArray))
deltaAverageZ /= float(len(deltaArray))
deltaAverage = (deltaAverageX + deltaAverageY + deltaAverageZ) / 3.0
print(deltaAverage)
return deltaAverage
def checkButtonPress(movementDiff, calibratedMovementDiff, movementRatio):
if (movementDiff < calibratedMovementDiff * movementRatio):
return 1
else:
return 0
def xDiff(curCoords, nailNumber, nuckleNumber, handNum):
if (curCoords and curCoords[handNum]):
nuckleDiff = abs(curCoords[handNum].landmark[17].y - curCoords[handNum].landmark[5].y)
calibratednuckleDiff = abs(calibratedCoords[handNum].landmark[17].y - calibratedCoords[handNum].landmark[5].y)
movementDiff = abs(curCoords[handNum].landmark[nailNumber].x - curCoords[handNum].landmark[nuckleNumber].x)
calibratedMovementDiff = abs(
calibratedCoords[handNum].landmark[nailNumber].x - calibratedCoords[handNum].landmark[
nuckleNumber].x) * nuckleDiff / calibratednuckleDiff
return (movementDiff, calibratedMovementDiff)
return (0, 0)
def yDiff(curCoords, nailNumber, nuckleNumber, handNum):
if (curCoords and curCoords[handNum]):
nuckleDiff = abs(curCoords[handNum].landmark[17].y - curCoords[handNum].landmark[5].y)
calibratednuckleDiff = abs(calibratedCoords[handNum].landmark[17].y - calibratedCoords[handNum].landmark[5].y)
movementDiff = abs(curCoords[handNum].landmark[nailNumber].y - curCoords[handNum].landmark[nuckleNumber].y)
calibratedMovementDiff = abs(
calibratedCoords[handNum].landmark[nailNumber].y - calibratedCoords[handNum].landmark[
nuckleNumber].y) * nuckleDiff / calibratednuckleDiff
return (movementDiff, calibratedMovementDiff)
return (0, 0)
def triggerPosition(movementDiff, calibratedMovementDiff):
if (not movementDiff):
return 0
ratio = movementDiff / calibratedMovementDiff
ratio -= 1
ratio /= 0.75 # scaling param that has yet to be determined
ratio = max(ratio, -1) # bound the joystick between -1 and 1
ratio = min(ratio, 1)
return ratio
controllers = []
temp_controllers = []
for i in range(numPlayers):
controllers.append(Controller())
temp_controllers.append([Controller(), Controller(), Controller()])
class LoadingSpinner():
SPINNER_CHARS = ['■ □ □ □', '□ ■ □ □', '□ □ ■ □', '□ □ □ ■', '□ □ □ ■', '□ □ ■ □', '□ ■ □ □', '■ □ □ □'] # noqa
def __init__(self):
self.creation_time = time.perf_counter()
self.last_update_time = self.creation_time
self.current_char_index = 0
def get_spinner_char(self):
current_time = time.perf_counter()
delta = current_time - self.last_update_time
if delta > 0.07:
self.last_update_time = current_time
if self.current_char_index == 7:
self.current_char_index = 0
else:
self.current_char_index += 1
return self.SPINNER_CHARS[self.current_char_index]
class ControllerTUI():
CONTROLS = {
"ZL": "◿□□□□",
"L": "◿□□□□",
"ZR": "□□□□◺",
"R": "□□□□◺",
"LS_UP": ".─.",
"LS_LEFT": "(",
"LS_RIGHT": ")",
"LS_DOWN": "`─'",
"RS_UP": ".─.",
"RS_LEFT": "(",
"RS_RIGHT": ")",
"RS_DOWN": "`─'",
"DPAD_UP": "△",
"DPAD_LEFT": "◁",
"DPAD_RIGHT": "▷",
"DPAD_DOWN": "▽",
"MINUS": "◎",
"PLUS": "◎",
"HOME": "□",
"CAPTURE": "□",
"A": "○",
"B": "○",
"X": "○",
"Y": "○",
}
def __init__(self, term):
self.term = term
# Save a copy of the controls we can restore the
# control text on deactivation
self.DEFAULT_CONTROLS = self.CONTROLS.copy()
self.CONTROL_RELEASE_TIMERS = self.CONTROLS.copy()
for control in self.CONTROL_RELEASE_TIMERS.keys():
self.CONTROL_RELEASE_TIMERS[control] = False
self.auto_keypress_deactivation = True
self.remote_connection = False
def toggle_auto_keypress_deactivation(self, toggle):
"""Toggles whether or not the ControllerTUI should deactivate
a control after a period of time.
:param toggle: A True/False value that toggles auto keypress
deactivation
:type toggle: bool
"""
self.auto_keypress_deactivation = toggle
def set_remote_connection_status(self, status):
"""Sets whether or not the controller should render
with remote connection specific controls.
:param status: The status of the remote connection
:type status: bool
"""
self.remote_connection = status
def activate_control(self, key, activated_text=None):
if activated_text:
self.CONTROLS[key] = activated_text
else:
self.CONTROLS[key] = self.term.bold_black_on_white(self.CONTROLS[key])
# Keep track of when the key was pressed so we can release later
if self.auto_keypress_deactivation:
self.CONTROL_RELEASE_TIMERS[key] = time.perf_counter()
def deactivate_control(self, key):
self.CONTROLS[key] = self.DEFAULT_CONTROLS[key]
def render_controller(self):
if self.auto_keypress_deactivation:
# Release any overdue timers
for control in self.CONTROL_RELEASE_TIMERS.keys():
pressed_time = self.CONTROL_RELEASE_TIMERS[control]
current_time = time.perf_counter()
if pressed_time is not False and current_time - pressed_time > 0.25:
self.deactivate_control(control)
ZL = self.CONTROLS['ZL']
L = self.CONTROLS['L']
ZR = self.CONTROLS['ZR']
R = self.CONTROLS['R']
LU = self.CONTROLS['LS_UP']
LL = self.CONTROLS['LS_LEFT']
LR = self.CONTROLS['LS_RIGHT']
LD = self.CONTROLS['LS_DOWN']
RU = self.CONTROLS['RS_UP']
RL = self.CONTROLS['RS_LEFT']
RR = self.CONTROLS['RS_RIGHT']
RD = self.CONTROLS['RS_DOWN']
DU = self.CONTROLS['DPAD_UP']
DL = self.CONTROLS['DPAD_LEFT']
DR = self.CONTROLS['DPAD_RIGHT']
DD = self.CONTROLS['DPAD_DOWN']
MN = self.CONTROLS['MINUS']
PL = self.CONTROLS['PLUS']
HM = self.CONTROLS['HOME']
CP = self.CONTROLS['CAPTURE']
A = self.CONTROLS['A']
B = self.CONTROLS['B']
X = self.CONTROLS['X']
Y = self.CONTROLS['Y']
if self.remote_connection:
lr_press = "L + R - - - - - - - - -▷ E"
else:
lr_press = " "
print(self.term.home + self.term.move_y((self.term.height // 2) - 9))
print(self.term.center(f" {ZL} {ZR} "))
print(self.term.center(f" ─{L}──────────{R}─ ┌─────────────┬────────────┐"))
print(self.term.center(" ╱ ╲ │ Controls │ Keys │"))
print(self.term.center(f" ╱ {LU} {MN} {PL} {X} ╲ └─────────────┴────────────┘")) # noqa
print(self.term.center(f"│ {LL} {LR} {CP} {HM} {Y} {A} │ Left Stick ─ ─ ─ ▷ W/A/S/D ")) # noqa
print(self.term.center(f"│ {LD} {B} │ DPad ─ ─ ─ ─ ─ ─ ▷ G/V/B/N "))
print(self.term.center(f"│ {DU} {RU} │ Capture/Home ─ ─ ─ ─ ▷ [/] "))
print(self.term.center(f"│╲ {DL} □ {DR} {RL} {RR} ╱│ +/- ─ ─ ─ ─ ─ ─ ─ ─ ─▷ 6/7 ")) # noqa
print(self.term.center(f"│░░╲ {DD} {RD} ╱░░│ X/Y/B/A ─ ─ ─ ─ ─▷ J/I/K/L "))
print(self.term.center("│░░░░╲ ──────────────── ╱░░░░│ L/ZL ─ ─ ─ ─ ─ ─ ─ ─ ▷ 1/2 "))
print(self.term.center("│░░░░╱ ╲░░░░│ R/ZR ─ ─ ─ ─ ─ ─ ─ ─ ▷ 8/9 "))
print(self.term.center("│░░╱ ╲░░│ Right Stick - - - ▷ Arrows "))
print(self.term.center(f"│╱ ╲│ {lr_press} "))
class InputTUI():
KEYMAP = {
# Left Stick Mapping
"w": {
"control": "LS_UP",
"stick_data": {
"stick_name": "L_STICK",
"x": "+000",
"y": "+100"
}
},
"a": {
"control": "LS_LEFT",
"stick_data": {
"stick_name": "L_STICK",
"x": "-100",
"y": "+000"
}
},
"d": {
"control": "LS_RIGHT",
"stick_data": {
"stick_name": "L_STICK",
"x": "+100",
"y": "+000"
}
},
"s": {
"control": "LS_DOWN",
"stick_data": {
"stick_name": "L_STICK",
"x": "+000",
"y": "-100"
}
},
# Right Stick Mapping
"KEY_UP": {
"control": "RS_UP",
"stick_data": {
"stick_name": "R_STICK",
"x": "+000",
"y": "+100"
}
},
"KEY_LEFT": {
"control": "RS_LEFT",
"stick_data": {
"stick_name": "R_STICK",
"x": "-100",
"y": "+000"
}
},
"KEY_RIGHT": {
"control": "RS_RIGHT",
"stick_data": {
"stick_name": "R_STICK",
"x": "+100",
"y": "+000"
}
},
"KEY_DOWN": {
"control": "RS_DOWN",
"stick_data": {
"stick_name": "R_STICK",
"x": "+000",
"y": "-100"
}
},
# Dpad Mapping
"g": "DPAD_UP",
"v": "DPAD_LEFT",
"n": "DPAD_RIGHT",
"b": "DPAD_DOWN",
# Button Mapping
"6": "MINUS",
"7": "PLUS",
"[": "CAPTURE",
"]": "HOME",
"i": "X",
"j": "Y",
"l": "A",
"k": "B",
# Triggers
"1": "L",
"2": "ZL",
"8": "R",
"9": "ZR",
}
def __init__(self, reconnect_target=None, debug=False, logfile=False, force_remote=False):
self.reconnect_target = reconnect_target
self.term = Terminal()
if force_remote:
self.remote_connection = True
else:
self.remote_connection = self.detect_remote_connection()
self.controller = ControllerTUI(self.term)
# Check if direct connection will fail
if not self.remote_connection:
try:
# from pynput import keyboard
pass
except ImportError as e:
print("Unable to import pynput for direct input.")
print("If you're accessing NXBT over a remote shell, ", end="")
print("please use the 'remote_tui' option instead of 'tui'.")
print("The original pynput import is displayed below:\n")
print(e)
exit(1)
self.debug = debug
self.logfile = logfile
def handDetectionLoop(self, cap, hands, on_press, on_release, startTime, counter):
global calibratedCoords
global arrows
global joystickPositionArr
global rt1Button
global rt2Button
global lt1Button
global lt2Button
global aButton
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
return
counter += 1
leftConroller = 0
rightConroller = 0
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# results.multi_hand_landmark stores each hand landmark x,y,z
# do calibration
curTime = datetime.datetime.now()
if ((curTime - startTime).total_seconds() < 5):
image = cv2.flip(image, 1)
cv2.putText(image, "CALIBRATING", (int(image.shape[0] // 2), int(image.shape[1] // 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 5)
image = cv2.flip(image, 1)
if (4 < (curTime - startTime).total_seconds() < 5):
calibratedCoords = results.multi_hand_landmarks
print("calibration complete")
if (calibratedCoords != None and len(calibratedCoords) > 0):
if results.multi_handedness != None:
for i in range(len(results.multi_handedness)):
if (results.multi_handedness[i].classification[0].label == "Left"):
if (leftConroller >= numPlayers):
print("oops 1")
image = cv2.flip(image, 1)
cv2.putText(image, "Please move your hands into view",
(int(image.shape[0] // 2), int(image.shape[1] // 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5)
image = cv2.flip(image, 1)
continue
try:
(rt1Movementdiff, rt1CalibratedMovementDiff) = xDiff(results.multi_hand_landmarks,
8, 5, i)
(rt2Movementdiff, rt2CalibratedMovementDiff) = xDiff(results.multi_hand_landmarks,
12, 9, i)
(aMovementdiff, aCalibratedMovementDiff) = yDiff(results.multi_hand_landmarks, 4, 5,
i)
except IndexError:
print("oops 2")
image = cv2.flip(image, 1)
cv2.putText(image, "Please move your hands into view",
(int(image.shape[0] // 2), int(image.shape[1] // 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5)
image = cv2.flip(image, 1)
continue
rt1ButtonPressed = checkButtonPress(rt1Movementdiff, rt1CalibratedMovementDiff, 0.85)
rt2ButtonPressed = checkButtonPress(rt2Movementdiff, rt2CalibratedMovementDiff, 0.87)
aButtonPressed = checkButtonPress(aMovementdiff, aCalibratedMovementDiff, 0.90)
print(leftConroller)
temp_controllers[leftConroller][counter % 3].right_trigger = rt1ButtonPressed
temp_controllers[leftConroller][counter % 3].right_bumper = rt2ButtonPressed
temp_controllers[leftConroller][counter % 3].Abutton = aButtonPressed
if (counter % 3 == 0):
controllers[leftConroller] = (temp_controllers[leftConroller][0] +
temp_controllers[leftConroller][1] +
temp_controllers[leftConroller][2]) / 3
if (rt1ButtonPressed == 1):
rt1Button += 1
# switch_controller.startRProcess()
on_press('8')
arrows.append([(130, 13), (110, 13), (0, 0, 255), 4, 0.5])
if (rt1Button < 2):
print("R1 Pressed")
else:
rt1Button = 0
on_release('8')
if (rt2ButtonPressed == 1):
rt2Button += 1
# switch_controller.startZRProcess()
on_press("9")
arrows.append([(130, 25), (110, 25), (0, 0, 255), 4, 0.5])
if (rt2Button < 2):
print("R2 Pressed")
else:
rt2Button = 0
on_release("9")
if (aButtonPressed == 1):
aButton += 1
# switch_controller.startThreadA()
on_press("l")
arrows.append([(130, 65), (110, 65), (0, 0, 255), 4, 0.5])
if (aButton < 2):
print("A Pressed")
else:
aButton = 0
on_release("l")
leftConroller += 1
elif (results.multi_handedness[i].classification[0].label == "Right"):
if (rightConroller >= numPlayers):
print("oops 3")
image = cv2.flip(image, 1)
cv2.putText(image, "Please move your hands into view",
(int(image.shape[0] // 2), int(image.shape[1] // 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5)
image = cv2.flip(image, 1)
continue
try:
(lt1Movementdiff, lt1CalibratedMovementDiff) = xDiff(results.multi_hand_landmarks,
8, 5, i)
(lt2Movementdiff, lt2CalibratedMovementDiff) = xDiff(results.multi_hand_landmarks,
12, 9, i)
m, c = xDiff(results.multi_hand_landmarks, 4, 5, i)
m_y, c_y = yDiff(results.multi_hand_landmarks, 4, 2, i)
except Exception as e:
image = cv2.flip(image, 1)
print(e)
cv2.putText(image, "Please move your hands into view",
(int(image.shape[0] // 2), int(image.shape[1] // 10)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 5)
image = cv2.flip(image, 1)
continue
lt1ButtonPressed = checkButtonPress(lt1Movementdiff, lt1CalibratedMovementDiff, 0.90)
lt2ButtonPressed = checkButtonPress(lt2Movementdiff, lt2CalibratedMovementDiff, 0.90)
joystickPosition = triggerPosition(m, c)
joystickPositionY = triggerPosition(m_y, c_y)
joystickPositionArr.append(joystickPosition)
joystickPositionArr.append(joystickPositionY)
# switch_controller.startLeftProcess(min(max(int(joystickPosition * 100), -100), 100))
if joystickPosition < -0.3:
on_press("a")
elif joystickPosition > 0.3:
on_press("d")
elif joystickPosition < 0:
on_release('a')
elif joystickPosition > 0:
on_release('d')
if joystickPositionY < -0.5:
on_press("s")
elif joystickPositionY > 0.5:
on_press("w")
elif joystickPositionY < 0:
on_release('w')
elif joystickPositionY > 0:
on_release('w')
temp_controllers[rightConroller][counter % 3].left_trigger = lt1ButtonPressed
temp_controllers[rightConroller][counter % 3].left_bumper = lt2ButtonPressed
temp_controllers[rightConroller][counter % 3].joystick = joystickPosition
temp_controllers[rightConroller][counter % 3].joystickY = joystickPositionY
if (counter % 3 == 0):
controllers[rightConroller] = (temp_controllers[rightConroller][0] +
temp_controllers[rightConroller][1] +
temp_controllers[rightConroller][2]) / 3
if (lt1ButtonPressed == 1):
lt1Button += 1
# switch_controller.startLProcess()
on_press("1")
arrows.append([(55, 13), (35, 13), (0, 0, 255), 4, 0.5])
if (lt1Button < 2):
print("L1 Pressed")
else:
lt1Button = 0
on_release("1")
if (lt2ButtonPressed == 1):
lt2Button += 1
# switch_controller.startZLProcess()
on_press("2")
arrows.append([(55, 25), (35, 25), (0, 0, 255), 4, 0.5])
if (lt2Button < 2):
print("L2 Pressed")
else:
lt2Button = 0
on_release("2")
rightConroller += 1
"""
if (controllers[0] is not None):
if(controllers[0].left_bumper == 0):
switch_controller.endZLProcess()
else:
switch_controller.startZLProcess()
if(controllers[0].right_bumper == 0):
switch_controller.endZRProcess()
else:
switch_controller.startZRProcess()
if(controllers[0].left_trigger == 0):
switch_controller.endLProcess()
else:
switch_controller.startLProcess()
if(controllers[0].right_trigger == 0):
switch_controller.endRProcess()
else:
switch_controller.startRProcess()
if(controllers[0].Abutton == 0):
switch_controller.endThreadA()
else:
switch_controller.startThreadA()
"""
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Flip the image horizontally for a selfie-view display.
image = cv2.flip(image, 1)
image[0:joycon.shape[0], 0:joycon.shape[1]] = joycon
for i in arrows:
image = cv2.arrowedLine(image, i[0], i[1], i[2], i[3], tipLength=i[4])
if (len(joystickPositionArr) > 0):
image = cv2.putText(image, "x:" + str(round(joystickPositionArr[0], 5)), (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0),
2)
#image = cv2.putText(image, "y: " + str(round(joystickPositionArr[1], 5)), (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0),
#2)
joystickPositionArr = []
arrows = []
cv2.imshow('PlugLess', image)
cv2.waitKey(1)
# cv2.destroyAllWindows()
if cv2.waitKey(5) & 0xFF == 27:
pass
def detect_remote_connection(self):
"""Traverse up the parent processes and check if any
have their parent as a remote daemon. If so, the python
script is running under a remote connection.
Remote shell detection is required for this TUI, due to
keyboard input limitations on most remote connections.
Specifically, no "keyup" events are sent when a key is
released. Keyup events are required for proper input to
the Switch, thus, we need to detect if the shell is a remote
session and workaround this.
:return: Returns a boolean value indicating whether or not
the current script is running as SSH
:rtype: bool
"""
remote_connection = False
remote_process_names = ['sshd', 'mosh-server']
ppid = os.getppid()
while ppid > 0:
process = psutil.Process(ppid)
if process.name() in remote_process_names:
remote_connection = True
break
ppid = process.ppid()
return remote_connection
def start(self):
self.mainloop(self.term)
def mainloop(self, term):
# Initializing a controller
if not self.debug:
self.nx = Nxbt(disable_logging=True)
else:
self.nx = Nxbt(debug=self.debug, logfile=self.logfile)
self.controller_index = self.nx.create_controller(
PRO_CONTROLLER,
reconnect_address=self.nx.get_switch_addresses())
state = None
spinner = LoadingSpinner()
errors = None
try:
with term.cbreak(), term.keypad(), term.location(), term.hidden_cursor():
print(term.home + term.clear)
self.render_top_bar(term)
self.render_bottom_bar(term)
self.render_start_screen(term, "Loading")
inp = term.inkey(timeout=0)
# Loading Screen
while inp != chr(113): # Checking for q press
# Check key at 15hz
inp = term.inkey(timeout=1 / 30)
new_state = self.nx.state[self.controller_index]["state"]
if new_state != state:
state = new_state
loading_text = "Loading"
if state == "initializing":
loading_text = "Initializing Controller"
elif state == "connecting":
loading_text = "Connecting to any Nintendo Switch"
elif state == "reconnecting":
loading_text = "Reconnecting to Nintendo Switch"
elif state == "connected":
loading_text = "Connected!"
elif state == "crashed":
errors = self.nx.state[self.controller_index]["errors"]
exit(1)
self.render_start_screen(term, loading_text)
print(term.move_y((term.height // 2) + 6))
if state != "connected":
print(term.bold(term.center(spinner.get_spinner_char())))
else:
print(term.center(""))
if state == "connected":
time.sleep(1)
break
# Main Gamepad Input Loop
if state == "connected":
self.direct_input_loop(term)
except KeyboardInterrupt:
pass
finally:
print(term.clear())
if errors:
print("The TUI encountered the following errors:")
print(errors)
def remote_input_loop(self, term):
self.controller.set_remote_connection_status(True)
inp = term.inkey(timeout=0)
while inp != chr(113): # Checking for q press
# Cutoff large buffered input from the deque
# so that we avoid spamming the Switch after
# a key releases from being held.
# Increasing the size of the buffer does not
# smooth out the jagginess of input.
if len(term._keyboard_buf) > 1:
term._keyboard_buf = deque([term._keyboard_buf.pop()])
inp = term.inkey(1 / 66)
pressed_key = None
if inp.is_sequence:
pressed_key = inp.name
elif inp:
pressed_key = inp
if pressed_key == 'e':
self.controller.activate_control('L')
self.controller.activate_control('R')
self.nx.macro(self.controller_index, "L R 0.1s")
else:
try:
control_data = self.KEYMAP[pressed_key]
if type(control_data) == dict and "stick_data" in control_data.keys():
x_value = control_data['stick_data']['x']
y_value = control_data['stick_data']['y']
stick_name = control_data['stick_data']['stick_name']
self.controller.activate_control(control_data["control"])
self.nx.macro(
self.controller_index,
f"{stick_name}@{x_value}{y_value} 0.1s")
else:
self.controller.activate_control(control_data)
self.nx.macro(self.controller_index, f"{control_data} 0.05s")
except KeyError:
pass
self.controller.render_controller()
self.check_for_disconnect(term)
def direct_input_loop(self, term):
self.controller.toggle_auto_keypress_deactivation(False)
self.exit_tui = False
self.capture_input = True
# Create a packet that is accessible from a multiprocessing Process
# and from within threads
packet_manager = multiprocessing.Manager()
input_packet = packet_manager.dict()
input_packet["packet"] = self.nx.create_input_packet()
print(term.move_y(term.height - 5))
print(term.center(term.bold_black_on_white(" <Press esc to toggle input capture> ")))
def on_press(key):
# Parse the key press event
pressed_key = None
try:
pressed_key = key
except AttributeError:
pressed_key = str(key).replace(".", "_").upper()
print("pressed " + pressed_key)
if not self.capture_input: # If we're not capturing input, pass
print("Not inputing")
pass
else:
try:
print("should work")
control_data = self.KEYMAP[pressed_key]
packet = input_packet["packet"]
if type(control_data) == dict and "stick_data" in control_data.keys():
stick_name = control_data['stick_data']['stick_name']
self.controller.activate_control(control_data["control"])
packet[stick_name][control_data["control"]] = True
else:
self.controller.activate_control(control_data)
packet[control_data] = True
input_packet["packet"] = packet
except KeyError:
print("Problem")
print("Error with " + pressed_key)
def on_release(key):
# Parse the key release event
released_key = None
try:
released_key = key
except AttributeError:
released_key = str(key).replace(".", "_").upper()
# If the esc key is released, toggle input capturing
if released_key == "KEY_ESC":
self.capture_input = not self.capture_input
# Exit on q key press
if released_key == 'q':
self.exit_tui = True
return False
if not self.capture_input: # If we're not capturing input, pass
pass
else:
try:
control_data = self.KEYMAP[released_key]
packet = input_packet["packet"]
if type(control_data) == dict and "stick_data" in control_data.keys():
stick_name = control_data['stick_data']['stick_name']
self.controller.deactivate_control(control_data["control"])
packet[stick_name][control_data["control"]] = False
else:
self.controller.deactivate_control(control_data)
packet[control_data] = False
input_packet["packet"] = packet
except KeyError:
pass
def input_worker(nxbt, controller_index, input_packet):
while True:
packet = input_packet["packet"]
# Calculating left x/y stick values
ls_x_value = 0
ls_y_value = 0
if packet["L_STICK"]["LS_LEFT"]:
ls_x_value -= 100
if packet["L_STICK"]["LS_RIGHT"]:
ls_x_value += 100
if packet["L_STICK"]["LS_UP"]:
ls_y_value += 100
if packet["L_STICK"]["LS_DOWN"]:
ls_y_value -= 100
packet["L_STICK"]["X_VALUE"] = ls_x_value
packet["L_STICK"]["Y_VALUE"] = ls_y_value
# Calculating right x/y stick values
rs_x_value = 0
rs_y_value = 0
if packet["R_STICK"]["RS_LEFT"]:
rs_x_value -= 100
if packet["R_STICK"]["RS_RIGHT"]:
rs_x_value += 100
if packet["R_STICK"]["RS_UP"]:
rs_y_value += 100
if packet["R_STICK"]["RS_DOWN"]:
rs_y_value -= 100
packet["R_STICK"]["X_VALUE"] = rs_x_value
packet["R_STICK"]["Y_VALUE"] = rs_y_value
nxbt.set_controller_input(controller_index, packet)
time.sleep(1 / 120)
input_process = multiprocessing.Process(
target=input_worker, args=(self.nx, self.controller_index, input_packet))
input_process.start()
startTime = datetime.datetime.now()
counter = 0
with mp_hands.Hands(
model_complexity=1,
max_num_hands=4,
min_detection_confidence=0.5,
min_tracking_confidence=0.7) as hands:
while cap.isOpened():
self.handDetectionLoop(cap, hands, on_press, on_release, startTime, counter)
cap.release()
# Main TUI Loop
"""while True:
if self.exit_tui:
packet_manager.shutdown()
input_process.terminate()
break
if not self.capture_input:
print(term.home + term.move_y((term.height // 2) - 4))
print(term.bold_black_on_white(term.center("")))
print(term.bold_black_on_white(term.center(
"<Input Paused. Press ESC Again to Begin Capturing Input>"
)))
print(term.bold_black_on_white(term.center("")))