@@ -71,6 +71,10 @@ def test_led_board_on_off():
7171 assert isinstance (board [0 ], LED )
7272 assert isinstance (board [1 ], LED )
7373 assert isinstance (board [2 ], LED )
74+ assert board .active_high
75+ assert board [0 ].active_high
76+ assert board [1 ].active_high
77+ assert board [2 ].active_high
7478 board .on ()
7579 assert all ((pin1 .state , pin2 .state , pin3 .state ))
7680 board .off ()
@@ -116,6 +120,91 @@ def test_led_board_on_off():
116120 assert pin2 .state
117121 assert pin3 .state
118122
123+ def test_led_board_active_low ():
124+ pin1 = MockPin (2 )
125+ pin2 = MockPin (3 )
126+ pin3 = MockPin (4 )
127+ with LEDBoard (pin1 , pin2 , foo = pin3 , active_high = False ) as board :
128+ assert not board .active_high
129+ assert not board [0 ].active_high
130+ assert not board [1 ].active_high
131+ assert not board [2 ].active_high
132+ board .on ()
133+ assert not any ((pin1 .state , pin2 .state , pin3 .state ))
134+ board .off ()
135+ assert all ((pin1 .state , pin2 .state , pin3 .state ))
136+ board [0 ].on ()
137+ assert board .value == (1 , 0 , 0 )
138+ assert not pin1 .state
139+ assert pin2 .state
140+ assert pin3 .state
141+ board .toggle ()
142+ assert board .value == (0 , 1 , 1 )
143+ assert pin1 .state
144+ assert not pin2 .state
145+ assert not pin3 .state
146+
147+ def test_led_board_value ():
148+ pin1 = MockPin (2 )
149+ pin2 = MockPin (3 )
150+ pin3 = MockPin (4 )
151+ with LEDBoard (pin1 , pin2 , foo = pin3 ) as board :
152+ assert board .value == (0 , 0 , 0 )
153+ board .value = (0 , 1 , 0 )
154+ assert board .value == (0 , 1 , 0 )
155+ board .value = (1 , 0 , 1 )
156+ assert board .value == (1 , 0 , 1 )
157+
158+ def test_led_board_pwm_value ():
159+ pin1 = MockPWMPin (2 )
160+ pin2 = MockPWMPin (3 )
161+ pin3 = MockPWMPin (4 )
162+ with LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True ) as board :
163+ assert board .value == (0 , 0 , 0 )
164+ board .value = (0 , 1 , 0 )
165+ assert board .value == (0 , 1 , 0 )
166+ board .value = (0.5 , 0 , 0.75 )
167+ assert board .value == (0.5 , 0 , 0.75 )
168+
169+ def test_led_board_pwm_bad_value ():
170+ pin1 = MockPWMPin (2 )
171+ pin2 = MockPWMPin (3 )
172+ pin3 = MockPWMPin (4 )
173+ with LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True ) as board :
174+ with pytest .raises (ValueError ):
175+ board .value = (- 1 , 0 , 0 )
176+ with pytest .raises (ValueError ):
177+ board .value = (0 , 2 , 0 )
178+
179+ def test_led_board_initial_value ():
180+ pin1 = MockPin (2 )
181+ pin2 = MockPin (3 )
182+ pin3 = MockPin (4 )
183+ with LEDBoard (pin1 , pin2 , foo = pin3 , initial_value = 0 ) as board :
184+ assert board .value == (0 , 0 , 0 )
185+ with LEDBoard (pin1 , pin2 , foo = pin3 , initial_value = 1 ) as board :
186+ assert board .value == (1 , 1 , 1 )
187+
188+ def test_led_board_pwm_initial_value ():
189+ pin1 = MockPWMPin (2 )
190+ pin2 = MockPWMPin (3 )
191+ pin3 = MockPWMPin (4 )
192+ with LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True , initial_value = 0 ) as board :
193+ assert board .value == (0 , 0 , 0 )
194+ with LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True , initial_value = 1 ) as board :
195+ assert board .value == (1 , 1 , 1 )
196+ with LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True , initial_value = 0.5 ) as board :
197+ assert board .value == (0.5 , 0.5 , 0.5 )
198+
199+ def test_led_board_pwm_bad_initial_value ():
200+ pin1 = MockPWMPin (2 )
201+ pin2 = MockPWMPin (3 )
202+ pin3 = MockPWMPin (4 )
203+ with pytest .raises (ValueError ):
204+ LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True , initial_value = - 1 )
205+ with pytest .raises (ValueError ):
206+ LEDBoard (pin1 , pin2 , foo = pin3 , pwm = True , initial_value = 2 )
207+
119208def test_led_board_nested ():
120209 pin1 = MockPin (2 )
121210 pin2 = MockPin (3 )
@@ -315,13 +404,24 @@ def test_led_bar_graph_value():
315404 pin2 = MockPin (3 )
316405 pin3 = MockPin (4 )
317406 with LEDBarGraph (pin1 , pin2 , pin3 ) as graph :
407+ assert isinstance (graph [0 ], LED )
408+ assert isinstance (graph [1 ], LED )
409+ assert isinstance (graph [2 ], LED )
410+ assert graph .active_high
411+ assert graph [0 ].active_high
412+ assert graph [1 ].active_high
413+ assert graph [2 ].active_high
318414 graph .value = 0
415+ assert graph .value == 0
319416 assert not any ((pin1 .state , pin2 .state , pin3 .state ))
320417 graph .value = 1
418+ assert graph .value == 1
321419 assert all ((pin1 .state , pin2 .state , pin3 .state ))
322420 graph .value = 1 / 3
421+ assert graph .value == 1 / 3
323422 assert pin1 .state and not (pin2 .state or pin3 .state )
324423 graph .value = - 1 / 3
424+ assert graph .value == - 1 / 3
325425 assert pin3 .state and not (pin1 .state or pin2 .state )
326426 pin1 .state = True
327427 pin2 .state = True
@@ -332,20 +432,50 @@ def test_led_bar_graph_value():
332432 pin1 .state = False
333433 assert graph .value == - 2 / 3
334434
435+ def test_led_bar_graph_active_low ():
436+ pin1 = MockPin (2 )
437+ pin2 = MockPin (3 )
438+ pin3 = MockPin (4 )
439+ with LEDBarGraph (pin1 , pin2 , pin3 , active_high = False ) as graph :
440+ assert not graph .active_high
441+ assert not graph [0 ].active_high
442+ assert not graph [1 ].active_high
443+ assert not graph [2 ].active_high
444+ graph .value = 0
445+ assert graph .value == 0
446+ assert all ((pin1 .state , pin2 .state , pin3 .state ))
447+ graph .value = 1
448+ assert graph .value == 1
449+ assert not any ((pin1 .state , pin2 .state , pin3 .state ))
450+ graph .value = 1 / 3
451+ assert graph .value == 1 / 3
452+ assert not pin1 .state and pin2 .state and pin3 .state
453+ graph .value = - 1 / 3
454+ assert graph .value == - 1 / 3
455+ assert not pin3 .state and pin1 .state and pin2 .state
456+
335457def test_led_bar_graph_pwm_value ():
336458 pin1 = MockPWMPin (2 )
337459 pin2 = MockPWMPin (3 )
338460 pin3 = MockPWMPin (4 )
339461 with LEDBarGraph (pin1 , pin2 , pin3 , pwm = True ) as graph :
462+ assert isinstance (graph [0 ], PWMLED )
463+ assert isinstance (graph [1 ], PWMLED )
464+ assert isinstance (graph [2 ], PWMLED )
340465 graph .value = 0
466+ assert graph .value == 0
341467 assert not any ((pin1 .state , pin2 .state , pin3 .state ))
342468 graph .value = 1
469+ assert graph .value == 1
343470 assert all ((pin1 .state , pin2 .state , pin3 .state ))
344471 graph .value = 1 / 3
472+ assert graph .value == 1 / 3
345473 assert pin1 .state and not (pin2 .state or pin3 .state )
346474 graph .value = - 1 / 3
475+ assert graph .value == - 1 / 3
347476 assert pin3 .state and not (pin1 .state or pin2 .state )
348477 graph .value = 1 / 2
478+ assert graph .value == 1 / 2
349479 assert (pin1 .state , pin2 .state , pin3 .state ) == (1 , 0.5 , 0 )
350480 pin1 .state = 0
351481 pin3 .state = 1
0 commit comments