-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
320 lines (279 loc) · 7.49 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
from numpy import interp
import pygame ,sys
from time import sleep
import serial
global ser
# ser = serial.Serial('/dev/ttyACM0',115200)
ser = serial.Serial()
ser.baudrate = 115200
ser.timeout=1
ser.port = "/dev/ttyUSB0"
#ser.port = 'COM17'
ser.open()
sleep(1)
#global hat_up
hat_up = 0
#global hat_down
hat_down = 0
#global hat_left
hat_left = 0
#global hat_right
hat_right = 0
#global up
up = 0
#global down
down = 0
#global left
left = 0
#global right
right = 0
#global start
start = 0
#global back
back = 0
#global front_left
front_left = 0
#global front_right
front_right = 0
#global left_x
left_x = 0
#global left_y
left_y = 0
#global right_x
right_x = 0
#global right_y
right_y = 0
#global lt1
lt1 = 0
#global rt1
rt1 = 0
left_x_dir = 1
left_y_dir = 1
right_x_dir = 1
right_y_dir = 1
def serial_print():
#message=str(hat_up)+str(hat_down)+str(hat_left)+str(hat_right)+str(up)+str(down)+str(left)+str(right)+str(start)+str(back)+str(front_left)+str(front_right)+str(left_x)+str(left_y)+str(right_x)+str(right_y)+str(lt1)+str(rt1)
global s
s = ("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s")%(hat_up,hat_down,hat_left,hat_right,up,down,left,right,start,back,front_left,front_right,left_x,left_y,right_x,right_y,lt1,rt1,left_x_dir,left_y_dir,right_x_dir,right_y_dir)
# ser.write("1".encode())
# print(s.encode())
ser.write(s.encode())
# print(s.encode())
print(s)
# print(s.encode())
# sleep(0.5)
#s = "%s" %(message)
#print(len(message))
#ser.write(s.encode())
#def rangeMap(number):
# return int(interp(number,[-1.0, 1.0] , [0,9]))
def rangeMap(value):
istart = -1.0
istop = 0.99
ostart = -9
ostop = 9
val = ostart + (ostop - ostart) * ((value - istart) / (istop - istart))
return int(val)
# setting up pygame window
pygame.init()
window = pygame.display.set_mode((200,200),0,32)
# Joysticks connected to the computer
joystick_count = pygame.joystick.get_count()
print ("There is " + str(joystick_count) + " joystick/s")
if joystick_count == 0:
# if no joysticks, quit program safely
print ("Error, I did not find any joysticks")
pygame.quit()
sys.exit()
else:
# initialise joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
axes = joystick.get_numaxes()
buttons = joystick.get_numbuttons()
hats = joystick.get_numhats()
print( "There is " + str(axes) + " axes")
print("There is " + str(buttons) + " button/s")
print("There is " + str(hats) + " hat/s")
def clear_all_axis():
global left_x_dir
left_x_dir = 1
global right_x_dir
right_x_dir = 1
global left_y_dir
left_y_dir = 1
global right_y_dir
right_y_dir = 1
global left_x
left_x = 0
global left_y
left_y = 0
global right_x
right_x = 0
global right_y
right_y = 0
global lt1
lt1 = 0
global rt1
rt1 = 0
def clear_all_buttons():
global up
up = 0
global down
down = 0
global left
left = 0
global right
right = 0
global start
start = 0
global back
back = 0
global front_left
front_left = 0
global front_right
front_right = 0
def clear_all_hats():
global hat_up
hat_up = 0
global hat_down
hat_down = 0
global hat_left
hat_left = 0
global hat_right
hat_right = 0
def getAxis(number):
# when nothing is moved on an axis, the VALUE IS NOT EXACTLY ZERO
# so this is used not "if joystick value not zero"
#print(number)
if joystick.get_axis(number) < -.1 or joystick.get_axis(number) > .1:
global left_x_dir
if number is 0 :
print(number)
global left_x
left_x = ((rangeMap(joystick.get_axis(number))))
if(left_x < 0):
left_x_dir = 0
if(left_x > 0):
left_x_dir = 2
left_x = abs((rangeMap(joystick.get_axis(number))))
if number is 1:
#print(number)
global left_y,left_y_dir
left_y = ((rangeMap(joystick.get_axis(number))))
if(left_y < 0):
#global left_y_dir
left_y_dir = 0
if(left_y > 0) :
#global left_y_dir
left_y_dir = 2
left_y = abs((rangeMap(joystick.get_axis(number))))
if number is 3:
#print(number)
global right_y
right_y = ((rangeMap(joystick.get_axis(number))))
right_y = abs((rangeMap(joystick.get_axis(number))))
if number is 4:
#print(number)
global right_x,right_x_dir
right_x = ((rangeMap(joystick.get_axis(number))))
if(right_x < 0):
global right_x_dir
right_x_dir = 0
if(right_x > 0):
#global right_x_dir
right_x_dir = 2
right_x = abs((rangeMap(joystick.get_axis(number))))
if (number is 2 ) and (joystick.get_axis(number) > 0):
#print(number)
global lt1,right_y_dir
lt1 = ((rangeMap(joystick.get_axis(number))))
right_y_dir = 0
lt1 = abs((rangeMap(joystick.get_axis(number))))
if (number is 2 ) and (joystick.get_axis(number) < 0):
#print(number)
global rt1,right_y_dir
rt1 = abs((rangeMap(joystick.get_axis(number))))
right_y_dir = 2
rt1 = abs((rangeMap(joystick.get_axis(number))))
#else:
# clear_all_axis()
#.999969482421875
# -1.0
def getButton(number):
# returns 1 or 0 - pressed or not
if joystick.get_button(number):
# just prints id of button
#print("Button ID is %s" %(number))
if number is 0:
global down
down = 1
if number is 1:
global right
right = 1
if number is 2:
global left
left = 1
if number is 3:
global up
up = 1
if number is 4:
global front_left
front_left = 1
if number is 5:
global front_right
front_right = 1
if number is 6:
global back
back = 1
if number is 7:
global start
start = 1
#else:
# clear_all_buttons()
def getHat(number):
if joystick.get_hat(number) != (0,0):
# returns tuple with values either 1, 0 or -1
#print("Hat value is %s, %s" %(joystick.get_hat(number)[0],joystick.get_hat(number)[1]))
#print ("Hat ID is %s" %(number))
if (joystick.get_hat(number)[0] is 0 ) and (joystick.get_hat(number)[1] is 1):
global hat_up
hat_up = 1
if (joystick.get_hat(number)[0] is 0 ) and (joystick.get_hat(number)[1] is -1):
global hat_down
hat_down = 1
if (joystick.get_hat(number)[0] is -1 ) and (joystick.get_hat(number)[1] is 0):
global hat_left
hat_left = 1
if (joystick.get_hat(number)[0] is 1 ) and (joystick.get_hat(number)[1] is 0):
global hat_right
hat_right = 1
#clear_all_hats()
while True:
for event in pygame.event.get():
# loop through events, if window shut down, quit program
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if axes != 0:
for i in range(axes):
getAxis(i)
if buttons != 0:
for i in range(buttons):
getButton(i)
if hats != 0:
for i in range(hats):
getHat(i)
# ser.flush()
# ser.write("1".encode())
serial_print()
#ser.flush()
clear_all_buttons()
clear_all_hats()
clear_all_axis()
#print(ser.read())
# print(s.encode())
#int(ser.read())
sleep(1)
# 14 pin error on board
##