def test_output_pwm_blink_interrupt(): pin = MockPWMPin(2) 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_led_bar_graph_pwm_value(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) with LEDBarGraph(pin1, pin2, pin3, pwm=True) as graph: assert isinstance(graph[0], PWMLED) assert isinstance(graph[1], PWMLED) assert isinstance(graph[2], PWMLED) graph.value = 0 assert graph.value == 0 assert not any((pin1.state, pin2.state, pin3.state)) graph.value = 1 assert graph.value == 1 assert all((pin1.state, pin2.state, pin3.state)) graph.value = 1/3 assert graph.value == 1/3 assert pin1.state and not (pin2.state or pin3.state) graph.value = -1/3 assert graph.value == -1/3 assert pin3.state and not (pin1.state or pin2.state) graph.value = 1/2 assert 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_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), ])
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), ])
def test_output_pwm_pulse_foreground(): pin = MockPWMPin(2) with PWMOutputDevice(pin) as device: start = time() device.pulse(0.2, 0.2, n=2, background=False) assert isclose(time() - start, 0.8, abs_tol=0.05) 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_states(): pin = MockPWMPin(2) 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_fade_background(): pin = MockPWMPin(2) with PWMOutputDevice(pin) as device: start = time() device.blink(0, 0, 0.2, 0.2, n=2) assert isclose(time() - start, 0, abs_tol=0.05) device._blink_thread.join() assert isclose(time() - start, 0.8, abs_tol=0.05) 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_mock_pin_init_twice_different_modes(): pin1 = MockPin(2) pin2 = MockPWMPin(pin1.number+1) assert pin1 != pin2 with pytest.raises(ValueError): pin3 = MockPWMPin(pin1.number) with pytest.raises(ValueError): pin4 = MockPin(pin2.number)
def test_led_board_pwm_bad_initial_value(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) with pytest.raises(ValueError): LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=-1) with pytest.raises(ValueError): LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=2)
def test_mock_pin_frequency_supported(): pin = MockPWMPin(3) pin.function = 'output' assert pin.frequency is None pin.frequency = 100 pin.state = 0.5 pin.frequency = None assert not pin.state
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
def test_output_pwm_blink_foreground(): pin = MockPWMPin(2) with PWMOutputDevice(pin) as device: start = time() device.blink(0.1, 0.1, n=2, background=False) assert isclose(time() - start, 0.4, abs_tol=0.05) pin.assert_states_and_times([(0.0, 0), (0.0, 1), (0.1, 0), (0.1, 1), (0.1, 0)])
def test_motor_pins(): f = MockPWMPin(1) b = MockPWMPin(2) with Motor(f, b) as device: assert device.forward_device.pin is f assert isinstance(device.forward_device, PWMOutputDevice) assert device.backward_device.pin is b assert isinstance(device.backward_device, PWMOutputDevice)
def test_motor_bad_value(): f = MockPWMPin(1) b = MockPWMPin(2) with Motor(f, b) as device: with pytest.raises(ValueError): device.value = -2 with pytest.raises(ValueError): device.value = 2
def test_led_board_pwm_bad_value(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) with LEDBoard(pin1, pin2, foo=pin3, pwm=True) as board: with pytest.raises(ValueError): board.value = (-1, 0, 0) with pytest.raises(ValueError): board.value = (0, 2, 0)
def test_output_pwm_toggle(): pin = MockPWMPin(2) 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_led_board_pwm_initial_value(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) with LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=0) as board: assert board.value == (0, 0, 0) with LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=1) as board: assert board.value == (1, 1, 1) with LEDBoard(pin1, pin2, foo=pin3, pwm=True, initial_value=0.5) as board: assert board.value == (0.5, 0.5, 0.5)
def test_led_bar_graph_pwm_initial_value(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) with LEDBarGraph(pin1, pin2, pin3, pwm=True, initial_value=0.5) as graph: assert graph.value == 0.5 assert (pin1.state, pin2.state, pin3.state) == (1, 0.5, 0) with LEDBarGraph(pin1, pin2, pin3, pwm=True, initial_value=-0.5) as graph: assert graph.value == -0.5 assert (pin1.state, pin2.state, pin3.state) == (0, 0.5, 1)
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
def test_led_board_pwm_value(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) with LEDBoard(pin1, pin2, foo=pin3, pwm=True) as board: assert board.value == (0, 0, 0) board.value = (0, 1, 0) assert board.value == (0, 1, 0) board.value = (0.5, 0, 0.75) assert board.value == (0.5, 0, 0.75)
def test_led_board_fade_background(): pin1 = MockPWMPin(2) pin2 = MockPWMPin(3) pin3 = MockPWMPin(4) 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_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) ])
def test_output_pwm_blink_background(): pin = MockPWMPin(2) 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) ])
def test_output_pwm_blink_foreground(): pin = MockPWMPin(2) with PWMOutputDevice(pin) as device: start = time() device.blink(0.1, 0.1, n=2, background=False) assert isclose(time() - start, 0.4, abs_tol=0.05) pin.assert_states_and_times([ (0.0, 0), (0.0, 1), (0.1, 0), (0.1, 1), (0.1, 0) ])
def test_motor_reverse(): f = MockPWMPin(1) b = MockPWMPin(2) 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 device.backward(0.5) assert device.value == -0.5 assert b.state == 0.5 and f.state == 0 device.reverse() assert device.value == 0.5 assert b.state == 0 and f.state == 0.5
def test_robot(): pins = [MockPWMPin(n) for n in (2, 3, 4, 5)] 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 assert robot.value == (0, 0) robot.forward() assert [pin.state for pin in pins] == [1, 0, 1, 0] assert robot.value == (1, 1) robot.backward() assert [pin.state for pin in pins] == [0, 1, 0, 1] assert robot.value == (-1, -1) robot.forward(0.5) assert [pin.state for pin in pins] == [0.5, 0, 0.5, 0] assert robot.value == (0.5, 0.5) robot.left() assert [pin.state for pin in pins] == [0, 1, 1, 0] assert robot.value == (-1, 1) robot.right() assert [pin.state for pin in pins] == [1, 0, 0, 1] assert robot.value == (1, -1) robot.reverse() assert [pin.state for pin in pins] == [0, 1, 1, 0] assert robot.value == (-1, 1) robot.stop() assert [pin.state for pin in pins] == [0, 0, 0, 0] assert robot.value == (0, 0) robot.value = (-1, -1) assert robot.value == (-1, -1) robot.value = (0.5, 1) assert robot.value == (0.5, 1) robot.value = (0, -0.5) assert robot.value == (0, -0.5)
def test_angular_servo_angles(): p = MockPWMPin(1) with AngularServo(p) as device: device.angle = 0 assert device.angle == 0 assert isclose(device.value, 0) device.max() assert device.angle == 90 assert isclose(device.value, 1) device.min() assert device.angle == -90 assert isclose(device.value, -1) device.detach() assert device.angle is None with AngularServo(p, initial_angle=15, min_angle=0, max_angle=90) as device: assert device.angle == 15 assert isclose(device.value, -2 / 3) device.angle = 0 assert device.angle == 0 assert isclose(device.value, -1) device.angle = 90 assert device.angle == 90 assert isclose(device.value, 1) device.angle = None assert device.angle is None with AngularServo(p, min_angle=45, max_angle=-45) as device: assert device.angle == 0 assert isclose(device.value, 0) device.angle = -45 assert device.angle == -45 assert isclose(device.value, 1) device.angle = -15 assert device.angle == -15 assert isclose(device.value, 1 / 3)
def test_rgbled_pulse_foreground(): r, g, b = (MockPWMPin(i) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: start = time() device.pulse(0.2, 0.2, n=2, background=False) assert isclose(time() - start, 0.8, abs_tol=0.05) 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_rgbled_fade_background(): r, g, b = (MockPWMPin(i) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: start = time() device.blink(0, 0, 0.2, 0.2, n=2) assert isclose(time() - start, 0, abs_tol=0.05) device._blink_thread.join() assert isclose(time() - start, 0.8, abs_tol=0.05) 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_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)
def test_servo_values(): p = MockPWMPin(1) with Servo(p) as device: device.min() assert device.is_active assert device.value == -1 assert isclose(p.state, 0.05) device.max() assert device.is_active assert device.value == 1 assert isclose(p.state, 0.1) device.mid() assert device.is_active assert device.value == 0.0 assert isclose(p.state, 0.075) device.value = 0.5 assert device.is_active assert device.value == 0.5 assert isclose(p.state, 0.0875) device.detach() assert not device.is_active assert device.value is None device.value = 0 assert device.value == 0 device.value = None assert device.value is None
def test_output_pwm_active_high_read(): pin = MockPWMPin(2) 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_rgbled_bad_value(): r, g, b = (MockPWMPin(i) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: with pytest.raises(ValueError): device.value = (2, 0, 0) with RGBLED(r, g, b) as device: with pytest.raises(ValueError): device.value = (0, -1, 0)
def test_rgbled_close(): r, g, b = (MockPWMPin(i) for i in (1, 2, 3)) with RGBLED(r, g, b) as device: assert not device.closed device.close() assert device.closed device.close() assert device.closed
def test_mock_pwm_pin_state(): pin = MockPWMPin(2) with pytest.raises(PinSetInput): pin.state = 1 pin.function = "output" assert pin.state == 0 pin.state = 1 assert pin.state == 1 pin.state = 0 assert pin.state == 0 pin.state = 0.5 assert pin.state == 0.5
def test_mock_pwm_pin_open_close(): pin = MockPWMPin(2) pin.close()
def test_output_pwm_write_silly(): pin = MockPWMPin(2) with PWMOutputDevice(pin) as device: pin.function = 'input' with pytest.raises(AttributeError): device.off()
def test_output_pwm_write(): pin = MockPWMPin(2) with PWMOutputDevice(pin) as device: device.on() device.off() pin.assert_states([False, True, False])