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