Commits

Tino de Bruijn committed e8a8f7c

Fixed get_the_board function and deleted the Boards dict as it was broken

Boards was broken and not usefull as it was. It should be implemented
with the new pin query possibilities of the latest Firmata.

  • Participants
  • Parent commits 4ffd4fb

Comments (0)

Files changed (1)

pyfirmata/util.py

 import time
 import os
 import pyfirmata
+from boards import BOARDS
 
-class Boards(dict):
-    """
-    A dictonary to manage multiple boards on the same machine. Tries to
-    connect to all defined usb ports, and then tries to fetch the board's
-    name. Result is a dictionary of `Board`_ classes mapped to their names.
-    """
-    def __init__(self, base_dir='/dev/', identifier='tty.usbserial', boards_map=None):
-        """
-        Searches all possible USB devices in ``base_dir`` that start with
-        ``identifier``. If given an ``boards_map`` dictionary it will setup
-        the boards in the map according to their type. Finding a board that
-        does not identify itself at all, that are not in the map, or boards
-        that are missing fail silenly. Use the ``verify_found_boards`` method
-        to check this.
-        
-        :base_dir arg: A absolute path for the base directory to search for USB connections
-        :identifier arg: A string a USB connection startswith and should be tried
-        :boards_map arg: A dictionary in the form of::
-            boards_map = { 1 : {'name' : 'myduino', 'board' : 'board'},
-                           2 : {'name' : 'mymega', 'board' : 'board_mega'} }
-        """
-        for device in os.listdir(base_dir):
-            if device.startswith(identifier):
-                try:
-                    board = pyfirmata.Board(os.path.join(base_dir, device))
-                except SerialException:
-                    pass
-                else:
-                    # if board.id:
-                    #     try:
-                    #         board.name = boards_map[board.id]['name']
-                    #         board.setup_layout(pyfirmata.BOARDS[boards_map[board.id]['board']])
-                    #     except (KeyError, TypeError): # FIXME Possible KeyErrors from setup_layout are caught here...
-                    #         # FIXME Better to check if boards_map is valid if given, otherwise raise error
-                    #         pass
-                    # if not board.name:
-                    board_name = 'unknown-%s' % device[-5:-1]
-                    self[board_name] = board
-                            
-    def verify_found_boards(self, boards_map):
-        """
-        Returns True if all and only boards in the map are found. Returns
-        False otherwise
-        """
-        names = set()
-        for board in boards_map:
-            names.add(board['name'])
-        return names is set(self.keys())
-        
-    def get_pin(self, pin_def):
-        parts = pin_def.split(':')
-        try:
-            return self[parts[0]].get_pin([parts[1], parts[2], parts[3]])
-        except KeyError:
-            raise KeyError("board named %s not found" % parts[0])
-        except IndexError:
-            raise pyfirmata.InvalidPinDefError("%s is not a valid pin definition" % pin_def)
-            
-    def exit(self):
-        for board in self.values():
-            board.exit()
-            
-    def __del__(self):
-        ''' 
-        The connection with the board can get messed up when a script is
-        closed without calling board.exit() (which closes the serial
-        connection). Therefore do it here and hope it helps.
-        '''
-        self.exit()
-
-def get_the_board(base_dir='/dev/', identifier='tty.usbserial',):
+def get_the_board(layout=BOARDS['arduino'], base_dir='/dev/', identifier='tty.usbserial',):
     """
     Helper function to get the one and only board connected to the computer
-    running this. It does needs the ``base_dir`` and ``identifier`` though. It
-    will raise an IOError if it can't find a board, on a serial, or if it
-    finds more than one.
+    running this. It assumes a normal arduino layout, but this can be
+    overriden by passing a different layout dict as the ``layout`` parameter.
+    ``base_dir`` and ``identifier`` are overridable as well. It will raise an
+    IOError if it can't find a board, on a serial, or if it finds more than
+    one.
     """
-    boards = Boards(base_dir='/dev/', identifier='tty.usbserial')
+    boards = []
+    for device in os.listdir(base_dir):
+        if device.startswith(identifier):
+            try:
+                board = pyfirmata.Board(os.path.join(base_dir, device), layout)
+            except serial.SerialException:
+                pass
+            else:
+                boards.append(board)
     if len(boards) == 0:
         raise IOError, "No boards found in %s with identifier %s" % (base_dir, identifier)
     elif len(boards) > 1:
         raise IOError, "More than one board found!"
-    return boards[boards.keys()[0]]
+    return boards[0]
 
 class Iterator(threading.Thread):
     def __init__(self, board):