Tino de Bruijn avatar Tino de Bruijn committed 70d2b5c

Fixed a small bug in the handling of incoming digital messages, that prevented correct values on ports higher than 0

Comments (0)

Files changed (2)

pyfirmata/pyfirmata.py

         in the correct data. It will return False if there wasn't enough data
         for the handler
         """
-        # TODO document that a handler should 
+        # TODO document that a handler should
         handler = self._command_handlers[command]
         if len(data) < handler.bytes_needed:
             return False
         if self.reporting:
             for pin in self.pins:
                 if pin.mode is INPUT:
-                    pin.value = (mask & (1 << pin.pin_number)) > 1
+                    pin_nr = pin.pin_number - self.port_number * 8
+                    pin.value = (mask & (1 << pin_nr)) > 1
 
 class Pin(object):
     """ A Pin representation """
     # digital I/O message   0x90   port       LSB(bits 0-6)         MSB(bits 7-13)
     def test_incoming_digital_message(self):
         # A digital message sets the value for a whole port. We will set pin
-        # 2 (on port 0) to 1 to test if this is working.
-        self.board.digital[2].mode = pyfirmata.INPUT
+        # 9 (on port 1) to 1 to test if this is working.
+        self.board.digital[9].mode = pyfirmata.INPUT
         self.board.sp.clear() # clear mode sent over the wire.
         # Create the mask
         mask = 0
-        mask |= 1 << 2 # set the bit for pin 2 to to 1
-        self.assertEqual(self.board.digital[2].read(), None)
-        self.board.sp.write([chr(pyfirmata.DIGITAL_MESSAGE + 0), chr(mask % 128), chr(mask >> 7)])
+        mask |= 1 << (9 - 8) # set the bit for pin 9 to to 1
+        self.assertEqual(self.board.digital[9].read(), None)
+        self.board.sp.write([chr(pyfirmata.DIGITAL_MESSAGE + 1), chr(mask % 128), chr(mask >> 7)])
         self.iterate(3)
-        self.assertEqual(self.board.digital[2].read(), True)
+        self.assertEqual(self.board.digital[9].read(), True)
         
     # version report format
     # -------------------------------------------------
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.