-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.py
63 lines (58 loc) · 1.33 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
import serial
import pydirectinput
arduino = serial.Serial('COM5', 9600, timeout =.1)
pydirectinput.PAUSE = 0
keysDown = {}
def keyDown(key):
keysDown[key] = True
pydirectinput.keyDown(key)
def keyUp(key):
if key in keysDown:
del (keysDown[key])
pydirectinput.keyUp(key)
def handleJoyStickAsArrowKeys(x,y,bx,by,bz,be):
if y == 2:
keyDown('up')
keyUp('down')
elif y == 0:
keyDown('down')
keyUp('up')
else:
keyUp('up')
keyUp('down')
if x == 0:
keyDown('right')
keyUp('left')
elif x == 2:
keyDown('left')
keyUp('right')
else:
keyUp('left')
keyUp('right')
if bx == 1:
keyDown('q')
else:
keyUp('q')
if by == 1:
keyDown('b')
else:
keyUp('b')
if bz == 1:
keyDown('d')
else:
keyUp('d')
if be == 1:
keyDown('e')
else:
keyUp('e')
while True:
rawdata = arduino.readline()
data = str(rawdata.decode('utf-8'))
if data.startswith("S"):
dx = int(data[1])
dy = int(data[3])
bx = int(data[5])
by = int(data[7])
bz = int(data[9])
be = int(data[11])
handleJoyStickAsArrowKeys(dx, dy, bx, by, bz, be)