Commits

Craig Swank committed 1259dc9 Merge

Merge branch 'master' of bitbucket.org:cswank/gadgets

Comments (0)

Files changed (6)

examples/greenhouse.py

     """
 
     _siphons = {
-        'bed one siphon open': 100,
-        'bed two siphon open': 150,
-        'bed three siphon open': 135
+        'bed one siphon': 135,
+        'bed two siphon': 150,
+        'bed three siphon': 135
         }
 
     def event_received(self, event, message):
-        siphon = self._get_siphon(event)
-        if siphon != '':
-            self._status[siphon] = True
-            self.sockets.send('turn off {0} valve'.format(siphon))
-            if self._all_siphons_running:
+        sender = message['sender']
+        if sender in self._siphons:
+            location = sender.replace(' siphon', '')
+            self.sockets.send('turn off {0} valve'.format(location))
+            if self._all_valves_closed(message):
                 self.sockets.send('turn off fish tank pump')
-            self._start_timer(siphon)
+            self._start_timer(sender)
             
     @property
     def events(self):
-        return self._siphons.keys()
+        return ['UPDATE']
 
     def on_start(self):
-        self._status = self._initial_status
         self.sockets.send('turn on bed one valve')
         self.sockets.send('turn on bed two valve')
         self.sockets.send('turn on bed three valve')
         self.sockets.send('turn on fish tank pump')
 
-    def _start_timer(self, siphon):
-        countdown = self._siphons['{0} siphon open'.format(siphon)]
-        print 'countdown', countdown
-        t = threading.Thread(target=self._siphon_finished, args=(siphon, countdown))
+    def _start_timer(self, location):
+        countdown = self._siphons['{0} siphon'.format(location)]
+        t = threading.Thread(target=self._siphon_finished, args=(location, countdown))
         t.start()
 
-    def _siphon_finished(self, siphon, countdown):
-        print siphon, countdown
+    def _siphon_finished(self, location, countdown):
         time.sleep(countdown)
         s = self._get_sockets()
-        self._status[siphon] = False
-        msg = 'turn on {0} valve'.format(siphon)
-        print msg
+        msg = 'turn on {0} valve'.format(location)
         s.send(msg)
         time.sleep(0.5)
         s.send('turn on fish tank pump')
         s.close()
 
-    def _get_siphon(self, event):
+    def _all_valves_closed(self, message):
         """
         >>> addresses = Addresses()
         >>> siphons = Siphons('na', 'siphons monitor', addresses)
-        >>> siphons._get_siphon('bed two siphon open')
-        'bed two'
-        >>> siphons._get_siphon('bed one siphon open')
-        'bed one'
+        
         """
-        parts = event.split(' ')
-        if len(parts) == 4 and parts[0] == 'bed' and parts[1] in ['one', 'two', 'three']:
-            return ' '.join([parts[0], parts[1]])
-        return ''
-
-    @property
-    def _initial_status(self):
-        """
-        >>> addresses = Addresses()
-        >>> siphons = Siphons('na', 'siphons monitor', addresses)
-        >>> siphons._initial_status
-        {'bed three': False, 'bed one': False, 'bed two': False}
-        """
-        return dict([(self._get_siphon(siphon), False) for siphon in self._siphons.keys()])
-
-    @property
-    def _all_siphons_running(self):
-        """
-        >>> addresses = Addresses()
-        >>> siphons = Siphons('na', 'siphons monitor', addresses)
-        >>> siphons._status = {'bed one': False, 'bed two': False}
-        >>> siphons._all_siphons_running
-        False
-        >>> siphons._status = {'bed one': True, 'bed two': False}
-        >>> siphons._all_siphons_running
-        False
-        >>> siphons._status = {'bed one': True, 'bed two': True}
-        >>> siphons._all_siphons_running
-        True
-        """
-        for key, value in self._status.iteritems():
-            if not value:
+        for siphon in self._siphons:
+            location = siphon.replace(' siphon', '')
+            val = message.get('locations', {}).get(location, {}).get('output', {}).get('valve', {}).get('value')
+            if val:
                 return False
         return True
 

examples/recorder.py

     If one would like to record the goings on of a gadgets system with
     pymongo, start this and record away.
 
+    NOTE:  be sure to configure the Addresses to point to the gadgets system that you wish to record.
     """
 
     events = ['UPDATE']
         pass
             
 if __name__ == '__main__':
-    addresses = Addresses(host="greenhouse") #configures this gadgets system as a remote, instead of the master
-    recorder = Recorder('greenhouse', 'recorder', addresses)
+    location = 'greenhouse'
+    addresses = Addresses(host=location) #configures this gadgets system as a remote, instead of the master
+    recorder = Recorder(location, 'recorder', addresses)
     gadgets = Gadgets([recorder], addresses)
     gadgets.start()

gadgets/coordinator.py

             self._update_method(message)
         else:
             self._update_location(message)
+            self._state['sender'] = message.get('sender')
         self.sockets.send('UPDATE', self._state)
         
     def _is_valid_update_message(self, message):

gadgets/io/gpio.py

         pullup: if True, configure the gpio to use the
         internal pullup resistor 
         """
-        self._mux = pin['mux']
+        self._mux = pin.get('mux')
         self._export = pin['export']
         self._direction = direction
         self._pullup = pullup
             val |= bit << i
         return hex(val)[2:]
 
-    def _initialize_pin(self):
+
+    def _sysfs_init(self):
         """
         Sets the mux value to gpio then enables the
         gpio sysfs inteface by writing the export name
         to the export file.
         """
+        self._write_to_file(self.mux_path, self._get_mux())
+        
+    def _initialize_pin(self):
+        """
+        determine whether to use the old sysfs
+        to set the pin mode or to use the new
+        device tree overlay
+        """
         if self._mux:
-            self._write_to_file(self.mux_path, self._get_mux())
+            self._sysfs_init()
+        else:
+            self._device_tree_init()
         if not os.path.exists(self.home_dir):
             self._write_to_file(self._export_path, str(self._export))
         if not os.path.exists(self.home_dir):
                     self.home_dir))
         path = self._base_path.format(self._export, 'direction')
         self._write_to_file(path, self._direction)
+
+
+    def _device_tree_init(self):
+        """
+        """
+        pass
+
+          
+        
+        

gadgets/pins/beaglebone_black.py

+pins = {
+    'adc': {
+        9: {
+            35:{'name': 'ain7'},
+            36:{'name': 'ain6'},
+            33:{'name': 'ain5'},
+            37:{'name': 'ain3'},
+            38:{'name': 'ain4'},
+            39:{'name': 'ain1'},
+            40:{'name': 'ain2'},
+        },
+    },
+    'pwm': {
+        8: {
+            13:{
+                'directory': 'ehrpwm.2:1',#ehrpwm2B
+                'mode':4
+                },
+            19: {
+                'directory': 'ehrpwm.2:0',#ehrpwm2A
+                'mode':4
+                },
+            },
+        9: {
+            14: {
+                'directory': 'ehrpwm.1:0', #ehrpwm1A_mux1
+                'mode': 6
+            },
+            16:{
+                'directory': 'ehrpwm.1:1',
+                'mode': 6
+            },
+            21:{
+                'directory': 'ehrpwm.0:1',
+                'mode': 3
+            },
+            22:{
+                'directory': 'ehrpwm.0:0',
+                'mode': 3
+            },
+        },
+    },
+    'gpio': {
+        8: {
+            10: {
+                'export':68
+            },
+            26:  {
+                'export':61
+            },
+        },
+        9: {
+        }
+    },
+}

gadgets/ui/dialogs/command_mode.py

 
     def _get_command_mode(self):
         return {
-            'sequence':'',
             'keys': [],
+            'i': -1
         }
 
     @property
         if x == KEYCODE_ESC:
             self._command_mode = self._get_command_mode()
             self.is_active = False
-        elif len(self._command_mode['keys']) < 2:
-            self._update_command_mode(x)
         elif len(self._command_mode['keys']) == 2 and x == KEYCODE_ENTER:
             self._do_command()
         elif len(self._command_mode['keys']) == 2 and x == ord('a'):
             argument = self._get_argument()
             if argument:
                 self._do_command(argument)
+        else:
+            self._update_command_mode(x)
         
     def _get_argument(self):
         self._lock.acquire()
     def _do_command(self, argument={}):
         keys = self._command_mode['keys']
         d = self._parent._data['locations']
-        f = open('command.txt', 'w')
-        f.write(str(keys))
-        f.write('\n')
-        f.write(str(d))
-        f.close()
         for i, name in enumerate(self._command_mode['keys']):
             if i == 0:
                 d = d[name]['output']
         command = self._parent._getter.commands[keys[0]][keys[1]][status]
         self._parent.sockets.send(str(command), argument)
 
+    def _get_devices(self, data):
+        devices = []
+        for location, val in data.iteritems():
+            for dev in val.get('output', {}).keys():
+                devices.append((location, dev))
+        return devices
+
     def _update_command_mode(self, x):
-        self._command_mode['sequence'] += chr(x)
-        sequence = self._command_mode['sequence']
-        d = self._parent._data['locations']
-        for i, name in enumerate(self._command_mode['keys']):
-            if i == 0:
-                d = d[name]['output']
-            else:
-                d = d[name]
-        keys = [key for key in d.keys() if key.startswith(sequence)]
-        if len(keys) == 1:
-            self._command_mode['keys'].append(keys[0])
-            self._command_mode['sequence'] = ''
+        """
+        down arrow = 258
+        up arrow =   259
+        C-p      =   16
+        p        =   112
+        C-n      =   14
+        n        =   110
+        """
+        direction = None
+        i = self._command_mode['i']
+        if x == 258 or x == 14 or x == 110:
+            i += 1
+        elif x == 259 or x == 16 or x == 112:
+            i -= 1
+        else:
+            return
+        d = self._parent._data.get('locations', {})
+        devices = self._get_devices(d)
+        if i >= len(devices):
+            i = 0
+        elif i < 0:
+            i = len(devices) - 1
+        self._command_mode['keys'] = devices[i]
+        self._command_mode['i'] = i
+