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