-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino_joystick.py
45 lines (34 loc) · 1022 Bytes
/
arduino_joystick.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
from sys import argv
from time import sleep
from serial import Serial
from struct import unpack
class SIO(object):
MSGLEN = 11
M1 = 0xBEEF
M2 = 0xF00D
def __init__(self, serial_port):
self.ser = Serial(serial_port)
self.count = 0
self.prev = bytes((0,))
def get_msg(self):
self.ser.reset_input_buffer()
while True:
curr = self.ser.read()
(m,) = unpack("<H", self.prev + curr)
if m != self.M1:
self.prev = curr
continue
msg_bytes = self.ser.read(self.MSGLEN)
(x, y, mode, m) = unpack("<ffBH", msg_bytes)
if m == self.M2:
print("R:(%f,%f),%d" % (x, y, mode))
else:
print("Invalid magic on packet %d" % self.count)
self.prev = self.ser.read()
self.count += 1
break
if __name__ == "__main__":
sio = SIO(argv[1])
while True:
sio.get_msg()
sleep(1)