Commits

Fabian Affolter committed f27c99d

Typos fixed and a small effort to respect PEP8

  • Participants
  • Parent commits 7663cfb

Comments (0)

Files changed (1)

File pyfirmata/pyfirmata.py

 from util import two_byte_iter_to_str, to_two_bytes
 
 # Message command bytes - straight from Firmata.h
-DIGITAL_MESSAGE = 0x90      # send data for a digital pin
-ANALOG_MESSAGE = 0xE0       # send data for an analog pin (or PWM)
+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
-QUERY_FIRMWARE = 0x79       # query the firmware name
+# 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
+QUERY_FIRMWARE = 0x79       # Query the firmware name
 
-# extended command set using sysex (0-127/0x00-0x7F)
+# Extended command set using sysex (0-127/0x00-0x7F)
 # 0x00-0x0F reserved for user-defined commands */
-SERVO_CONFIG = 0x70         # set max angle, minPulse, maxPulse, freq
-STRING_DATA = 0x71          # a string message with 14-bits per char
-SHIFT_DATA = 0x75           # a bitstream to/from a shift register
-I2C_REQUEST = 0x76          # send an I2C read/write request
-I2C_REPLY = 0x77            # a reply to an I2C read request
-I2C_CONFIG = 0x78           # config I2C settings such as delay times and power pins
-REPORT_FIRMWARE = 0x79      # report name and version of the firmware
-SAMPLING_INTERVAL = 0x7A    # set the poll rate of the main loop
+SERVO_CONFIG = 0x70         # Set max angle, minPulse, maxPulse, freq
+STRING_DATA = 0x71          # A string message with 14-bits per char
+SHIFT_DATA = 0x75           # A bitstream to/from a shift register
+I2C_REQUEST = 0x76          # Send an I2C read/write request
+I2C_REPLY = 0x77            # A reply to an I2C read request
+I2C_CONFIG = 0x78           # Config I2C settings such as delay times and power pins
+REPORT_FIRMWARE = 0x79      # Report name and version of the firmware
+SAMPLING_INTERVAL = 0x7A    # Set the poll rate of the main loop
 SYSEX_NON_REALTIME = 0x7E   # MIDI Reserved for non-realtime messages
 SYSEX_REALTIME = 0x7F       # MIDI Reserved for realtime messages
 
 
 # Pin modes.
-# except from UNAVAILABLE taken from Firmata.h
+# Except from UNAVAILABLE taken from Firmata.h
 UNAVAILABLE = -1 
-INPUT = 0          # as defined in wiring.h
-OUTPUT = 1         # as defined in wiring.h
-ANALOG = 2         # analog pin in analogInput mode
-PWM = 3            # digital pin in PWM output mode
-SERVO = 4          # digital pin in SERVO mode
+INPUT = 0          # As defined in wiring.h
+OUTPUT = 1         # As defined in wiring.h
+ANALOG = 2         # Analog pin in analogInput mode
+PWM = 3            # Digital pin in PWM output mode
+SERVO = 4          # Digital pin in SERVO mode
 
 # Pin types
-DIGITAL = OUTPUT   # same as OUTPUT below
+DIGITAL = OUTPUT   # Same as OUTPUT below
 # ANALOG is already defined above
 
 class PinAlreadyTakenError(Exception):
     def __init__(self, port, layout, baudrate=57600, name=None):
         self.sp = serial.Serial(port, baudrate)
         # Allow 5 secs for Arduino's auto-reset to happen
-        # Alas, Firmata blinks it's version before printing it to serial
+        # Alas, Firmata blinks its version before printing it to serial
         # For 2.3, even 5 seconds might not be enough.
         # TODO Find a more reliable way to wait until the board is ready
         self.pass_time(5)
 
     def setup_layout(self, board_layout):
         """
-        Setup the Pin instances based on the given board-layout. Maybe it will
+        Setup the Pin instances based on the given board layout. Maybe it will
         be possible to do this automatically in the future, by polling the
         board for its type.
         """
         Returns the activated pin given by the pin definition.
         May raise an ``InvalidPinDefError`` or a ``PinAlreadyTakenError``.
         
-        :arg pin_def: Pin definition as described in TODO,
+        :arg pin_def: Pin definition as described below,
             but without the arduino name. So for example ``a:1:i``.
-        
+           
+        'a' analog pin     Pin number   'i' for input 
+        'd' digital pin                 'o' for output
+                                        'p' for pwm (Pulse-width modulation)
+        All seperated by ``:``. 
         """
         if type(pin_def) == list:
             bits = pin_def
         """ 
         Reads and handles data from the microcontroller over the serial port.
         This method should be called in a main loop, or in an
-        :class:`Iterator` instance to keep this boards pin values up to date
+        :class:`Iterator` instance to keep this boards pin values up to date.
         """
         byte = self.sp.read()
         if not byte:
             
     def get_firmata_version(self):
         """
-        Returns a version tuple (major, mino) for the firmata firmware on the
+        Returns a version tuple (major, minor) for the firmata firmware on the
         board.
         """
         return self.firmata_version
         
     def servo_config(self, pin, min_pulse=544, max_pulse=2400, angle=0):
         """
-        Configure a pin as servo with min_pulse, max_pulse and first angle.
+        Configure a pin as servo with min_pulse, max_pulse, and first angle.
         ``min_pulse`` and ``max_pulse`` default to the arduino defaults.
         """
         if pin > len(self.digital) or self.digital[pin].mode == UNAVAILABLE:
-            raise IOError("Pin %s is not a valid servo pin")
+            raise IOError("Pin %s is not a valid servo pin.")
         data = itertools.chain([pin], to_two_bytes(min_pulse),
                                         to_two_bytes(max_pulse))
         self.send_sysex(SERVO_CONFIG, data)
     def _handle_digital_message(self, port_nr, lsb, msb):
         """
         Digital messages always go by the whole port. This means we have a
-        bitmask wich we update the port.
+        bitmask which update the port.
         """
         mask = (msb << 7) + lsb
         try:
         self.firmware = two_byte_iter_to_str(data[2:])
 
 class Port(object):
-    """ An 8-bit port on the board """
+    """ An 8-bit port on the board. """
     def __init__(self, board, port_number):
         self.board = board
         self.port_number = port_number
         return "Digital Port %i on %s" % (self.port_number, self.board)
         
     def enable_reporting(self):
-        """ Enable reporting of values for the whole port """
+        """ Enable reporting of values for the whole port. """
         self.reporting = True
         msg = chr(REPORT_DIGITAL + self.port_number)
         msg += chr(1)
                 pin.reporting = True # TODO Shouldn't this happen at the pin?
         
     def disable_reporting(self):
-        """ Disable the reporting of the port """
+        """ Disable the reporting of the port. """
         self.reporting = False
         msg = chr(REPORT_DIGITAL + self.port_number)
         msg += chr(0)
         self.board.sp.write(msg)
                 
     def write(self):
-        """Set the output pins of the port to the correct state"""
+        """ Set the output pins of the port to the correct state. """
         mask = 0
         for pin in self.pins:
             if pin.mode == OUTPUT:
                     pin.value = (mask & (1 << pin_nr)) > 0
 
 class Pin(object):
-    """ A Pin representation """
+    """ A Pin representation. """
     def __init__(self, board, pin_number, type=ANALOG, port=None):
         self.board = board
         self.pin_number = pin_number
             self._mode = UNAVAILABLE
             return
         if self._mode is UNAVAILABLE:
-            raise IOError("%s can not be used through Firmata" % self)
+            raise IOError("%s can not be used through Firmata." % self)
         if mode is PWM and not self.PWM_CAPABLE:
-            raise IOError("%s does not have PWM capabilities" % self)
+            raise IOError("%s does not have PWM capabilities." % self)
         if mode == SERVO:
             if self.type != DIGITAL:
                 raise IOError("Only digital pins can drive servos! %s is not"
-                    "digital" % self)
+                    "digital." % self)
             self._mode = SERVO
             self.board.servo_config(self.pin_number)
             return
     mode = property(_get_mode, _set_mode)
     """
     Mode of operation for the pin. Can be one of the pin modes: INPUT, OUTPUT,
-    ANALOG, PWM or SERVO (or UNAVAILABLE)
+    ANALOG, PWM, or SERVO (or UNAVAILABLE).
     """
     
     def enable_reporting(self):
-        """ Set an input pin to report values """
+        """ Set an input pin to report values. """
         if self.mode is not INPUT:
-            raise IOError, "%s is not an input and can therefore not report" % self
+            raise IOError, "%s is not an input and can therefore not" 
+                "report." % self
         if self.type == ANALOG:
             self.reporting = True
             msg = chr(REPORT_ANALOG + self.pin_number)
             self.port.enable_reporting() # TODO This is not going to work for non-optimized boards like Mega
         
     def disable_reporting(self):
-        """ Disable the reporting of an input pin """
+        """ Disable the reporting of an input pin. """
         if self.type == ANALOG:
             self.reporting = False
             msg = chr(REPORT_ANALOG + self.pin_number)
     def read(self):
         """
         Returns the output value of the pin. This value is updated by the
-        boards :meth:`Board.iterate` method. Value is alway in the range 0.0 - 1.0
+        boards :meth:`Board.iterate` method. Value is always in the range 
+        of 0.0 - 1.0 .
         """
         if self.mode == UNAVAILABLE:
             raise IOError, "Cannot read pin %s"% self.__str__()
         
         """
         if self.mode is UNAVAILABLE:
-            raise IOError, "%s can not be used through Firmata" % self
+            raise IOError, "%s can not be used through Firmata." % self
         if self.mode is INPUT:
             raise IOError, "%s is set up as an INPUT and can therefore not be written to" % self
         if value is not self.value:
                 msg = chr(ANALOG_MESSAGE + self.pin_number)
                 msg += chr(value % 128)
                 msg += chr(value >> 7)
-                self.board.sp.write(msg)
+                self.board.sp.write(msg)