Tino de Bruijn avatar Tino de Bruijn committed ce21005

Wait and iterate over initial firmata messages

Specifically get Firmware and Port Init messages
when openning a Board, instead of depending to the
user to set up an iterator or loop.

Thanks to nickraptis

Comments (0)

Files changed (2)

pyfirmata/mockup.py

     def close(self):
         self.clear()
         
+    def inWaiting(self):
+        return len(self)
+        
 class MockupBoard(pyfirmata.Board):
 
     def __init__(self, port, layout, values_dict={}):

pyfirmata/pyfirmata.py

     
     def __init__(self, port, layout, baudrate=57600, name=None):
         self.sp = serial.Serial(port, baudrate)
-        # Allow 2 secs for Arduino's auto-reset to happen
-        # TODO Is this still necessary?
-        self.pass_time(2)
+        # Allow 5 secs for Arduino's auto-reset to happen
+        # Alas, Firmata blinks it's 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)
         self.name = name
         if not self.name:
             self.name = port
         self.setup_layout(layout)
-        # TODO Test if we get a firmware name and version, otherwise there 
+        # Iterate over the first messages to get firmware data
+        while self.bytes_available():
+            self.iterate()
+        # TODO Test whether we got a firmware name and version, otherwise there 
         # probably isn't any Firmata installed
         
     def __str__(self):
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.