-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
159 lines (148 loc) · 5.15 KB
/
controller.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
import sys
import serial
import math
serial = serial.Serial()
serial.close()
def bitstring_to_bytes(s):
return int(s, 2).to_bytes(len(s) // 8, byteorder='big')
def Main(args):
try:
buff = ''
if len(args) != 2:
return 1
serial.port = args[1]
serial.baudrate = 115200
serial.timeout = 10
serial.open()
framecount = 0
while True:
buff = input()
if(buff == 'START'):
#Set everything to zero to start
framecount += 1
b = 0
y = 0
select = 0
start = 0
up = 0
down = 0
left = 0
right = 0
a = 0
x = 0
lb = 0
rb = 0
home = 0
app = 0
click = 0
volm = 0
buff = input()
mode = int(buff)
if(mode == 1):
buff = input()
#<crapcode>
if(buff == ''):
buf = 0
else:
buf = float(buff)
if(buf < -0.55):
left = 1
right = 0
elif(buf < -0.35):
left = 1 if (framecount % 3 != 0) else 0
right = 0
elif(buf < -0.20):
left = 1 if ((framecount % 5) % 2 == 0) else 0
right = 0
elif(buf < -0.08):
left = 1 if (framecount % 2 == 0) else 0
right = 0
elif(buf > 0.55):
right = 1
left = 0
elif(buf > 0.35):
right = 1 if (framecount % 3 != 0) else 0
left = 0
elif(buf > 0.20):
right = 1 if ((framecount % 5) % 2 == 0) else 0
left = 0
elif(buf > 0.08):
right = 1 if (framecount % 2 == 0) else 0
left = 0
#</crapcode>
buff = input()
if (buff): home = 1
up = home
buff = input()
if (buff): app = 1
down = app
if(home or app): a = 1
buff = input()
if(buff): click = 1
lb = click
buff = input()
if(buff): volm = 1
start = volm
#<crapcode2.0>
buff = input()
if(buff == ''):
buf = 0
else:
buf = int(buff)
if(buf < 128 and buf > 0):
y = 1
b = 0
elif(buf > 128):
b = 1
y = 0
else:
b = 0
y = 0
buff = input()
pass
#</crapcode2.0>
if(mode == 2):
buff = input()
pass
buff = input()
if(buff): home = 1
b = home
buff = input()
if(buff): app = 1
x = app
buff = input()
pass
buff = input()
pass
buff = input()
xTouch = int(buff)-128 if buff else 0
buff = input()
yTouch = int(buff)-128 if buff else 0
#Do the math to turn the touchpad to a d pad
if(xTouch != 0 and yTouch != 0 and (math.sqrt(xTouch ** 2 + yTouch ** 2))>55):
angle = (math.atan2(xTouch, yTouch)*360/6.28) +180
if(angle < 45 or angle > 315):
left = 1
elif(angle > 45 and angle < 135):
down = 1
elif(angle > 135 and angle < 225):
right = 1
elif(angle > 225 and angle < 315):
up = 1
buff = ''
controllerData = str(b)+str(y)+str(select)+str(start)+str(up)+str(down)+str(left)+str(right)+str(a)+str(x)+str(lb)+str(rb)+'0000'
contDataBytes = bitstring_to_bytes(controllerData)
print(contDataBytes)
serial.reset_output_buffer()
serial.write(contDataBytes)
echo = serial.read(len(contDataBytes))
if len(echo) != len(contDataBytes):
print("BAD!")
#print(controllerData)
except KeyboardInterrupt:
sys.stdout.flush()
serial.close()
pass
print("DONE")
if __name__ == "__main__":
sys.exit(Main(sys.argv))