]> Pileus Git - ~andy/csm213a-hw/blob - vis/device.py
ba4678709df6b8872c022b805e9f490b5c79171d
[~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                 except Exception as ex:
36                         return str(ex)
37
38         def disconnect(self):
39                 if self.serial and self.serial.isOpen():
40                         self.serial.close()
41
42         def running(self):
43                 if self.serial == None:
44                         return False
45                 if self.serial.isOpen() == False:
46                         return False
47                 return True
48
49         def process(self):
50                 items = []
51                 count = 0
52                 limit = 100
53                 if not self.running():
54                         return items
55                 while self.serial.readable():
56                         try:
57                                 char = self.serial.read().decode()
58                         except Exception as ex:
59                                 char = ''
60                         if len(char) == 0:
61                                 break
62                         if char == '\r' or char == '\n':
63                                 if len(self.inbuf) == 0:
64                                         continue
65                                 line = "".join(self.inbuf)
66                                 item = self._parse_ascii(line)
67                                 items.append(item)
68                                 self.inbuf = []
69                         else:
70                                 self.inbuf.append(char)
71                         if count > limit:
72                                 print("Eror: exceeded read limit")
73                                 break
74                         count += 1
75                 return items
76
77         # Private methods
78         def _parse_ascii(self, line):
79                 acc_re = compile("\[ACC\] accX=(.*) accY=(.*) accZ=(.*)")
80                 mag_re = compile("\[MAG\] magX=(.*) magY=(.*) magZ=(.*)")
81                 lgt_re = compile("\[LGT\] Light Intensity=(.*)")
82                 tch_re = compile("\[TCH\] Force=(.*) Distance=(.*)")
83                 a2d_re = compile("\[A2D\] ...")
84
85                 acc_m = acc_re.match(line)
86                 mag_m = mag_re.match(line)
87                 lgt_m = lgt_re.match(line)
88                 tch_m = tch_re.match(line)
89                 a2d_m = a2d_re.match(line)
90
91                 state = State()
92                 if acc_m:
93                         state.acc[0]   = float(acc_m.group(1))
94                         state.acc[1]   = float(acc_m.group(2))
95                         state.acc[2]   = float(acc_m.group(3))
96                 if mag_m:              
97                         state.acc[0]   = float(mag_m.group(1))
98                         state.acc[1]   = float(mag_m.group(2))
99                         state.acc[2]   = float(mag_m.group(3))
100                 if lgt_m:
101                         state.light[0] = float(lgt_m.group(1))
102                 if tch_m:
103                         state.touch[0] = float(tch_m.group(1))
104                 if a2d_m:
105                         state.a2d[0]   = float(tch_m.group(1))
106                         state.a2d[1]   = float(tch_m.group(2))
107                         state.a2d[2]   = float(tch_m.group(3))
108                         state.a2d[3]   = float(tch_m.group(4))
109                         state.a2d[4]   = float(tch_m.group(5))
110                         state.a2d[5]   = float(tch_m.group(6))
111
112                 return state