Tino de Bruijn avatar Tino de Bruijn committed d5b8449

Factored out unnecessary Commands class.

Comments (0)

Files changed (2)

pyfirmata/pyfirmata.py

 from boards import BOARDS
 
 # Message command bytes - straight from Firmata.h
-COMMANDS = dict(
-    DIGITAL_MESSAGE = 0x90,      # send data for a digital pin
-    ANALOG_MESSAGE = 0xE0,       # send data for an analog pin (or PWM)
-    DIGITAL_PULSE = 0x91,        # SysEx command to send a digital pulse
+DIGITAL_MESSAGE = 0x90,      # send data for a digital pin
+ANALOG_MESSAGE = 0xE0,       # send data for an analog pin (or PWM)
+DIGITAL_PULSE = 0x91,        # SysEx command to send a digital pulse
 
-    # PULSE_MESSAGE = 0xA0,      # proposed pulseIn/Out msg (SysEx)
-    # SHIFTOUT_MESSAGE = 0xB0,   # proposed shiftOut msg (SysEx)
-    REPORT_ANALOG = 0xC0,        # enable analog input by pin #
-    REPORT_DIGITAL = 0xD0,       # enable digital input by port pair
-    START_SYSEX = 0xF0,          # start a MIDI SysEx msg
-    SET_PIN_MODE = 0xF4,         # set a pin to INPUT/OUTPUT/PWM/etc
-    END_SYSEX = 0xF7,            # end a MIDI SysEx msg
-    REPORT_VERSION = 0xF9,       # report firmware version
-    SYSTEM_RESET = 0xFF,         # reset from MIDI
-)
+# PULSE_MESSAGE = 0xA0,      # proposed pulseIn/Out msg (SysEx)
+# SHIFTOUT_MESSAGE = 0xB0,   # proposed shiftOut msg (SysEx)
+REPORT_ANALOG = 0xC0,        # enable analog input by pin #
+REPORT_DIGITAL = 0xD0,       # enable digital input by port pair
+START_SYSEX = 0xF0,          # start a MIDI SysEx msg
+SET_PIN_MODE = 0xF4,         # set a pin to INPUT/OUTPUT/PWM/etc
+END_SYSEX = 0xF7,            # end a MIDI SysEx msg
+REPORT_VERSION = 0xF9,       # report firmware version
+SYSTEM_RESET = 0xFF,         # reset from MIDI
 
 # Pin modes.
 # except from UNAVAILABLE taken from Firmata.h
     """
     
     def __init__(self, port, type="arduino", baudrate=57600):
-        # Setup a helper class to deal with commands
-        self.cmds = util.Commands(COMMANDS)
         self.type = type
         self.setup_layout(BOARDS[type])
+        self.command_handlers = dict()
         self.sp = serial.Serial(port, baudrate)
         # Allow 2 secs for Arduino's auto-reset to happen
         self.pass_time(2)
         self.taken = { 'analog' : dict(map(lambda p: (p.pin_number, False), self.analog)),
                        'digital' : dict(map(lambda p: (p.pin_number, False), self.digital)) }
         # Setup default handlers for standard incoming commands
-        self.cmds.add_handler('ANALOG_MESSAGE', self._update_analog)
-        self.cmds.add_handler('DIGITAL_MESSAGE', self._update_digital)
-        self.cmds.add_handler('REPORT_VERSION', self._update_version)
+        self.command_handlers[ANALOG_MESSAGE] = self._handle_analog_message
+        self.command_handlers[DIGITAL_MESSAGE] =  self._handle_digital_message
+        self.command_handlers[REPORT_VERSION] = self._handle_report_version
         
         
     def get_pin(self, pin_def):
             else:
                 self._stored_data.append(byte)
         elif not self._command:
-            if byte not in self.cmds:
-                # We received a byte not denoting a known command while we
-                # are not processing any commands data. Nothing we can do
-                # about it so discard and we'll see what comes next.
+            if byte not in self.command_handlers:
+                # We received a byte not denoting a command with handler 
+                # while we are not processing any commands data. Nothing we
+                # can do about it so discard and we'll see what comes next.
                 return
             self._command = byte
         else:
         return its return status.
         """
         try:
-             handle_cmd = self.self.cmds.get_handler(command)
+             handle_cmd = self.command_handlers[command]
              return handle_cmd(self, data)
         except (KeyError, ValueError):
             # something got corrupted
         self.sp.close()
         
     # Command handlers
-    def _update_analog(self, data):
+    def _handle_analog_message(self, data):
         if len(data) < 3:
             return False
         pin_number, lsb, msb = data
         self.analog[pin_number].value = value
         return True
 
-    def _update_digital(self, data):
+    def _handle_digital_message(self, data):
         if len(data) < 3:
             return False
         pin_number, lsb, msb = data
         self.digital[pin_number].value = value
         return True
 
-    def _update_version(self, data):
+    def _handle_report_version(self, data):
         if len(data) < 3:
             return False
         major, minor = data

pyfirmata/util.py

-class Commands(dict):
-    """
-    A helper class to deal with firmata command bytes. Set it up with a
-    dictionary of commandnames and there bytes. Then add handlers for certain
-    commands with ``add_handler``. Allows for accessing the commands as
-    attributes.
-    """
-    _handlers = {}
-    def __init__(self, bytes_dict):
-        if not type(bytes_dict):
-            bytes_dicts = dict(bytes_dict)
-        self = bytes_dict
-        
-    def __getattr__(self, name):
-        return self[name]
-    
-    def add_handler(self, command, handler):
-        if not command in self:
-            raise ValueError, "This command does not exist yet. Add it first with add_command"
-        self._handlers[command] = handler
-
-    def add_command(self, command, byte):
-        self[command] = byte
-    
-    def get_handler(self, command):
-        try:
-            return self._handlers[command]
-        except IndexError:
-            raise NotImplementedError, "A handler for %s has not been defined yet." % chr(command)
-
 import threading
 import serial
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.