]> Pileus Git - ~andy/csm213a-hw/blob - vis/device.py
Add serial interface
[~andy/csm213a-hw] / vis / device.py
1 from re     import compile
2 from serial import Serial
3
4 class State:
5         acc   = [None]*3
6         mag   = [None]*3
7         touch = [None]*2
8         light = [None]*1
9         a2d   = [None]*6
10
11         def __init__(self):
12                 pass
13
14 class Device:
15         # Attributes
16         port = "/dev/ttyACM0"
17
18         # Constructors
19         def __init__(self, config):
20                 self.config = config
21                 self.serial = None
22
23         # Methods
24         def connect(self):
25                 try:
26                         self.inbuf  = []
27                         self.serial = Serial(self.port,          \
28                                 baudrate = self.config.baudrate, \
29                                 parity   = self.config.parity,   \
30                                 bytesize = self.config.databits, \
31                                 stopbits = self.config.stopbits, \
32                                 timeout  = 0)
33                 except Exception as ex:
34                         return str(ex)
35
36         def disconnect(self):
37                 if self.serial and self.serial.isOpen():
38                         self.serial.close()
39
40         def running(self):
41                 if self.serial == None:
42                         return False
43                 if self.serial.isOpen() == False:
44                         return False
45                 return True
46
47         def process(self):
48                 items = []
49                 while self.serial.readable():
50                         try:
51                                 char = self.serial.read()
52                         except Exception as ex:
53                                 char = ''
54                         if char == '':
55                                 break
56                         if char == '\r' or char == '\n':
57                                 if len(self.inbuf) == 0:
58                                         continue
59                                 line = "".join(self.inbuf)
60                                 item = self._parse_ascii(line)
61                                 items.append(item)
62                                 self.inbuf = []
63                         else:
64                                 self.inbuf.append(char)
65                 return items
66
67         # Private methods
68         def _parse_ascii(self, line):
69                 acc_re = compile("\[ACC\] accX=(.*) accY=(.*) accZ=(.*)")
70                 mag_re = compile("\[MAG\] magX=(.*) magY=(.*) magZ=(.*)")
71                 lgt_re = compile("\[LGT\] Light Intensity=(.*)")
72                 tch_re = compile("\[TCH\] Force=(.*) Distance=(.*)")
73                 a2d_re = compile("\[A2D\] ...")
74
75                 acc_m = acc_re.match(line)
76                 mag_m = mag_re.match(line)
77                 lgt_m = lgt_re.match(line)
78                 tch_m = tch_re.match(line)
79                 a2d_m = a2d_re.match(line)
80
81                 state = State()
82                 if acc_m:
83                         state.acc[0]   = float(acc_m.group(1))
84                         state.acc[1]   = float(acc_m.group(2))
85                         state.acc[2]   = float(acc_m.group(3))
86                 if mag_m:              
87                         state.acc[0]   = float(mag_m.group(1))
88                         state.acc[1]   = float(mag_m.group(2))
89                         state.acc[2]   = float(mag_m.group(3))
90                 if lgt_m:
91                         state.light[0] = float(lgt_m.group(1))
92                 if tch_m:
93                         state.touch[0] = float(tch_m.group(1))
94                 if a2d_m:
95                         state.a2d[0]   = float(tch_m.group(1))
96                         state.a2d[1]   = float(tch_m.group(2))
97                         state.a2d[2]   = float(tch_m.group(3))
98                         state.a2d[3]   = float(tch_m.group(4))
99                         state.a2d[4]   = float(tch_m.group(5))
100                         state.a2d[5]   = float(tch_m.group(6))
101
102                 return state