mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Make tests work reliably on the Pi
While the tests work well on a PC or Travis, the Pi (where I ought to be running them!) has some issues with the timing tests. Need to relax the tolerance of the "assert_states_and_times" method to 0.05 seconds otherwise it periodically fails even on something reasonably quick like a Pi 2 (less failures on a Pi 3 but still occasionally). Also reduced default fps to 25; if the default timing occasionally fails on a Pi 2 it's evidently too fast for a Pi 1 and shouldn't be the default; 25 also doesn't look any different to me on a pulsing LED. There's also a bunch of miscellaneous fixes in here; last minute typos and chart re-gens for the 1.2 release.
This commit is contained in:
@@ -31,84 +31,84 @@ def test_composite_output_on_off():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
device = CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3))
|
||||
device.on()
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
device.off()
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
with CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3)) as device:
|
||||
device.on()
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
device.off()
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
|
||||
def test_composite_output_toggle():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
device = CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3))
|
||||
device.toggle()
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
device[0].off()
|
||||
device.toggle()
|
||||
assert pin1.state
|
||||
assert not pin2.state
|
||||
assert not pin3.state
|
||||
with CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3)) as device:
|
||||
device.toggle()
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
device[0].off()
|
||||
device.toggle()
|
||||
assert pin1.state
|
||||
assert not pin2.state
|
||||
assert not pin3.state
|
||||
|
||||
def test_composite_output_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
device = CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3))
|
||||
assert device.value == (0, 0, 0)
|
||||
device.toggle()
|
||||
assert device.value == (1, 1, 1)
|
||||
device.value = (1, 0, 1)
|
||||
assert device[0].is_active
|
||||
assert not device[1].is_active
|
||||
assert device[2].is_active
|
||||
with CompositeOutputDevice(OutputDevice(pin1), OutputDevice(pin2), foo=OutputDevice(pin3)) as device:
|
||||
assert device.value == (0, 0, 0)
|
||||
device.toggle()
|
||||
assert device.value == (1, 1, 1)
|
||||
device.value = (1, 0, 1)
|
||||
assert device[0].is_active
|
||||
assert not device[1].is_active
|
||||
assert device[2].is_active
|
||||
|
||||
def test_led_board_on_off():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, pin2, foo=pin3)
|
||||
assert isinstance(board[0], LED)
|
||||
assert isinstance(board[1], LED)
|
||||
assert isinstance(board[2], LED)
|
||||
board.on()
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
board.off()
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
board[0].on()
|
||||
assert board.value == (1, 0, 0)
|
||||
assert pin1.state
|
||||
assert not pin2.state
|
||||
assert not pin3.state
|
||||
board.toggle()
|
||||
assert board.value == (0, 1, 1)
|
||||
assert not pin1.state
|
||||
assert pin2.state
|
||||
assert pin3.state
|
||||
with LEDBoard(pin1, pin2, foo=pin3) as board:
|
||||
assert isinstance(board[0], LED)
|
||||
assert isinstance(board[1], LED)
|
||||
assert isinstance(board[2], LED)
|
||||
board.on()
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
board.off()
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
board[0].on()
|
||||
assert board.value == (1, 0, 0)
|
||||
assert pin1.state
|
||||
assert not pin2.state
|
||||
assert not pin3.state
|
||||
board.toggle()
|
||||
assert board.value == (0, 1, 1)
|
||||
assert not pin1.state
|
||||
assert pin2.state
|
||||
assert pin3.state
|
||||
|
||||
def test_led_board_nested():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
assert list(led.pin for led in board.leds) == [pin1, pin2, pin3]
|
||||
assert board.value == (0, (0, 0))
|
||||
board.value = (1, (0, 1))
|
||||
assert pin1.state
|
||||
assert not pin2.state
|
||||
assert pin3.state
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
assert list(led.pin for led in board.leds) == [pin1, pin2, pin3]
|
||||
assert board.value == (0, (0, 0))
|
||||
board.value = (1, (0, 1))
|
||||
assert pin1.state
|
||||
assert not pin2.state
|
||||
assert pin3.state
|
||||
|
||||
def test_led_board_bad_blink():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
with pytest.raises(ValueError):
|
||||
board.blink(fade_in_time=1, fade_out_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
board.blink(fade_out_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
board.pulse()
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
with pytest.raises(ValueError):
|
||||
board.blink(fade_in_time=1, fade_out_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
board.blink(fade_out_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
board.pulse()
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -116,19 +116,19 @@ def test_led_board_blink_background():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
board._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
board._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -136,18 +136,18 @@ def test_led_board_blink_foreground():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board.blink(0.1, 0.1, n=2, background=False)
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board.blink(0.1, 0.1, n=2, background=False)
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -155,24 +155,24 @@ def test_led_board_blink_control():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
# make sure the blink thread's started
|
||||
while not board._blink_leds:
|
||||
sleep(0.00001)
|
||||
board[1][0].off() # immediately take over the second LED
|
||||
board._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
print(pin2.states)
|
||||
pin2.assert_states_and_times([(0.0, False), (0.0, True), (0.0, False)])
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
# make sure the blink thread's started
|
||||
while not board._blink_leds:
|
||||
sleep(0.00001)
|
||||
board[1][0].off() # immediately take over the second LED
|
||||
board._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
print(pin2.states)
|
||||
pin2.assert_states_and_times([(0.0, False), (0.0, True), (0.0, False)])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -180,21 +180,21 @@ def test_led_board_blink_take_over():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board[1].blink(0.1, 0.1, n=2)
|
||||
board.blink(0.1, 0.1, n=2) # immediately take over blinking
|
||||
board[1]._blink_thread.join()
|
||||
board._blink_thread.join()
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board[1].blink(0.1, 0.1, n=2)
|
||||
board.blink(0.1, 0.1, n=2) # immediately take over blinking
|
||||
board[1]._blink_thread.join()
|
||||
board._blink_thread.join()
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -202,47 +202,47 @@ def test_led_board_blink_control_all():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
# make sure the blink thread's started
|
||||
while not board._blink_leds:
|
||||
sleep(0.00001)
|
||||
board[0].off() # immediately take over all LEDs
|
||||
board[1][0].off()
|
||||
board[1][1].off()
|
||||
board._blink_thread.join() # blink should terminate here anyway
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.0, False),
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board.blink(0.1, 0.1, n=2)
|
||||
# make sure the blink thread's started
|
||||
while not board._blink_leds:
|
||||
sleep(0.00001)
|
||||
board[0].off() # immediately take over all LEDs
|
||||
board[1][0].off()
|
||||
board[1][1].off()
|
||||
board._blink_thread.join() # blink should terminate here anyway
|
||||
test = [
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.0, False),
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
def test_led_board_blink_interrupt_on():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
board.off() # should interrupt while on
|
||||
pin1.assert_states([False, True, False])
|
||||
pin2.assert_states([False, True, False])
|
||||
pin3.assert_states([False, True, False])
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
board.off() # should interrupt while on
|
||||
pin1.assert_states([False, True, False])
|
||||
pin2.assert_states([False, True, False])
|
||||
pin3.assert_states([False, True, False])
|
||||
|
||||
def test_led_board_blink_interrupt_off():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3))
|
||||
board.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
board.off() # should interrupt while off
|
||||
pin1.assert_states([False, True, False])
|
||||
pin2.assert_states([False, True, False])
|
||||
pin3.assert_states([False, True, False])
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3)) as board:
|
||||
board.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
board.off() # should interrupt while off
|
||||
pin1.assert_states([False, True, False])
|
||||
pin2.assert_states([False, True, False])
|
||||
pin3.assert_states([False, True, False])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -250,76 +250,76 @@ def test_led_board_fade_background():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(4)
|
||||
board = LEDBoard(pin1, LEDBoard(pin2, pin3, pwm=True), pwm=True)
|
||||
board.blink(0, 0, 0.1, 0.1, n=2)
|
||||
board._blink_thread.join()
|
||||
test = [
|
||||
(0.0, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
with LEDBoard(pin1, LEDBoard(pin2, pin3, pwm=True), pwm=True) as board:
|
||||
board.blink(0, 0, 0.2, 0.2, n=2)
|
||||
board._blink_thread.join()
|
||||
test = [
|
||||
(0.0, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
]
|
||||
pin1.assert_states_and_times(test)
|
||||
pin2.assert_states_and_times(test)
|
||||
pin3.assert_states_and_times(test)
|
||||
|
||||
def test_led_bar_graph_value():
|
||||
pin1 = MockPin(2)
|
||||
pin2 = MockPin(3)
|
||||
pin3 = MockPin(4)
|
||||
graph = LEDBarGraph(pin1, pin2, pin3)
|
||||
graph.value = 0
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1/3
|
||||
assert pin1.state and not (pin2.state or pin3.state)
|
||||
graph.value = -1/3
|
||||
assert pin3.state and not (pin1.state or pin2.state)
|
||||
pin1.state = True
|
||||
pin2.state = True
|
||||
assert graph.value == 1
|
||||
pin3.state = False
|
||||
assert graph.value == 2/3
|
||||
pin3.state = True
|
||||
pin1.state = False
|
||||
assert graph.value == -2/3
|
||||
with LEDBarGraph(pin1, pin2, pin3) as graph:
|
||||
graph.value = 0
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1/3
|
||||
assert pin1.state and not (pin2.state or pin3.state)
|
||||
graph.value = -1/3
|
||||
assert pin3.state and not (pin1.state or pin2.state)
|
||||
pin1.state = True
|
||||
pin2.state = True
|
||||
assert graph.value == 1
|
||||
pin3.state = False
|
||||
assert graph.value == 2/3
|
||||
pin3.state = True
|
||||
pin1.state = False
|
||||
assert graph.value == -2/3
|
||||
|
||||
def test_led_bar_graph_pwm_value():
|
||||
pin1 = MockPWMPin(2)
|
||||
pin2 = MockPWMPin(3)
|
||||
pin3 = MockPWMPin(4)
|
||||
graph = LEDBarGraph(pin1, pin2, pin3, pwm=True)
|
||||
graph.value = 0
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1/3
|
||||
assert pin1.state and not (pin2.state or pin3.state)
|
||||
graph.value = -1/3
|
||||
assert pin3.state and not (pin1.state or pin2.state)
|
||||
graph.value = 1/2
|
||||
assert (pin1.state, pin2.state, pin3.state) == (1, 0.5, 0)
|
||||
pin1.state = 0
|
||||
pin3.state = 1
|
||||
assert graph.value == -1/2
|
||||
with LEDBarGraph(pin1, pin2, pin3, pwm=True) as graph:
|
||||
graph.value = 0
|
||||
assert not any((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1
|
||||
assert all((pin1.state, pin2.state, pin3.state))
|
||||
graph.value = 1/3
|
||||
assert pin1.state and not (pin2.state or pin3.state)
|
||||
graph.value = -1/3
|
||||
assert pin3.state and not (pin1.state or pin2.state)
|
||||
graph.value = 1/2
|
||||
assert (pin1.state, pin2.state, pin3.state) == (1, 0.5, 0)
|
||||
pin1.state = 0
|
||||
pin3.state = 1
|
||||
assert graph.value == -1/2
|
||||
|
||||
def test_led_bar_graph_bad_init():
|
||||
pin1 = MockPin(2)
|
||||
@@ -330,26 +330,26 @@ def test_led_bar_graph_bad_init():
|
||||
|
||||
def test_pi_liter():
|
||||
pins = [MockPin(n) for n in (4, 17, 27, 18, 22, 23, 24, 25)]
|
||||
board = PiLiter()
|
||||
assert [device.pin for device in board] == pins
|
||||
with PiLiter() as board:
|
||||
assert [device.pin for device in board] == pins
|
||||
|
||||
def test_pi_liter_graph():
|
||||
pins = [MockPin(n) for n in (4, 17, 27, 18, 22, 23, 24, 25)]
|
||||
board = PiLiterBarGraph()
|
||||
board.value = 0.5
|
||||
assert [pin.state for pin in pins] == [1, 1, 1, 1, 0, 0, 0, 0]
|
||||
pins[4].state = 1
|
||||
assert board.value == 5/8
|
||||
with PiLiterBarGraph() as board:
|
||||
board.value = 0.5
|
||||
assert [pin.state for pin in pins] == [1, 1, 1, 1, 0, 0, 0, 0]
|
||||
pins[4].state = 1
|
||||
assert board.value == 5/8
|
||||
|
||||
def test_traffic_lights():
|
||||
red_pin = MockPin(2)
|
||||
amber_pin = MockPin(3)
|
||||
green_pin = MockPin(4)
|
||||
board = TrafficLights(red_pin, amber_pin, green_pin)
|
||||
board.red.on()
|
||||
assert red_pin.state
|
||||
assert not amber_pin.state
|
||||
assert not green_pin.state
|
||||
with TrafficLights(red_pin, amber_pin, green_pin) as board:
|
||||
board.red.on()
|
||||
assert red_pin.state
|
||||
assert not amber_pin.state
|
||||
assert not green_pin.state
|
||||
|
||||
def test_traffic_lights_bad_init():
|
||||
with pytest.raises(ValueError):
|
||||
@@ -357,13 +357,13 @@ def test_traffic_lights_bad_init():
|
||||
|
||||
def test_pi_traffic():
|
||||
pins = [MockPin(n) for n in (9, 10, 11)]
|
||||
board = PiTraffic()
|
||||
assert [device.pin for device in board] == pins
|
||||
with PiTraffic() as board:
|
||||
assert [device.pin for device in board] == pins
|
||||
|
||||
def test_snow_pi():
|
||||
pins = [MockPin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)]
|
||||
board = SnowPi()
|
||||
assert [device.pin for device in board.leds] == pins
|
||||
with SnowPi() as board:
|
||||
assert [device.pin for device in board.leds] == pins
|
||||
|
||||
def test_traffic_lights_buzzer():
|
||||
red_pin = MockPin(2)
|
||||
@@ -371,59 +371,59 @@ def test_traffic_lights_buzzer():
|
||||
green_pin = MockPin(4)
|
||||
buzzer_pin = MockPin(5)
|
||||
button_pin = MockPin(6)
|
||||
board = TrafficLightsBuzzer(
|
||||
with TrafficLightsBuzzer(
|
||||
TrafficLights(red_pin, amber_pin, green_pin),
|
||||
Buzzer(buzzer_pin),
|
||||
Button(button_pin))
|
||||
board.lights.red.on()
|
||||
board.buzzer.on()
|
||||
assert red_pin.state
|
||||
assert not amber_pin.state
|
||||
assert not green_pin.state
|
||||
assert buzzer_pin.state
|
||||
button_pin.drive_low()
|
||||
assert board.button.is_active
|
||||
Button(button_pin)) as board:
|
||||
board.lights.red.on()
|
||||
board.buzzer.on()
|
||||
assert red_pin.state
|
||||
assert not amber_pin.state
|
||||
assert not green_pin.state
|
||||
assert buzzer_pin.state
|
||||
button_pin.drive_low()
|
||||
assert board.button.is_active
|
||||
|
||||
def test_fish_dish():
|
||||
pins = [MockPin(n) for n in (9, 22, 4, 8, 7)]
|
||||
board = FishDish()
|
||||
assert [led.pin for led in board.lights] + [board.buzzer.pin, board.button.pin] == pins
|
||||
with FishDish() as board:
|
||||
assert [led.pin for led in board.lights] + [board.buzzer.pin, board.button.pin] == pins
|
||||
|
||||
def test_traffic_hat():
|
||||
pins = [MockPin(n) for n in (24, 23, 22, 5, 25)]
|
||||
board = TrafficHat()
|
||||
assert [led.pin for led in board.lights] + [board.buzzer.pin, board.button.pin] == pins
|
||||
with TrafficHat() as board:
|
||||
assert [led.pin for led in board.lights] + [board.buzzer.pin, board.button.pin] == pins
|
||||
|
||||
def test_robot():
|
||||
pins = [MockPWMPin(n) for n in (2, 3, 4, 5)]
|
||||
robot = Robot((2, 3), (4, 5))
|
||||
assert (
|
||||
[device.pin for device in robot.left_motor] +
|
||||
[device.pin for device in robot.right_motor]) == pins
|
||||
robot.forward()
|
||||
assert [pin.state for pin in pins] == [1, 0, 1, 0]
|
||||
robot.backward()
|
||||
assert [pin.state for pin in pins] == [0, 1, 0, 1]
|
||||
robot.forward(0.5)
|
||||
assert [pin.state for pin in pins] == [0.5, 0, 0.5, 0]
|
||||
robot.left()
|
||||
assert [pin.state for pin in pins] == [0, 1, 1, 0]
|
||||
robot.right()
|
||||
assert [pin.state for pin in pins] == [1, 0, 0, 1]
|
||||
robot.reverse()
|
||||
assert [pin.state for pin in pins] == [0, 1, 1, 0]
|
||||
robot.stop()
|
||||
assert [pin.state for pin in pins] == [0, 0, 0, 0]
|
||||
with Robot((2, 3), (4, 5)) as robot:
|
||||
assert (
|
||||
[device.pin for device in robot.left_motor] +
|
||||
[device.pin for device in robot.right_motor]) == pins
|
||||
robot.forward()
|
||||
assert [pin.state for pin in pins] == [1, 0, 1, 0]
|
||||
robot.backward()
|
||||
assert [pin.state for pin in pins] == [0, 1, 0, 1]
|
||||
robot.forward(0.5)
|
||||
assert [pin.state for pin in pins] == [0.5, 0, 0.5, 0]
|
||||
robot.left()
|
||||
assert [pin.state for pin in pins] == [0, 1, 1, 0]
|
||||
robot.right()
|
||||
assert [pin.state for pin in pins] == [1, 0, 0, 1]
|
||||
robot.reverse()
|
||||
assert [pin.state for pin in pins] == [0, 1, 1, 0]
|
||||
robot.stop()
|
||||
assert [pin.state for pin in pins] == [0, 0, 0, 0]
|
||||
|
||||
def test_ryanteck_robot():
|
||||
pins = [MockPWMPin(n) for n in (17, 18, 22, 23)]
|
||||
board = RyanteckRobot()
|
||||
assert [device.pin for motor in board for device in motor] == pins
|
||||
with RyanteckRobot() as board:
|
||||
assert [device.pin for motor in board for device in motor] == pins
|
||||
|
||||
def test_camjam_kit_robot():
|
||||
pins = [MockPWMPin(n) for n in (9, 10, 7, 8)]
|
||||
board = CamJamKitRobot()
|
||||
assert [device.pin for motor in board for device in motor] == pins
|
||||
with CamJamKitRobot() as board:
|
||||
assert [device.pin for motor in board for device in motor] == pins
|
||||
|
||||
def test_energenie_bad_init():
|
||||
with pytest.raises(ValueError):
|
||||
@@ -433,26 +433,26 @@ def test_energenie_bad_init():
|
||||
|
||||
def test_energenie():
|
||||
pins = [MockPin(n) for n in (17, 22, 23, 27, 24, 25)]
|
||||
device1 = Energenie(1, initial_value=True)
|
||||
device2 = Energenie(2, initial_value=False)
|
||||
assert device1.value
|
||||
assert not device2.value
|
||||
[pin.clear_states() for pin in pins]
|
||||
device1.on()
|
||||
assert device1.value
|
||||
pins[0].assert_states_and_times([(0.0, False), (0.0, True)])
|
||||
pins[1].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[2].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[3].assert_states_and_times([(0.0, False), (0.0, True)])
|
||||
pins[4].assert_states_and_times([(0.0, False)])
|
||||
pins[5].assert_states_and_times([(0.0, False), (0.1, True), (0.25, False)])
|
||||
[pin.clear_states() for pin in pins]
|
||||
device2.on()
|
||||
assert device2.value
|
||||
pins[0].assert_states_and_times([(0.0, True), (0.0, False)])
|
||||
pins[1].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[2].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[3].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[4].assert_states_and_times([(0.0, False)])
|
||||
pins[5].assert_states_and_times([(0.0, False), (0.1, True), (0.25, False)])
|
||||
with Energenie(1, initial_value=True) as device1, \
|
||||
Energenie(2, initial_value=False) as device2:
|
||||
assert device1.value
|
||||
assert not device2.value
|
||||
[pin.clear_states() for pin in pins]
|
||||
device1.on()
|
||||
assert device1.value
|
||||
pins[0].assert_states_and_times([(0.0, False), (0.0, True)])
|
||||
pins[1].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[2].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[3].assert_states_and_times([(0.0, False), (0.0, True)])
|
||||
pins[4].assert_states_and_times([(0.0, False)])
|
||||
pins[5].assert_states_and_times([(0.0, False), (0.1, True), (0.25, False)])
|
||||
[pin.clear_states() for pin in pins]
|
||||
device2.on()
|
||||
assert device2.value
|
||||
pins[0].assert_states_and_times([(0.0, True), (0.0, False)])
|
||||
pins[1].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[2].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[3].assert_states_and_times([(0.0, True), (0.0, True)])
|
||||
pins[4].assert_states_and_times([(0.0, False)])
|
||||
pins[5].assert_states_and_times([(0.0, False), (0.1, True), (0.25, False)])
|
||||
|
||||
|
||||
@@ -24,21 +24,22 @@ def test_device_no_pin():
|
||||
|
||||
def test_device_init():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
assert not device.closed
|
||||
assert device.pin == pin
|
||||
with GPIODevice(pin) as device:
|
||||
assert not device.closed
|
||||
assert device.pin == pin
|
||||
|
||||
def test_device_init_twice_same_pin():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
with pytest.raises(GPIOPinInUse):
|
||||
device2 = GPIODevice(pin)
|
||||
with GPIODevice(pin) as device:
|
||||
with pytest.raises(GPIOPinInUse):
|
||||
device2 = GPIODevice(pin)
|
||||
|
||||
def test_device_init_twice_different_pin():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
pin2 = MockPin(3)
|
||||
device2 = GPIODevice(pin2)
|
||||
with GPIODevice(pin) as device:
|
||||
with GPIODevice(pin2) as device2:
|
||||
pass
|
||||
|
||||
def test_device_close():
|
||||
pin = MockPin(2)
|
||||
@@ -56,11 +57,12 @@ def test_device_reopen_same_pin():
|
||||
assert device2.pin == pin
|
||||
assert device.closed
|
||||
assert device.pin is None
|
||||
device2.close()
|
||||
|
||||
def test_device_repr():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
assert repr(device) == '<gpiozero.GPIODevice object on pin %s, is_active=False>' % pin
|
||||
with GPIODevice(pin) as device:
|
||||
assert repr(device) == '<gpiozero.GPIODevice object on pin %s, is_active=False>' % pin
|
||||
|
||||
def test_device_repr_after_close():
|
||||
pin = MockPin(2)
|
||||
@@ -70,9 +72,9 @@ def test_device_repr_after_close():
|
||||
|
||||
def test_device_unknown_attr():
|
||||
pin = MockPin(2)
|
||||
device = GPIODevice(pin)
|
||||
with pytest.raises(AttributeError):
|
||||
device.foo = 1
|
||||
with GPIODevice(pin) as device:
|
||||
with pytest.raises(AttributeError):
|
||||
device.foo = 1
|
||||
|
||||
def test_device_context_manager():
|
||||
pin = MockPin(2)
|
||||
@@ -81,35 +83,35 @@ def test_device_context_manager():
|
||||
assert device.closed
|
||||
|
||||
def test_composite_device_sequence():
|
||||
device = CompositeDevice(
|
||||
InputDevice(MockPin(2)),
|
||||
InputDevice(MockPin(3))
|
||||
)
|
||||
assert len(device) == 2
|
||||
assert device[0].pin.number == 2
|
||||
assert device[1].pin.number == 3
|
||||
assert device.tuple._fields == ('_0', '_1')
|
||||
with CompositeDevice(
|
||||
InputDevice(MockPin(2)),
|
||||
InputDevice(MockPin(3))
|
||||
) as device:
|
||||
assert len(device) == 2
|
||||
assert device[0].pin.number == 2
|
||||
assert device[1].pin.number == 3
|
||||
assert device.tuple._fields == ('_0', '_1')
|
||||
|
||||
def test_composite_device_values():
|
||||
device = CompositeDevice(
|
||||
InputDevice(MockPin(2)),
|
||||
InputDevice(MockPin(3))
|
||||
)
|
||||
assert device.value == (0, 0)
|
||||
assert not device.is_active
|
||||
device[0].pin.drive_high()
|
||||
assert device.value == (1, 0)
|
||||
assert device.is_active
|
||||
with CompositeDevice(
|
||||
InputDevice(MockPin(2)),
|
||||
InputDevice(MockPin(3))
|
||||
) as device:
|
||||
assert device.value == (0, 0)
|
||||
assert not device.is_active
|
||||
device[0].pin.drive_high()
|
||||
assert device.value == (1, 0)
|
||||
assert device.is_active
|
||||
|
||||
def test_composite_device_named():
|
||||
device = CompositeDevice(
|
||||
foo=InputDevice(MockPin(2)),
|
||||
bar=InputDevice(MockPin(3)),
|
||||
_order=('foo', 'bar')
|
||||
)
|
||||
assert device.tuple._fields == ('foo', 'bar')
|
||||
assert device.value == (0, 0)
|
||||
assert not device.is_active
|
||||
with CompositeDevice(
|
||||
foo=InputDevice(MockPin(2)),
|
||||
bar=InputDevice(MockPin(3)),
|
||||
_order=('foo', 'bar')
|
||||
) as device:
|
||||
assert device.tuple._fields == ('foo', 'bar')
|
||||
assert device.value == (0, 0)
|
||||
assert not device.is_active
|
||||
|
||||
def test_composite_device_bad_init():
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
@@ -25,131 +25,121 @@ def teardown_function(function):
|
||||
|
||||
def test_input_initial_values():
|
||||
pin = MockPin(2)
|
||||
device = InputDevice(pin, pull_up=True)
|
||||
assert pin.function == 'input'
|
||||
assert pin.pull == 'up'
|
||||
assert device.pull_up
|
||||
device.close()
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
assert pin.pull == 'down'
|
||||
assert not device.pull_up
|
||||
device.close()
|
||||
with InputDevice(pin, pull_up=True) as device:
|
||||
assert pin.function == 'input'
|
||||
assert pin.pull == 'up'
|
||||
assert device.pull_up
|
||||
device.close()
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
assert pin.pull == 'down'
|
||||
assert not device.pull_up
|
||||
|
||||
def test_input_is_active():
|
||||
pin = MockPin(2)
|
||||
device = InputDevice(pin, pull_up=True)
|
||||
pin.drive_high()
|
||||
assert not device.is_active
|
||||
pin.drive_low()
|
||||
assert device.is_active
|
||||
with InputDevice(pin, pull_up=True) as device:
|
||||
pin.drive_high()
|
||||
assert not device.is_active
|
||||
pin.drive_low()
|
||||
assert device.is_active
|
||||
|
||||
def test_input_pulled_up():
|
||||
pin = MockPulledUpPin(2)
|
||||
with pytest.raises(PinFixedPull):
|
||||
device = InputDevice(pin, pull_up=False)
|
||||
InputDevice(pin, pull_up=False)
|
||||
|
||||
def test_input_event_activated():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
device.when_activated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert event.wait(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
device.when_activated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert event.wait(0)
|
||||
|
||||
def test_input_event_deactivated():
|
||||
event = Event()
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
device.when_deactivated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert not event.wait(0)
|
||||
pin.drive_low()
|
||||
assert event.wait(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
device.when_deactivated = lambda: event.set()
|
||||
assert not event.wait(0)
|
||||
pin.drive_high()
|
||||
assert not event.wait(0)
|
||||
pin.drive_low()
|
||||
assert event.wait(0)
|
||||
|
||||
def test_input_wait_active():
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
assert not device.wait_for_inactive(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
assert not device.wait_for_inactive(0)
|
||||
|
||||
def test_input_wait_inactive():
|
||||
pin = MockPin(2)
|
||||
device = DigitalInputDevice(pin)
|
||||
assert device.wait_for_inactive(1)
|
||||
assert not device.wait_for_active(0)
|
||||
with DigitalInputDevice(pin) as device:
|
||||
assert device.wait_for_inactive(1)
|
||||
assert not device.wait_for_active(0)
|
||||
|
||||
def test_input_smoothed_attrib():
|
||||
pin = MockPin(2)
|
||||
device = SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False)
|
||||
assert device.threshold == 0.5
|
||||
assert device.queue_len == 5
|
||||
assert not device.partial
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
|
||||
def test_input_smoothed_silly():
|
||||
pin = MockPin(2)
|
||||
with pytest.raises(InputDeviceError):
|
||||
device = SmoothedInputDevice(pin, threshold=-1)
|
||||
device = SmoothedInputDevice(pin)
|
||||
del device._queue.stopping
|
||||
with pytest.raises(AttributeError):
|
||||
device.close()
|
||||
with SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False) as device:
|
||||
assert device.threshold == 0.5
|
||||
assert device.queue_len == 5
|
||||
assert not device.partial
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
|
||||
def test_input_smoothed_values():
|
||||
pin = MockPin(2)
|
||||
device = SmoothedInputDevice(pin)
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
pin.drive_low()
|
||||
assert device.wait_for_inactive(1)
|
||||
with SmoothedInputDevice(pin) as device:
|
||||
device._queue.start()
|
||||
assert not device.is_active
|
||||
pin.drive_high()
|
||||
assert device.wait_for_active(1)
|
||||
pin.drive_low()
|
||||
assert device.wait_for_inactive(1)
|
||||
|
||||
def test_input_button():
|
||||
pin = MockPin(2)
|
||||
button = Button(pin)
|
||||
assert pin.pull == 'up'
|
||||
assert not button.is_pressed
|
||||
pin.drive_low()
|
||||
assert button.is_pressed
|
||||
assert button.wait_for_press(1)
|
||||
pin.drive_high()
|
||||
assert not button.is_pressed
|
||||
assert button.wait_for_release(1)
|
||||
with Button(pin) as button:
|
||||
assert pin.pull == 'up'
|
||||
assert not button.is_pressed
|
||||
pin.drive_low()
|
||||
assert button.is_pressed
|
||||
assert button.wait_for_press(1)
|
||||
pin.drive_high()
|
||||
assert not button.is_pressed
|
||||
assert button.wait_for_release(1)
|
||||
|
||||
def test_input_line_sensor():
|
||||
pin = MockPin(2)
|
||||
sensor = LineSensor(pin)
|
||||
pin.drive_low() # logic is inverted for line sensor
|
||||
assert sensor.wait_for_line(1)
|
||||
assert sensor.line_detected
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_no_line(1)
|
||||
assert not sensor.line_detected
|
||||
with LineSensor(pin) as sensor:
|
||||
pin.drive_low() # logic is inverted for line sensor
|
||||
assert sensor.wait_for_line(1)
|
||||
assert sensor.line_detected
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_no_line(1)
|
||||
assert not sensor.line_detected
|
||||
|
||||
def test_input_motion_sensor():
|
||||
pin = MockPin(2)
|
||||
sensor = MotionSensor(pin)
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_motion(1)
|
||||
assert sensor.motion_detected
|
||||
pin.drive_low()
|
||||
assert sensor.wait_for_no_motion(1)
|
||||
assert not sensor.motion_detected
|
||||
with MotionSensor(pin) as sensor:
|
||||
pin.drive_high()
|
||||
assert sensor.wait_for_motion(1)
|
||||
assert sensor.motion_detected
|
||||
pin.drive_low()
|
||||
assert sensor.wait_for_no_motion(1)
|
||||
assert not sensor.motion_detected
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_input_light_sensor():
|
||||
pin = MockChargingPin(2)
|
||||
sensor = LightSensor(pin)
|
||||
pin.charge_time = 0.1
|
||||
assert sensor.wait_for_dark(1)
|
||||
pin.charge_time = 0.0
|
||||
assert sensor.wait_for_light(1)
|
||||
with LightSensor(pin) as sensor:
|
||||
pin.charge_time = 0.1
|
||||
assert sensor.wait_for_dark(1)
|
||||
pin.charge_time = 0.0
|
||||
assert sensor.wait_for_light(1)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
@@ -162,22 +152,22 @@ def test_input_distance_sensor():
|
||||
DistanceSensor(echo_pin, trig_pin, max_distance=-1)
|
||||
# normal queue len is large (because the sensor is *really* jittery) but
|
||||
# we want quick tests and we've got precisely controlled pins :)
|
||||
sensor = DistanceSensor(echo_pin, trig_pin, queue_len=5, max_distance=1)
|
||||
assert sensor.max_distance == 1
|
||||
assert sensor.trigger is trig_pin
|
||||
assert sensor.echo is echo_pin
|
||||
assert sensor.wait_for_out_of_range(1)
|
||||
assert not sensor.in_range
|
||||
assert sensor.distance == 1.0 # should be waay before max-distance so this should work
|
||||
trig_pin.echo_time = 0.0
|
||||
assert sensor.wait_for_in_range(1)
|
||||
assert sensor.in_range
|
||||
assert sensor.distance < sensor.threshold_distance # depending on speed of machine, may not reach 0 here
|
||||
sensor.threshold_distance = 0.1
|
||||
assert sensor.threshold_distance == 0.1
|
||||
with pytest.raises(ValueError):
|
||||
sensor.max_distance = -1
|
||||
sensor.max_distance = 20
|
||||
assert sensor.max_distance == 20
|
||||
assert sensor.threshold_distance == 0.1
|
||||
with DistanceSensor(echo_pin, trig_pin, queue_len=5, max_distance=1) as sensor:
|
||||
assert sensor.max_distance == 1
|
||||
assert sensor.trigger is trig_pin
|
||||
assert sensor.echo is echo_pin
|
||||
assert sensor.wait_for_out_of_range(1)
|
||||
assert not sensor.in_range
|
||||
assert sensor.distance == 1.0 # should be waay before max-distance so this should work
|
||||
trig_pin.echo_time = 0.0
|
||||
assert sensor.wait_for_in_range(1)
|
||||
assert sensor.in_range
|
||||
assert sensor.distance < sensor.threshold_distance # depending on speed of machine, may not reach 0 here
|
||||
sensor.threshold_distance = 0.1
|
||||
assert sensor.threshold_distance == 0.1
|
||||
with pytest.raises(ValueError):
|
||||
sensor.max_distance = -1
|
||||
sensor.max_distance = 20
|
||||
assert sensor.max_distance == 20
|
||||
assert sensor.threshold_distance == 0.1
|
||||
|
||||
|
||||
@@ -25,114 +25,112 @@ def teardown_function(function):
|
||||
|
||||
def test_output_initial_values():
|
||||
pin = MockPin(2)
|
||||
device = OutputDevice(pin, initial_value=False)
|
||||
assert pin.function == 'output'
|
||||
assert not pin.state
|
||||
device.close()
|
||||
device = OutputDevice(pin, initial_value=True)
|
||||
assert pin.state
|
||||
device.close()
|
||||
state = pin.state
|
||||
device = OutputDevice(pin, initial_value=None)
|
||||
assert state == pin.state
|
||||
with OutputDevice(pin, initial_value=False) as device:
|
||||
assert pin.function == 'output'
|
||||
assert not pin.state
|
||||
with OutputDevice(pin, initial_value=True) as device:
|
||||
assert pin.state
|
||||
state = pin.state
|
||||
with OutputDevice(pin, initial_value=None) as device:
|
||||
assert state == pin.state
|
||||
|
||||
def test_output_write_active_high():
|
||||
pin = MockPin(2)
|
||||
device = OutputDevice(pin)
|
||||
device.on()
|
||||
assert pin.state
|
||||
device.off()
|
||||
assert not pin.state
|
||||
with OutputDevice(pin) as device:
|
||||
device.on()
|
||||
assert pin.state
|
||||
device.off()
|
||||
assert not pin.state
|
||||
|
||||
def test_output_write_active_low():
|
||||
pin = MockPin(2)
|
||||
device = OutputDevice(pin, active_high=False)
|
||||
device.on()
|
||||
assert not pin.state
|
||||
device.off()
|
||||
assert pin.state
|
||||
with OutputDevice(pin, active_high=False) as device:
|
||||
device.on()
|
||||
assert not pin.state
|
||||
device.off()
|
||||
assert pin.state
|
||||
|
||||
def test_output_write_closed():
|
||||
device = OutputDevice(MockPin(2))
|
||||
device.close()
|
||||
with pytest.raises(GPIODeviceClosed):
|
||||
device.on()
|
||||
with OutputDevice(MockPin(2)) as device:
|
||||
device.close()
|
||||
with pytest.raises(GPIODeviceClosed):
|
||||
device.on()
|
||||
|
||||
def test_output_write_silly():
|
||||
pin = MockPin(2)
|
||||
device = OutputDevice(pin)
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.on()
|
||||
with OutputDevice(pin) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.on()
|
||||
|
||||
def test_output_value():
|
||||
pin = MockPin(2)
|
||||
device = OutputDevice(pin)
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
device.on()
|
||||
assert device.value
|
||||
assert pin.state
|
||||
device.value = False
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
with OutputDevice(pin) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
device.on()
|
||||
assert device.value
|
||||
assert pin.state
|
||||
device.value = False
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
|
||||
def test_output_digital_toggle():
|
||||
pin = MockPin(2)
|
||||
device = DigitalOutputDevice(pin)
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
device.toggle()
|
||||
assert device.value
|
||||
assert pin.state
|
||||
device.toggle()
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
device.toggle()
|
||||
assert device.value
|
||||
assert pin.state
|
||||
device.toggle()
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_blink_background():
|
||||
pin = MockPin(2)
|
||||
device = DigitalOutputDevice(pin)
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
device._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
pin.assert_states_and_times([
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
])
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
device._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
pin.assert_states_and_times([
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_blink_foreground():
|
||||
pin = MockPin(2)
|
||||
device = DigitalOutputDevice(pin)
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
])
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
(0.1, False),
|
||||
(0.1, True),
|
||||
(0.1, False)
|
||||
])
|
||||
|
||||
def test_output_blink_interrupt_on():
|
||||
pin = MockPin(2)
|
||||
device = DigitalOutputDevice(pin)
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
pin.assert_states([False, True, False])
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_blink_interrupt_off():
|
||||
pin = MockPin(2)
|
||||
device = DigitalOutputDevice(pin)
|
||||
device.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while off
|
||||
pin.assert_states([False, True, False])
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while off
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_pwm_bad_initial_value():
|
||||
with pytest.raises(ValueError):
|
||||
@@ -144,50 +142,50 @@ def test_output_pwm_not_supported():
|
||||
|
||||
def test_output_pwm_states():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.value = 0.1
|
||||
device.value = 0.2
|
||||
device.value = 0.0
|
||||
pin.assert_states([0.0, 0.1, 0.2, 0.0])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.value = 0.1
|
||||
device.value = 0.2
|
||||
device.value = 0.0
|
||||
pin.assert_states([0.0, 0.1, 0.2, 0.0])
|
||||
|
||||
def test_output_pwm_read():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin, frequency=100)
|
||||
assert device.frequency == 100
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
assert isclose(pin.state, 0.1)
|
||||
assert device.is_active
|
||||
device.frequency = None
|
||||
assert not device.value
|
||||
assert not device.is_active
|
||||
assert device.frequency is None
|
||||
with PWMOutputDevice(pin, frequency=100) as device:
|
||||
assert device.frequency == 100
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
assert isclose(pin.state, 0.1)
|
||||
assert device.is_active
|
||||
device.frequency = None
|
||||
assert not device.value
|
||||
assert not device.is_active
|
||||
assert device.frequency is None
|
||||
|
||||
def test_output_pwm_write():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.on()
|
||||
device.off()
|
||||
pin.assert_states([False, True, False])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.on()
|
||||
device.off()
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_pwm_toggle():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.toggle()
|
||||
device.value = 0.5
|
||||
device.value = 0.1
|
||||
device.toggle()
|
||||
device.off()
|
||||
pin.assert_states([False, True, 0.5, 0.1, 0.9, False])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.toggle()
|
||||
device.value = 0.5
|
||||
device.value = 0.1
|
||||
device.toggle()
|
||||
device.off()
|
||||
pin.assert_states([False, True, 0.5, 0.1, 0.9, False])
|
||||
|
||||
def test_output_pwm_active_high_read():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin, active_high=False)
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
assert isclose(pin.state, 0.9)
|
||||
device.frequency = None
|
||||
assert device.value
|
||||
with PWMOutputDevice(pin, active_high=False) as device:
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
assert isclose(pin.state, 0.9)
|
||||
device.frequency = None
|
||||
assert device.value
|
||||
|
||||
def test_output_pwm_bad_value():
|
||||
with pytest.raises(ValueError):
|
||||
@@ -201,108 +199,108 @@ def test_output_pwm_write_closed():
|
||||
|
||||
def test_output_pwm_write_silly():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.off()
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.off()
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_blink_background():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
device._blink_thread.join()
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
device._blink_thread.join()
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_blink_foreground():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_fade_background():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.blink(0, 0, 0.1, 0.1, n=2)
|
||||
device._blink_thread.join()
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
device._blink_thread.join()
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_fade_foreground():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.blink(0, 0, 0.1, 0.1, n=2, background=False)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
])
|
||||
|
||||
def test_output_pwm_blink_interrupt():
|
||||
pin = MockPWMPin(2)
|
||||
device = PWMOutputDevice(pin)
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
pin.assert_states([0, 1, 0])
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
pin.assert_states([0, 1, 0])
|
||||
|
||||
def test_rgbled_missing_pins():
|
||||
with pytest.raises(ValueError):
|
||||
@@ -310,116 +308,116 @@ def test_rgbled_missing_pins():
|
||||
|
||||
def test_rgbled_initial_value():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b, initial_value=(0.1, 0.2, 0))
|
||||
assert r.frequency
|
||||
assert g.frequency
|
||||
assert b.frequency
|
||||
assert isclose(r.state, 0.1)
|
||||
assert isclose(g.state, 0.2)
|
||||
assert isclose(b.state, 0.0)
|
||||
with RGBLED(r, g, b, initial_value=(0.1, 0.2, 0)) as device:
|
||||
assert r.frequency
|
||||
assert g.frequency
|
||||
assert b.frequency
|
||||
assert isclose(r.state, 0.1)
|
||||
assert isclose(g.state, 0.2)
|
||||
assert isclose(b.state, 0.0)
|
||||
|
||||
def test_rgbled_value():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b)
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
device.on()
|
||||
assert device.is_active
|
||||
assert device.value == (1, 1, 1)
|
||||
device.off()
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
with RGBLED(r, g, b) as device:
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
device.on()
|
||||
assert device.is_active
|
||||
assert device.value == (1, 1, 1)
|
||||
device.off()
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_toggle():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b)
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
device.toggle()
|
||||
assert device.is_active
|
||||
assert device.value == (1, 1, 1)
|
||||
device.toggle()
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
with RGBLED(r, g, b) as device:
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
device.toggle()
|
||||
assert device.is_active
|
||||
assert device.value == (1, 1, 1)
|
||||
device.toggle()
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_background():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b)
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
device._blink_thread.join()
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
]
|
||||
r.assert_states_and_times(expected)
|
||||
g.assert_states_and_times(expected)
|
||||
b.assert_states_and_times(expected)
|
||||
with RGBLED(r, g, b) as device:
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
device._blink_thread.join()
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
]
|
||||
r.assert_states_and_times(expected)
|
||||
g.assert_states_and_times(expected)
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_foreground():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b)
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
]
|
||||
r.assert_states_and_times(expected)
|
||||
g.assert_states_and_times(expected)
|
||||
b.assert_states_and_times(expected)
|
||||
with RGBLED(r, g, b) as device:
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
(0.1, 0),
|
||||
(0.1, 1),
|
||||
(0.1, 0)
|
||||
]
|
||||
r.assert_states_and_times(expected)
|
||||
g.assert_states_and_times(expected)
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_fade_background():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b)
|
||||
device.blink(0, 0, 0.1, 0.1, n=2)
|
||||
device._blink_thread.join()
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.8),
|
||||
(0.02, 1),
|
||||
(0.02, 0.8),
|
||||
(0.02, 0.6),
|
||||
(0.02, 0.4),
|
||||
(0.02, 0.2),
|
||||
(0.02, 0),
|
||||
]
|
||||
r.assert_states_and_times(expected)
|
||||
g.assert_states_and_times(expected)
|
||||
b.assert_states_and_times(expected)
|
||||
with RGBLED(r, g, b) as device:
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
device._blink_thread.join()
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.8),
|
||||
(0.04, 1),
|
||||
(0.04, 0.8),
|
||||
(0.04, 0.6),
|
||||
(0.04, 0.4),
|
||||
(0.04, 0.2),
|
||||
(0.04, 0),
|
||||
]
|
||||
r.assert_states_and_times(expected)
|
||||
g.assert_states_and_times(expected)
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_output_rgbled_blink_interrupt():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
device = RGBLED(r, g, b)
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
r.assert_states([0, 1, 0])
|
||||
g.assert_states([0, 1, 0])
|
||||
b.assert_states([0, 1, 0])
|
||||
with RGBLED(r, g, b) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
r.assert_states([0, 1, 0])
|
||||
g.assert_states([0, 1, 0])
|
||||
b.assert_states([0, 1, 0])
|
||||
|
||||
def test_motor_missing_pins():
|
||||
with pytest.raises(ValueError):
|
||||
@@ -428,55 +426,55 @@ def test_motor_missing_pins():
|
||||
def test_motor_pins():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
device = Motor(f, b)
|
||||
assert device.forward_device.pin is f
|
||||
assert device.backward_device.pin is b
|
||||
with Motor(f, b) as device:
|
||||
assert device.forward_device.pin is f
|
||||
assert device.backward_device.pin is b
|
||||
|
||||
def test_motor_close():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
device = Motor(f, b)
|
||||
device.close()
|
||||
assert device.closed
|
||||
assert device.forward_device.pin is None
|
||||
assert device.backward_device.pin is None
|
||||
with Motor(f, b) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
assert device.forward_device.pin is None
|
||||
assert device.backward_device.pin is None
|
||||
|
||||
def test_motor_value():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
device = Motor(f, b)
|
||||
device.value = -1
|
||||
assert device.is_active
|
||||
assert device.value == -1
|
||||
assert b.state == 1 and f.state == 0
|
||||
device.value = 1
|
||||
assert device.is_active
|
||||
assert device.value == 1
|
||||
assert b.state == 0 and f.state == 1
|
||||
device.value = 0.5
|
||||
assert device.is_active
|
||||
assert device.value == 0.5
|
||||
assert b.state == 0 and f.state == 0.5
|
||||
device.value = 0
|
||||
assert not device.is_active
|
||||
assert not device.value
|
||||
assert b.state == 0 and f.state == 0
|
||||
with Motor(f, b) as device:
|
||||
device.value = -1
|
||||
assert device.is_active
|
||||
assert device.value == -1
|
||||
assert b.state == 1 and f.state == 0
|
||||
device.value = 1
|
||||
assert device.is_active
|
||||
assert device.value == 1
|
||||
assert b.state == 0 and f.state == 1
|
||||
device.value = 0.5
|
||||
assert device.is_active
|
||||
assert device.value == 0.5
|
||||
assert b.state == 0 and f.state == 0.5
|
||||
device.value = 0
|
||||
assert not device.is_active
|
||||
assert not device.value
|
||||
assert b.state == 0 and f.state == 0
|
||||
|
||||
def test_motor_bad_value():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
device = Motor(f, b)
|
||||
with pytest.raises(ValueError):
|
||||
device.value = 2
|
||||
with Motor(f, b) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = 2
|
||||
|
||||
def test_motor_reverse():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
device = Motor(f, b)
|
||||
device.forward()
|
||||
assert device.value == 1
|
||||
assert b.state == 0 and f.state == 1
|
||||
device.reverse()
|
||||
assert device.value == -1
|
||||
assert b.state == 1 and f.state == 0
|
||||
with Motor(f, b) as device:
|
||||
device.forward()
|
||||
assert device.value == 1
|
||||
assert b.state == 0 and f.state == 1
|
||||
device.reverse()
|
||||
assert device.value == -1
|
||||
assert b.state == 1 and f.state == 0
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import pytest
|
||||
from math import sin, cos, radians
|
||||
from time import time
|
||||
|
||||
from gpiozero import *
|
||||
from gpiozero.tools import *
|
||||
try:
|
||||
from math import isclose
|
||||
except ImportError:
|
||||
Reference in New Issue
Block a user