55
66ACK = "\xFF \x00 "
77NACK = "\xFF \x01 "
8- REPORT = "\xF1 \x05 \x10 \x01 \x3D \x05 \x20 "
8+ REPORT = "\xF1 \x05 \x10 \x80 \x3D \x05 \x20 "
9+ REPORT_B = "\xF1 \x1A \x10 \xF1 \xA0 \x3D \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 \x01 \x00 "
910
1011
1112class TestArduinoInterface (unittest .TestCase ):
@@ -15,33 +16,28 @@ def setUp(self):
1516 self ._interface = ArduinoInterface (self ._connection , boards .Due )
1617
1718 def test_set_precition (self ):
18- self ._interface .set_precition (12 )
19+ self ._interface .set_precision (12 )
1920 data = self ._connection .pop_data ()
2021 self .assertEqual ("\x01 \x01 \x0C " , data )
2122 with self .assertRaises (Exception ):
2223 self ._interface .set_precition (33 )
2324
24- def test_set_pin_mode (self ):
25- self ._interface .set_pin_mode (1 , "INPUT" )
26- data = self ._connection .pop_data ()
27- self .assertEqual ("\x04 \x02 \x01 \x00 " , data )
28- with self .assertRaises (Exception ):
29- self ._interface .set_pin_mode (1 , "SOMETHING" )
30-
3125 def test_report_mode (self ):
3226 self ._interface .set_report_mode ("AVERAGE" )
3327 data = self ._connection .pop_data ()
34- self .assertEqual ("\x05 \x03 \x00 \x01 \x00 " , data )
28+ self .assertEqual ("\x05 \x03 \x00 \x00 \x00 " , data )
3529 self ._interface .set_report_mode ("AVERAGE" , read_count = 10 , read_delay = 5 )
3630 data = self ._connection .pop_data ()
37- self .assertEqual ("\x05 \x03 \x00 \x0A \x05 " , data )
31+ self .assertEqual ("\x05 \x03 \x00 \x09 \x05 " , data )
3832 with self .assertRaises (Exception ):
3933 self ._interface .set_report_mode ("SOMETHING" )
4034
4135 def test_add_input (self ):
4236 self ._interface .add_input (60 )
4337 data = self ._connection .pop_data ()
44- self .assertEqual ("\x02 \x01 \x06 " , data ) # 6 = 60 - min(boards.Due["ANALOG_PINS"])
38+ self .assertEqual ("\x02 \x01 \x3C " , data )
39+ data = self ._connection .pop_data ()
40+ self .assertEqual ("\x04 \x02 \x3C \x00 " , data )
4541 self ._interface .add_input (60 )
4642 with self .assertRaises (Exception ): # Checks that no data has been sent
4743 data = self ._connection .pop_data ()
@@ -52,6 +48,8 @@ def test_add_output(self):
5248 self ._interface .add_output (60 )
5349 data = self ._connection .pop_data ()
5450 self .assertEqual ("\x03 \x01 \x3C " , data )
51+ data = self ._connection .pop_data ()
52+ self .assertEqual ("\x04 \x02 \x3C \x01 " , data )
5553 self ._interface .add_output (60 )
5654 with self .assertRaises (Exception ): # Checks that no data has been sent
5755 data = self ._connection .pop_data ()
@@ -69,16 +67,48 @@ def test_actuate_error_response(self):
6967 with self .assertRaises (Exception ):
7068 self ._interface .actuate ([(60 , 128 )])
7169
72- def test_actuate (self ):
70+ def test_actuate_with_one_read (self ):
7371 self ._connection = MockConnection (REPORT )
7472 self ._interface = ArduinoInterface (self ._connection , boards .Due )
7573 self ._interface .add_output (60 )
74+ self ._interface .add_input (16 )
75+ self ._interface .add_input (61 )
76+ response = self ._interface .actuate ([(60 , 128 )])
77+ self .assertEqual (1 , len (response [0x10 ]))
78+ self .assertTrue (response [0x10 ][0 ])
79+ self .assertEqual (1 , len (response [0x3D ]))
80+ self .assertEqual (0x0520 ,response [0x3D ][0 ])
81+
82+ def test_actuate_with_many_readings (self ):
83+ self ._connection = MockConnection (REPORT_B )
84+ self ._interface = ArduinoInterface (self ._connection , boards .Due )
85+ self ._interface .set_report_mode ("BULK" , read_count = 11 , read_delay = 5 )
86+ self ._interface .add_output (60 )
87+ self ._interface .add_input (61 )
88+ self ._interface .add_input (16 )
89+ response = self ._interface .actuate ([(60 , 128 )])
90+ self .assertEqual (2 , len (response ))
91+ self .assertTrue (response [0x10 ][0 ])
92+ self .assertFalse (response [0x10 ][6 ])
93+ self .assertTrue (response [0x10 ][7 ])
94+ self .assertTrue (response [0x10 ][8 ])
95+ self .assertFalse (response [0x10 ][9 ])
96+ self .assertTrue (response [0x10 ][10 ])
97+ for i in range (0 , 11 ):
98+ self .assertEqual (0x0100 , response [0x3D ][i ])
99+
100+
101+ def test_average (self ):
102+ self ._connection = MockConnection (REPORT_B )
103+ self ._interface = ArduinoInterface (self ._connection , boards .Due )
104+ self ._interface .set_report_mode ("AVERAGE" , read_count = 11 , read_delay = 5 )
105+ self ._interface .add_output (60 )
76106 self ._interface .add_input (61 )
107+ self ._interface .add_input (16 )
77108 response = self ._interface .actuate ([(60 , 128 )])
78109 self .assertEqual (2 , len (response ))
79- self .assertEqual ("\x10 " ,response [0 ][0 ])
80- self .assertTrue (response [0 ][1 ])
81- self .assertEqual ("\x3D " ,response [1 ][0 ])
82- self .assertEqual (0x0520 ,response [1 ][1 ])
110+ self .assertEqual (1 , len (response [0x10 ]))
111+ self .assertTrue (response [0x10 ][0 ])
112+ self .assertEqual (0x0100 , response [0x3D ][0 ])
83113
84114
0 commit comments