2222import unittest
2323from MLC .arduino .connection import MockConnection
2424from MLC .arduino .protocol import ArduinoInterface
25+ from MLC .arduino .protocol import REPORT_MODES
2526from MLC .arduino import boards
2627
2728ACK = "\xFF \x00 "
3233
3334
3435class TestArduinoInterface (unittest .TestCase ):
35-
3636 def setUp (self ):
3737 self ._connection = MockConnection (ACK )
3838 self ._interface = ArduinoInterface (self ._connection , boards .Due )
39-
39+
4040 def test_set_precition (self ):
4141 self ._interface .set_precision (12 )
4242 data = self ._connection .pop_data ()
@@ -45,14 +45,14 @@ def test_set_precition(self):
4545 self ._interface .set_precition (33 )
4646
4747 def test_report_mode (self ):
48- self ._interface .set_report_mode (" AVERAGE" )
48+ self ._interface .set_report_mode (REPORT_MODES . AVERAGE )
4949 data = self ._connection .pop_data ()
5050 self .assertEqual ("\x05 \x03 \x00 \x00 \x00 " , data )
51- self ._interface .set_report_mode (" AVERAGE" , read_count = 10 , read_delay = 5 )
51+ self ._interface .set_report_mode (REPORT_MODES . AVERAGE , read_count = 10 , read_delay = 5 )
5252 data = self ._connection .pop_data ()
5353 self .assertEqual ("\x05 \x03 \x00 \x09 \x05 " , data )
5454 with self .assertRaises (Exception ):
55- self ._interface .set_report_mode ("SOMETHING" )
55+ self ._interface .set_report_mode (12334 ) # Invalid report mode
5656
5757 def test_add_input (self ):
5858 self ._interface .add_input (60 )
@@ -77,16 +77,16 @@ def test_add_output(self):
7777 data = self ._connection .pop_data ()
7878 with self .assertRaises (Exception ): # Checks that no data has been sent
7979 self ._interface .add_output (128 )
80-
80+
8181 def test_error_adding_output_that_is_input (self ):
8282 self ._interface .add_input (60 )
83- with self .assertRaises (Exception ):
83+ with self .assertRaises (Exception ):
8484 self ._interface .add_analog_output (60 )
85-
85+
8686 def test_actuate_error_response (self ):
8787 self ._interface .add_output (60 )
8888 self ._connection .pop_data ()
89- with self .assertRaises (Exception ):
89+ with self .assertRaises (Exception ):
9090 self ._interface .actuate ([(60 , 128 )])
9191
9292 def test_actuate_with_one_read (self ):
@@ -96,54 +96,51 @@ def test_actuate_with_one_read(self):
9696 self ._interface .add_input (16 )
9797 self ._interface .add_input (61 )
9898 response = self ._interface .actuate ([(60 , 128 )])
99- self .assertEqual (1 , len (response [0x10 ]))
100- self .assertTrue (response [0x10 ][0 ])
101- self .assertEqual (1 , len (response [0x3D ]))
102- self .assertEqual (0x0520 ,response [0x3D ][0 ])
103-
99+ self .assertEqual (1 , len (response ["D16" ]))
100+ self .assertTrue (response ["D16" ][0 ])
101+ self .assertEqual (1 , len (response ["A7" ]))
102+ self .assertEqual (0x0520 , response ["A7" ][0 ])
103+
104104 def test_actuate_with_many_readings (self ):
105105 self ._connection = MockConnection (REPORT_B )
106106 self ._interface = ArduinoInterface (self ._connection , boards .Due )
107- self ._interface .set_report_mode (" BULK" , read_count = 11 , read_delay = 5 )
107+ self ._interface .set_report_mode (REPORT_MODES . BULK , read_count = 11 , read_delay = 5 )
108108 self ._interface .add_output (60 )
109109 self ._interface .add_input (61 )
110110 self ._interface .add_input (16 )
111111 response = self ._interface .actuate ([(60 , 128 )])
112112 self .assertEqual (2 , len (response ))
113- self .assertTrue (response [0x10 ][0 ])
114- self .assertTrue (response [0x10 ][6 ])
115- self .assertTrue (response [0x10 ][7 ])
116- self .assertFalse (response [0x10 ][8 ])
117- self .assertTrue (response [0x10 ][9 ])
118- self .assertFalse (response [0x10 ][10 ])
113+ self .assertTrue (response ["D16" ][0 ])
114+ self .assertTrue (response ["D16" ][6 ])
115+ self .assertTrue (response ["D16" ][7 ])
116+ self .assertFalse (response ["D16" ][8 ])
117+ self .assertTrue (response ["D16" ][9 ])
118+ self .assertFalse (response ["D16" ][10 ])
119119 for i in range (0 , 11 ):
120- self .assertEqual (0x0100 , response [0x3D ][i ])
120+ self .assertEqual (0x0100 , response ["A7" ][i ])
121121
122122 def test_actuate_with_many_pins_readings (self ):
123123 self ._connection = MockConnection (REPORT_C )
124124 self ._interface = ArduinoInterface (self ._connection , boards .Due )
125- self ._interface .set_report_mode (" BULK" , read_count = 5 , read_delay = 5 )
126- self ._interface .add_output (60 )
127- self ._interface .add_input (61 )
128- self ._interface .add_input (62 )
125+ self ._interface .set_report_mode (REPORT_MODES . BULK , read_count = 5 , read_delay = 5 )
126+ self ._interface .add_output (60 ) # Pin A6
127+ self ._interface .add_input (61 ) # Pin A7
128+ self ._interface .add_input (62 ) # Pin A8
129129 response = self ._interface .actuate ([(60 , 128 )])
130130 for i in range (0 , 5 ):
131- self .assertEqual (0x0100 , response [0x3D ][i ])
131+ self .assertEqual (0x0100 , response ["A7" ][i ])
132132 for i in range (0 , 5 ):
133- self .assertEqual (0x0100 , response [0x3E ][i ])
134-
133+ self .assertEqual (0x0100 , response ["A8" ][i ])
135134
136135 def test_average (self ):
137136 self ._connection = MockConnection (REPORT_B )
138137 self ._interface = ArduinoInterface (self ._connection , boards .Due )
139- self ._interface .set_report_mode (" AVERAGE" , read_count = 11 , read_delay = 5 )
140- self ._interface .add_output (60 )
141- self ._interface .add_input (61 )
142- self ._interface .add_input (16 )
138+ self ._interface .set_report_mode (REPORT_MODES . AVERAGE , read_count = 11 , read_delay = 5 )
139+ self ._interface .add_output (boards . Due [ "ANALOG_PINS" ][ 6 ] )
140+ self ._interface .add_input (boards . Due [ "ANALOG_PINS" ][ 7 ]) # Pin A7
141+ self ._interface .add_input (boards . Due [ "DIGITAL_PINS" ][ 16 ] )
143142 response = self ._interface .actuate ([(60 , 128 )])
144143 self .assertEqual (2 , len (response ))
145- self .assertEqual (1 , len (response [0x10 ]))
146- self .assertTrue (response [0x10 ][0 ])
147- self .assertEqual (0x0100 , response [0x3D ][0 ])
148-
149-
144+ self .assertEqual (1 , len (response ["D16" ]))
145+ self .assertTrue (response ["D16" ][0 ])
146+ self .assertEqual (0x0100 , response ["A7" ][0 ])
0 commit comments