@@ -261,9 +261,6 @@ def test_led_board_nested():
261
261
assert pin3 .state
262
262
263
263
def test_led_board_bad_blink ():
264
- pin1 = Device .pin_factory .pin (2 )
265
- pin2 = Device .pin_factory .pin (3 )
266
- pin3 = Device .pin_factory .pin (4 )
267
264
with LEDBoard (2 , LEDBoard (3 , 4 )) as board :
268
265
with pytest .raises (ValueError ):
269
266
board .blink (fade_in_time = 1 , fade_out_time = 1 )
@@ -846,13 +843,179 @@ def test_rgbledboard_initial_value_no_pwm():
846
843
board .value = ((0.5 , 0.5 , 0.5 ), (0.5 , 0.5 , 0.5 ))
847
844
assert board .value == ((0 , 0 , 0 ), (0 , 0 , 0 ))
848
845
849
- def test_rgbledboard_blink ():
850
- with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 )) as board :
851
- board .blink ()
852
- assert board .value == ((1 , 1 , 1 ), (1 , 1 , 1 ))
853
- board .off ()
854
- board [0 ].blink ()
855
- assert board .value == ((1 , 1 , 1 ), (0 , 0 , 0 ))
846
+ # start
847
+ def test_rgbledboard_bad_blink ():
848
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
849
+ with pytest .raises (ValueError ):
850
+ board .blink (fade_in_time = 1 , fade_out_time = 1 )
851
+ with pytest .raises (ValueError ):
852
+ board .blink (fade_out_time = 1 )
853
+ with pytest .raises (ValueError ):
854
+ board .pulse ()
855
+
856
+ @pytest .mark .skipif (hasattr (sys , 'pypy_version_info' ),
857
+ reason = 'timing is too random on pypy' )
858
+ def test_rgbledboard_blink_background ():
859
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
860
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
861
+ # Instantiation takes a long enough time that it throws off our timing
862
+ # here!
863
+ for pin in pins :
864
+ pin .clear_states ()
865
+ board .blink (0.1 , 0.1 , n = 2 )
866
+ board ._blink_thread .join () # naughty, but ensures no arbitrary waits in the test
867
+ test = [
868
+ (0.0 , False ),
869
+ (0.0 , True ),
870
+ (0.1 , False ),
871
+ (0.1 , True ),
872
+ (0.1 , False )
873
+ ]
874
+ for pin in pins :
875
+ pin .assert_states_and_times (test )
876
+
877
+ @pytest .mark .skipif (hasattr (sys , 'pypy_version_info' ),
878
+ reason = 'timing is too random on pypy' )
879
+ def test_rgbledboard_blink_foreground ():
880
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
881
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
882
+ for pin in pins :
883
+ pin .clear_states ()
884
+ board .blink (0.1 , 0.1 , n = 2 , background = False )
885
+ test = [
886
+ (0.0 , False ),
887
+ (0.0 , True ),
888
+ (0.1 , False ),
889
+ (0.1 , True ),
890
+ (0.1 , False )
891
+ ]
892
+ for pin in pins :
893
+ pin .assert_states_and_times (test )
894
+
895
+ @pytest .mark .skipif (hasattr (sys , 'pypy_version_info' ),
896
+ reason = 'timing is too random on pypy' )
897
+ def test_rgbledboard_blink_control ():
898
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
899
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
900
+ for pin in pins :
901
+ pin .clear_states ()
902
+ board .blink (0.1 , 0.1 , n = 2 )
903
+ # make sure the blink thread's started
904
+ while not board ._blink_leds :
905
+ sleep (0.00001 ) # pragma: no cover
906
+ board [1 ][0 ].off () # immediately take over the second LED
907
+ board ._blink_thread .join () # naughty, but ensures no arbitrary waits in the test
908
+ test = [
909
+ (0.0 , False ),
910
+ (0.0 , True ),
911
+ (0.1 , False ),
912
+ (0.1 , True ),
913
+ (0.1 , False )
914
+ ]
915
+ test2 = [(0.0 , False ), (0.0 , True ), (0.0 , False )]
916
+ for pin in pins [:3 ]:
917
+ pin .assert_states_and_times (test )
918
+ for pin in pins [3 :]:
919
+ pin .assert_states_and_times (test2 )
920
+
921
+ @pytest .mark .skipif (hasattr (sys , 'pypy_version_info' ),
922
+ reason = 'timing is too random on pypy' )
923
+ def test_rgbledboard_blink_take_over ():
924
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
925
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
926
+ for pin in pins :
927
+ pin .clear_states ()
928
+ board [1 ].blink (0.1 , 0.1 , n = 2 )
929
+ board .blink (0.1 , 0.1 , n = 2 ) # immediately take over blinking
930
+ board [1 ]._blink_thread .join ()
931
+ board ._blink_thread .join ()
932
+ test = [
933
+ (0.0 , False ),
934
+ (0.0 , True ),
935
+ (0.1 , False ),
936
+ (0.1 , True ),
937
+ (0.1 , False )
938
+ ]
939
+ for pin in pins :
940
+ pin .assert_states_and_times (test )
941
+
942
+ @pytest .mark .skipif (hasattr (sys , 'pypy_version_info' ),
943
+ reason = 'timing is too random on pypy' )
944
+ def test_rgbledboard_blink_control_all ():
945
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
946
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
947
+ for pin in pins :
948
+ pin .clear_states ()
949
+ board .blink (0.1 , 0.1 , n = 2 )
950
+ # make sure the blink thread's started
951
+ while not board ._blink_leds :
952
+ sleep (0.00001 ) # pragma: no cover
953
+ board [0 ].off () # immediately take over all LEDs
954
+ board [1 ].off ()
955
+ board ._blink_thread .join () # blink should terminate here anyway
956
+ test = [
957
+ (0.0 , False ),
958
+ (0.0 , True ),
959
+ (0.0 , False ),
960
+ ]
961
+ for pin in pins :
962
+ pin .assert_states_and_times (test )
963
+
964
+ def test_rgbledboard_blink_interrupt_on ():
965
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
966
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
967
+ board .blink (1 , 0.1 )
968
+ sleep (0.2 )
969
+ board .off () # should interrupt while on
970
+ for pin in pins :
971
+ pin .assert_states ([False , True , False ])
972
+
973
+ def test_rgbledboard_blink_interrupt_off ():
974
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
975
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
976
+ for pin in pins :
977
+ pin .clear_states ()
978
+ board .blink (0.1 , 1 )
979
+ sleep (0.2 )
980
+ board .off () # should interrupt while off
981
+ for pin in pins :
982
+ pin .assert_states ([False , True , False ])
983
+
984
+ @pytest .mark .skipif (hasattr (sys , 'pypy_version_info' ),
985
+ reason = 'timing is too random on pypy' )
986
+ def test_rgbledboard_fade_background ():
987
+ pins = [Device .pin_factory .pin (n ) for n in (2 , 3 , 4 , 5 , 6 , 7 )]
988
+ with RGBLEDBoard ((2 , 3 , 4 ), (5 , 6 , 7 ), pwm = False ) as board :
989
+ for pin in pins :
990
+ pin .clear_states ()
991
+ board .blink (0 , 0 , 0.2 , 0.2 , n = 2 )
992
+ board ._blink_thread .join ()
993
+ test = [
994
+ (0.0 , 0 ),
995
+ (0.04 , 0.2 ),
996
+ (0.04 , 0.4 ),
997
+ (0.04 , 0.6 ),
998
+ (0.04 , 0.8 ),
999
+ (0.04 , 1 ),
1000
+ (0.04 , 0.8 ),
1001
+ (0.04 , 0.6 ),
1002
+ (0.04 , 0.4 ),
1003
+ (0.04 , 0.2 ),
1004
+ (0.04 , 0 ),
1005
+ (0.04 , 0.2 ),
1006
+ (0.04 , 0.4 ),
1007
+ (0.04 , 0.6 ),
1008
+ (0.04 , 0.8 ),
1009
+ (0.04 , 1 ),
1010
+ (0.04 , 0.8 ),
1011
+ (0.04 , 0.6 ),
1012
+ (0.04 , 0.4 ),
1013
+ (0.04 , 0.2 ),
1014
+ (0.04 , 0 ),
1015
+ ]
1016
+ for pin in pins :
1017
+ pin .assert_states_and_times (test )
1018
+ # end
856
1019
857
1020
def test_traffic_lights_buzzer ():
858
1021
red_pin = Device .pin_factory .pin (2 )
0 commit comments