Commits

Tino de Bruijn committed f3feab1

got tests running again, for what it's worth

  • Participants
  • Parent commits d5b8449

Comments (0)

Files changed (5)

File pyfirmata/__init__.py

+from pyfirmata import *

File pyfirmata/boards.py

 BOARDS = {
-    'normal' : {
+    'arduino' : {
         'digital' : tuple(x for x in range(14)),
         'analog' : tuple(x for x in range(6)),
         'pwm' : (3, 5, 6, 9, 10, 11),
         'use_ports' : True,
         'disabled' : (0, 1, 14, 15) # Rx, Tx, Crystal
     },
-    'mega' : {
+    'arduino_mega' : {
         'digital' : tuple(x for x in range(54)),
         'analog' : tuple(x for x in range(16)),
         'pwm' : tuple(x for x in range(2,14)),

File pyfirmata/mockup.py

             return ''
             
     def write(self, value):
-        if value == chr(pyfirmata.REPORT_ARDUINO_ID):
-            self.return_id = True
+        pass
             
     def close(self):
         pass
-
-class MockupArduinos(pyfirmata.Arduinos):
-    def __init__(self, identifier='', arduinos_map={}, values_dict={}):
-        self.values_dict = values_dict
-        for a in arduinos_map.values():
-            self[a['name']] = MockupArduino(name=a['name'], type=a['board'], values_dict=values_dict)
         
-    def update_values_dict(self, values_dict=None):
-        if values_dict is not None:
-            self.values_dict.update(values_dict)
-        if values_dict == {}: # reset
-            self.values_dict = {}
-        self['gen'].values_dict = self.values_dict
-        self['gen'].update_values_dict()
-    
-    def reset_taken(self):
-        self['gen'].reset_taken()
-        
-class MockupArduino(pyfirmata.Arduino):
+class MockupBoard(pyfirmata.Board):
 
     def __init__(self, port='', type="normal", values_dict={}, name=''):
         self.name = name
         for pin in self.analog:
             pin.values_dict = self.values_dict
         
-class MockupPort(pyfirmata.DigitalPort):
-    def __init__(self, sp, port_number):
-        self.sp = sp
+class MockupPort(pyfirmata.Port):
+    def __init__(self, board, port_number):
+        self.board = board
         self.port_number = port_number
         self.reporting = False
         
         self.pins = []
         for i in range(8):
             pin_nr = i + self.port_number * 8
-            self.pins.append(MockupPin(sp, pin_nr, type=pyfirmata.DIGITAL, port=self))
+            self.pins.append(MockupPin(self.board, pin_nr, type=pyfirmata.DIGITAL, port=self))
 
     def update_values_dict(self):
         for pin in self.pins:
     def stop(self):
         pass
 
-pyfirmata.DigitalPort = MockupPort
+pyfirmata.Port = MockupPort
 pyfirmata.Pin = MockupPin
 
-Arduinos = MockupArduinos
-Arduino = MockupArduino
+Board = MockupBoard

File pyfirmata/pyfirmata.py

-from serial import SerialException
+import serial
 import threading
 import util
+import time
 from boards import BOARDS
 
 # 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_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):
-        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.type = type
+        self.command_handlers = {}
+        self.setup_layout(BOARDS[type])
         
     def __str__(self):
         return "Board (%s) on %s" % (self.type, self.sp.port)
         # Create pin instances based on board layout
         self.analog = []
         for i in board_layout['analog']:
-            self.analog.append(Pin(self.sp, i, board_name=self.name))
+            self.analog.append(Pin(self, i))
         # Only create digital ports if the Firmata can use them (ie. not on the Mega...)
         # TODO Why is (TOTAL_FIRMATA_PINS + 7) / 8 used in Firmata?
         if board_layout['use_ports']:
             self.digital = []
             self.digital_ports = []
             for i in range(len(board_layout['digital']) / 7):
-                self.digital_ports.append(Port(self.sp, i, self))
+                self.digital_ports.append(Port(self, i))
             # Allow to access the Pin instances directly
             for port in self.digital_ports:
                 self.digital += port.pins
         else:
             self.digital = []
             for i in board_layout['digital']:
-                self.digital.append(Pin(self.sp, i, type=DIGITAL, board_name=self.name))
+                self.digital.append(Pin(self.sp, i, type=DIGITAL))
         # Disable certain ports like Rx/Tx and crystal ports
         for i in board_layout['disabled']:
             self.digital[i].mode = UNAVAILABLE
 
 class Port(object):
     """ An 8-bit port on the board """
-    def __init__(self, sp, port_number, board):
+    def __init__(self, board, port_number):
         self.board = board
-        self.sp = sp
         self.port_number = port_number
         self.reporting = False
         
         """ Enable reporting of values for the whole port """
         self.reporting = True
         msg = chr(REPORT_DIGITAL + self.port_number + 1)
-        self.sp.write(msg)
+        self.board.sp.write(msg)
         
     def disable_reporting(self):
         """ Disable the reporting of the port """
         self.reporting = False
         msg = chr(REPORT_DIGITAL + self.port_number + 0)
-        self.sp.write(msg)
+        self.board.sp.write(msg)
         
     def set_value(self, mask):
         """Record the value of each of the input pins belonging to the port"""
         msg = chr(DIGITAL_MESSAGE + self.port_number)
         msg += chr(mask % 128)
         msg += chr(mask >> 7)
-        self.sp.write(msg)
+        self.board.sp.write(msg)
 
 class Pin(object):
     """ A Pin representation """
-    def __init__(self, sp, pin_number, type=ANALOG, board_name=None, port=None):
-        self.sp = sp
+    def __init__(self, board, pin_number, type=ANALOG, port=None):
+        self.board = board
         self.pin_number = pin_number
         self.type = type
-        self.board_name = board_name
         self.port = port
         self.PWM_CAPABLE = False
         self._mode = (type == DIGITAL and OUTPUT or INPUT)
         command = chr(SET_PIN_MODE)
         command += chr(self.pin_number)
         command += chr(mode)
-        self.sp.write(command)
+        self.board.sp.write(command)
         
     def _get_mode(self):
         return self._mode
         self.reporting = True
         msg = chr(REPORT_ANALOG + self.pin_number)
         msg += chr(1)
-        self.sp.write(msg)
+        self.board.sp.write(msg)
         
     def disable_reporting(self):
         """ Disable the reporting of an input pin """
         self.reporting = False
         msg = chr(REPORT_ANALOG + self.pin_number)
         msg += chr(0)
-        self.sp.write(msg)
+        self.board.sp.write(msg)
     
     def read(self):
         """
                     msg = chr(DIGITAL_MESSAGE)
                     msg += chr(self.pin_number)
                     msg += chr(value)
-                    self.sp.write(msg)
+                    self.board.sp.write(msg)
             elif self.mode is PWM:
                 value = int(round(value * 255))
                 msg = chr(ANALOG_MESSAGE + self.pin_number)
                 msg += chr(value % 128)
                 msg += chr(value >> 7)
-                self.sp.write(msg)
+                self.board.sp.write(msg)
                 
     def send_sysex(self, sysex_cmd, data=[]):
         """
         :arg data: A list of data values
         """
         # TODO make the boards send_sysex available to the pin
-        self.sp.write(chr(START_SYSEX))
-        self.sp.write(chr(sysex_cmd))
+        self.board.sp.write(chr(START_SYSEX))
+        self.board.sp.write(chr(sysex_cmd))
         for byte in data:
             try:
                 byte = chr(byte)
             except ValueError:
                 byte = chr(byte >> 7) # TODO send multiple bytes
-            self.sp.write(byte)
-        self.sp.write(chr(END_SYSEX))
+            self.board.sp.write(byte)
+        self.board.sp.write(chr(END_SYSEX))
 from optparse import OptionParser
 
 import pyfirmata
-import mockup
+from pyfirmata import mockup
 
-# TODO Test Arduinos class
-
-class TestLiveArduinos(unittest.TestCase):
+class TestLiveBoards(unittest.TestCase):
     """
-    Test two live arduinos. On the 'transmitter' arduino:
+    Test two live boards. On the 'transmitter' board:
     
     - connect 5v and analog port 0 directly
-    - connect digital port 2 to the 'receiver' arduino's analog port 2 directly
-    - connect digital port 5 to the 'receiver' arduino's analog port 5 directly
-    - connect 3v to the 'receiver' arduino's analog port 1 directly
+    - connect digital port 2 to the 'receiver' board's analog port 2 directly
+    - connect digital port 5 to the 'receiver' board's analog port 5 directly
+    - connect 3v to the 'receiver' board's analog port 1 directly
     
-    On the 'receiver' arduino:
+    On the 'receiver' board:
     
     - connect ground and analog port 0 directly
     
     """
     
     def setUp(self):
-        self.arduinos = pyfirmata.Arduinos()
-        assert len(self.arduinos) >= 2, "Only %d arduino(s) found. I need at least 2!" % len(arduinos)
-        x, y = self.arduinos.values()[0], self.arduinos.values()[1]
+        self.boards = pyfirmata.Boards()
+        assert len(self.boards) >= 2, "Only %d board(s) found. I need at least 2!" % len(boards)
+        x, y = self.boards.values()[0], self.boards.values()[1]
         it1, it2 = pyfirmata.Iterator(x), pyfirmata.Iterator(y)
         it1.start(), it2.start()
         # give iterator time to iterate
             self.receiver = y
         else:
             self.fail("Could not complete setup. One, and only one of the \
-                arduino's analog ports should be set high by connecting it to \
-                its 5v output. That arduino will be considered the transmitter. \
+                board's analog ports should be set high by connecting it to \
+                its 5v output. That board will be considered the transmitter. \
                 Values received: %f and %f" % (x0.read(), y0.read()))
         self.Ta0 = self.transmitter.analog[0] # already taken by line 36
         self.Ra1 = self.receiver.get_pin('a:1:i')
         self.failUnless(0.09 < value < 0.11, msg="%f not between 0.09 and 0.11" % value)
         
     def tearDown(self):
-        self.arduinos.exit()
+        self.boards.exit()
 
-class TestArduino(unittest.TestCase):
-    # TODO Test layout of Arduino Mega
+class TestBoard(unittest.TestCase):
+    # TODO Test layout of Board Mega
     # TODO Test if messages are correct...
     
     def setUp(self):
-        pyfirmata.serial.Serial = mockup.MockupSerial
-        self.arduino = pyfirmata.Arduino('test')
-        self.arduino.setup_layout(pyfirmata.BOARDS['normal'])
-    
-    def test_identifier(self):
-        self.assertEqual(self.arduino.id, ord(chr(1)))
+        pyfirmata.pyfirmata.serial.Serial = mockup.MockupSerial
+        self.board = pyfirmata.Board('test')
+        # self.board.setup_layout(pyfirmata.BOARDS['normal'])
 
     def test_pwm_layout(self):
         pins = []
-        for pin in self.arduino.digital:
+        for pin in self.board.digital:
             if pin.PWM_CAPABLE:
-                pins.append(self.arduino.get_pin('d:%d:p' % pin.pin_number))
+                pins.append(self.board.get_pin('d:%d:p' % pin.pin_number))
         for pin in pins:
             self.assertEqual(pin.mode, pyfirmata.PWM)
         
     def test_get_pin_digital(self):
-        pin = self.arduino.get_pin('d:13:o')
+        pin = self.board.get_pin('d:13:o')
         self.assertEqual(pin.pin_number, 13)
         self.assertEqual(pin.mode, pyfirmata.OUTPUT)
-        self.assertEqual(pin.sp, self.arduino.sp)
         self.assertEqual(pin.port.port_number, 1)
-        self.assertEqual(pin.port.get_active(), 1)
+        self.assertEqual(pin.port.reporting, False)
         
     def test_get_pin_analog(self):
-        pin = self.arduino.get_pin('a:5:i')
+        pin = self.board.get_pin('a:5:i')
         self.assertEqual(pin.pin_number, 5)
-        self.assertEqual(pin.sp, self.arduino.sp)
         self.assertEqual(pin.reporting, True)
         self.assertEqual(pin.value, None)
         
     def tearDown(self):
-        self.arduino.exit()
+        self.board.exit()
         pyfirmata.serial.Serial = serial.Serial
         
-class TestMockupArduino(unittest.TestCase):
+class TestMockupBoard(unittest.TestCase):
     
     def setUp(self):
-        self.arduino = mockup.MockupArduino('test')
+        self.board = mockup.MockupBoard('test')
     
     def test_identifier(self):
-        self.assertEqual(self.arduino.identifier, ord(chr(ID)))
+        self.assertEqual(self.board.identifier, ord(chr(ID)))
     
     def test_pwm_layout(self):
         pins = []
-        for pin_nr in self.arduino.layout['p']:
-            pins.append(self.arduino.get_pin('d:%d:p' % pin_nr))
+        for pin_nr in self.board.layout['p']:
+            pins.append(self.board.get_pin('d:%d:p' % pin_nr))
         for pin in pins:
             mode = pin.get_mode()
             self.assertEqual(mode, pyfirmata.DIGITAL_PWM)
         
     def test_get_pin_digital(self):
-        pin = self.arduino.get_pin('d:13:o')
+        pin = self.board.get_pin('d:13:o')
         self.assertEqual(pin.pin_number, 5)
         self.assertEqual(pin.mode, pyfirmata.DIGITAL_OUTPUT)
-        self.assertEqual(pin.sp, self.arduino.sp)
+        self.assertEqual(pin.sp, self.board.sp)
         self.assertEqual(pin.port.port_number, 1)
         self.assertEqual(pin._get_board_pin_number(), 13)
         self.assertEqual(pin.port.get_active(), 1)
         
     def test_get_pin_analog(self):
-        pin = self.arduino.get_pin('a:5:i')
+        pin = self.board.get_pin('a:5:i')
         self.assertEqual(pin.pin_number, 5)
-        self.assertEqual(pin.sp, self.arduino.sp)
+        self.assertEqual(pin.sp, self.board.sp)
         self.assertEqual(pin.get_active(), 1)
         self.assertEqual(pin.value, -1)
         
     def tearDown(self):
-        self.arduino.exit()
+        self.board.exit()
         pyfirmata.serial.Serial = serial.Serial
 
 
-suite = unittest.TestLoader().loadTestsFromTestCase(TestArduino)
-live_suite = unittest.TestLoader().loadTestsFromTestCase(TestLiveArduinos)
-mockup_suite = unittest.TestLoader().loadTestsFromTestCase(TestMockupArduino)
+suite = unittest.TestLoader().loadTestsFromTestCase(TestBoard)
+live_suite = unittest.TestLoader().loadTestsFromTestCase(TestLiveBoards)
+mockup_suite = unittest.TestLoader().loadTestsFromTestCase(TestMockupBoard)
 
 if __name__ == '__main__':
     parser = OptionParser()