]> Pileus Git - ~andy/csm213a-hw/commitdiff
Work on writing settings to devices
authorAndy Spencer <andy753421@gmail.com>
Tue, 4 Feb 2014 00:52:37 +0000 (00:52 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 4 Feb 2014 00:52:37 +0000 (00:52 +0000)
vis/device.py

index 24860bac33c430cd2aef444208d9bdd2dad08700..a8963751975191bed1a7294d589cbe2d5d5f6383 100644 (file)
@@ -1,3 +1,5 @@
+import time
+
 from re       import compile
 from serial   import Serial
 from datetime import datetime
@@ -32,6 +34,7 @@ class Device:
                                bytesize = self.config.databits, \
                                stopbits = self.config.stopbits, \
                                timeout  = 0)
+                       self.control()
                        self.serial.flushInput()
                except Exception as ex:
                        return str(ex)
@@ -48,7 +51,12 @@ class Device:
                return True
 
        def control(self):
-               pass
+               for key in list(self.config.enable.keys()):
+                       state = self.config.enable[key]
+                       rate  = self.config.rate[key]
+                       cmd   = 'start' if state else 'stop'
+                       self._write_ascii('%s %s' % (cmd, key))
+                       self._write_ascii('set %s int %d' % (key, rate))
 
        def process(self):
                items = []
@@ -67,6 +75,7 @@ class Device:
                                if len(self.inbuf) == 0:
                                        continue
                                line = "".join(self.inbuf)
+                               print('read: [' + line + ']')
                                item = self._parse_ascii(line)
                                items.append(item)
                                self.inbuf = []
@@ -79,6 +88,14 @@ class Device:
                return items
 
        # Private methods
+       def _write_ascii(self, line):
+               if self.serial:
+                       print('write: [' + line + ']')
+                       data = bytes(line+'\n', 'UTF-8')
+                       self.serial.write(data)
+                       self.serial.flush()
+                       time.sleep(0.1)
+
        def _parse_ascii(self, line):
                acc_re = compile("\[ACC\] accX=(.*) accY=(.*) accZ=(.*)")
                mag_re = compile("\[MAG\] magX=(.*) magY=(.*) magZ=(.*)")