Commits

Tino de Bruijn committed 468d686

Fix issue #9, first pin in port not correctly set, with tests

  • Participants
  • Parent commits a346528

Comments (0)

Files changed (2)

pyfirmata/pyfirmata.py

             for pin in self.pins:
                 if pin.mode is INPUT:
                     pin_nr = pin.pin_number - self.port_number * 8
-                    pin.value = (mask & (1 << pin_nr)) > 1
+                    pin.value = (mask & (1 << pin_nr)) > 0
 
 class Pin(object):
     """ A Pin representation """
     """
     def setUp(self):
         self.board = mockup.MockupBoard('test', BOARDS['arduino'])
+        
+class RegressionTests(BoardBaseTest):
+    
+    def test_correct_digital_input_first_pin_issue_9(self):
+        """
+        The first pin on the port would always be low, even if the mask said
+        it to be high.
+        """
+        pin = self.board.get_pin('d:8:i')
+        mask = 0
+        mask |= 1 << 0 # set pin 0 high
+        self.board._handle_digital_message(pin.port.port_number, 
+            mask % 128, mask >> 7)
+        self.assertEqual(pin.value, True)
+        
+    def test_handle_digital_inputs(self):
+        """
+        Test if digital inputs are correctly updated.
+        """
+        for i in range(8, 16): # pins of port 1
+            if not bool(i%2) and i != 14: # all even pins
+                self.board.digital[i].mode = pyfirmata.INPUT
+                self.assertEqual(self.board.digital[i].value, None)
+        mask = 0
+        # Set the mask high for the first 4 pins
+        for i in range(4):
+            mask |= 1 << i
+        self.board._handle_digital_message(1, mask % 128, mask >> 7)
+        self.assertEqual(self.board.digital[8].value, True)
+        self.assertEqual(self.board.digital[9].value, None)
+        self.assertEqual(self.board.digital[10].value, True)
+        self.assertEqual(self.board.digital[11].value, None)
+        self.assertEqual(self.board.digital[12].value, False)
+        self.assertEqual(self.board.digital[13].value, None)
+
 
 board_messages = unittest.TestLoader().loadTestsFromTestCase(TestBoardMessages)
 board_layout = unittest.TestLoader().loadTestsFromTestCase(TestBoardLayout)
-default = unittest.TestSuite([board_messages, board_layout])
+regression = unittest.TestLoader().loadTestsFromTestCase(RegressionTests)
+default = unittest.TestSuite([board_messages, board_layout, regression])
 mockup_suite = unittest.TestLoader().loadTestsFromTestCase(TestMockupBoardLayout)
 
 if __name__ == '__main__':