]> Pileus Git - ~andy/csm213a-hw/blobdiff - vis/visual.py
Add serial interface
[~andy/csm213a-hw] / vis / visual.py
index 908304d908da2b374d93211d51c1982fbe2db559..f07ea0d11c2b0a4858803892992c997f60392c01 100644 (file)
@@ -5,6 +5,10 @@ import gtk
 
 class Visual:
        def __init__(self, config, device, xively):
+               def get_objects(names):
+                       return map(lambda x:
+                               self.builder.get_object(x), names)
+
                self.config   = config
                self.device   = device
                self.xively   = xively
@@ -15,6 +19,12 @@ class Visual:
                self.window   = self.builder.get_object("window")
                self.settings = self.builder.get_object("settings")
 
+               self.accs     = get_objects(['accx', 'accy', 'accz'])
+               self.mags     = get_objects(['magx', 'magy', 'magz'])
+               self.touch    = get_objects(['touch'])
+               self.light    = get_objects(['light'])
+               self.a2ds     = get_objects(['a2d0', 'a2d1', 'a2d2', 'a2d3', 'a2d4', 'a2d5'])
+
        # Signal handlers
        def on_hide(self, win, *args):
                self.settings.hide()
@@ -24,18 +34,24 @@ class Visual:
                self.settings.show()
                return True
 
-       def on_serial(self, win):
-               self.device.connect()
+       def on_serial(self, act):
+               if act.get_active():
+                       self.status(self.device.connect())
+               else:
+                       self.status(self.device.disconnect())
                return True
 
-       def on_xively(self, win):
-               self.xively.connect()
+       def on_xively(self, act):
+               if act.get_active():
+                       self.status(self.xively.connect())
+               else:
+                       self.status(self.xively.disconnect())
                return True
 
        def on_enable(self, _):
                print "Unimplemented: on_enable"
                return True
-       
+
        def on_rate(self, _):
                print "Unimplemented: on_rate"
                return True
@@ -69,7 +85,56 @@ class Visual:
 
                # TODO - send changes back to config
 
+       def update(self, state):
+               def setxyz(objs, vals):
+                       if vals[0]: objs[0].set_text('X: %f' % vals[0])
+                       if vals[1]: objs[1].set_text('Y: %f' % vals[1])
+                       if vals[2]: objs[2].set_text('Z: %f' % vals[2])
+               def setabs(objs, vals):
+                       for i in range(0,len(objs)):
+                               if vals[i]: objs[i].set_text('%f' % vals[i])
+
+               setxyz(self.accs,  state.acc)
+               setxyz(self.mags,  state.mag)
+               setabs(self.touch, state.touch)
+               setabs(self.light, state.light)
+               setabs(self.a2ds,  state.a2d)
+
+       def debug(self, state):
+               print 'update: ' + str(state)
+               print '\tacc - ' + str(state.acc)
+               print '\tmag - ' + str(state.mag)
+               print '\tlgt - ' + str(state.light)
+               print '\ttch - ' + str(state.touch)
+               print '\ta2d - ' + str(state.a2d)
+       
+       def timer(self):
+               serial = self.builder.get_object("serial_btn")
+               status = self.builder.get_object("conn")
+
+               if self.device.running():
+                       serial.set_active(True)
+                       status.set_from_stock(gtk.STOCK_YES, gtk.ICON_SIZE_BUTTON)
+               else:
+                       serial.set_active(False)
+                       status.set_from_stock(gtk.STOCK_NO,  gtk.ICON_SIZE_BUTTON)
+               if self.device.running():
+                       for item in self.device.process():
+                               self.update(item)
+                               #self.debug(item)
+
+               return True
+
        def run(self):
                self.load_config()
                self.window.show()
+               gtk.timeout_add(1000/60, self.timer)
                gtk.main()
+
+       # Private methods
+       def status(self, msg):
+               if not msg:
+                       return
+               status = self.builder.get_object("status")
+               status.push(text=('Error: ' + msg), context_id=0)
+