mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-12-08 20:39:01 +00:00
Fix #459 - properly support remote SPI with pigpio
Sorry! Dave's messing around with the pin implementations again. Hopefully the last time. The pin_factory is now really a factory object which can be asked to produce individual pins or pin-based interfaces like SPI (which can be supported properly via pigpio).
This commit is contained in:
@@ -16,15 +16,16 @@ except ImportError:
|
||||
|
||||
import pytest
|
||||
|
||||
from gpiozero.pins.mock import MockPin, MockPWMPin
|
||||
from gpiozero.pins.mock import MockPWMPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def teardown_function(function):
|
||||
MockPin.clear_pins()
|
||||
Device._pin_factory.reset()
|
||||
|
||||
|
||||
def test_output_initial_values():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin, initial_value=False) as device:
|
||||
assert pin.function == 'output'
|
||||
assert not pin.state
|
||||
@@ -35,7 +36,7 @@ def test_output_initial_values():
|
||||
assert state == pin.state
|
||||
|
||||
def test_output_write_active_high():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
device.on()
|
||||
assert pin.state
|
||||
@@ -43,7 +44,7 @@ def test_output_write_active_high():
|
||||
assert not pin.state
|
||||
|
||||
def test_output_write_active_low():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin, active_high=False) as device:
|
||||
device.on()
|
||||
assert not pin.state
|
||||
@@ -51,7 +52,7 @@ def test_output_write_active_low():
|
||||
assert pin.state
|
||||
|
||||
def test_output_write_closed():
|
||||
with OutputDevice(MockPin(2)) as device:
|
||||
with OutputDevice(Device._pin_factory.pin(2)) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
device.close()
|
||||
@@ -60,14 +61,14 @@ def test_output_write_closed():
|
||||
device.on()
|
||||
|
||||
def test_output_write_silly():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.on()
|
||||
|
||||
def test_output_value():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
@@ -79,7 +80,7 @@ def test_output_value():
|
||||
assert not pin.state
|
||||
|
||||
def test_output_digital_toggle():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
@@ -93,7 +94,7 @@ def test_output_digital_toggle():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_blink_background():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -111,7 +112,7 @@ def test_output_blink_background():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_blink_foreground():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -125,7 +126,7 @@ def test_output_blink_foreground():
|
||||
])
|
||||
|
||||
def test_output_blink_interrupt_on():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -133,7 +134,7 @@ def test_output_blink_interrupt_on():
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_blink_interrupt_off():
|
||||
pin = MockPin(2)
|
||||
pin = Device._pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
device.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
@@ -142,14 +143,14 @@ def test_output_blink_interrupt_off():
|
||||
|
||||
def test_output_pwm_bad_initial_value():
|
||||
with pytest.raises(ValueError):
|
||||
PWMOutputDevice(MockPin(2), initial_value=2)
|
||||
PWMOutputDevice(Device._pin_factory.pin(2), initial_value=2)
|
||||
|
||||
def test_output_pwm_not_supported():
|
||||
with pytest.raises(AttributeError):
|
||||
PWMOutputDevice(MockPin(2))
|
||||
PWMOutputDevice(Device._pin_factory.pin(2))
|
||||
|
||||
def test_output_pwm_states():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.value = 0.1
|
||||
device.value = 0.2
|
||||
@@ -157,7 +158,7 @@ def test_output_pwm_states():
|
||||
pin.assert_states([0.0, 0.1, 0.2, 0.0])
|
||||
|
||||
def test_output_pwm_read():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin, frequency=100) as device:
|
||||
assert device.frequency == 100
|
||||
device.value = 0.1
|
||||
@@ -170,14 +171,14 @@ def test_output_pwm_read():
|
||||
assert device.frequency is None
|
||||
|
||||
def test_output_pwm_write():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.on()
|
||||
device.off()
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_pwm_toggle():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.toggle()
|
||||
device.value = 0.5
|
||||
@@ -187,7 +188,7 @@ def test_output_pwm_toggle():
|
||||
pin.assert_states([False, True, 0.5, 0.1, 0.9, False])
|
||||
|
||||
def test_output_pwm_active_high_read():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin, active_high=False) as device:
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
@@ -196,17 +197,18 @@ def test_output_pwm_active_high_read():
|
||||
assert device.value
|
||||
|
||||
def test_output_pwm_bad_value():
|
||||
with pytest.raises(ValueError):
|
||||
PWMOutputDevice(MockPWMPin(2)).value = 2
|
||||
with PWMOutputDevice(Device._pin_factory.pin(2, pin_class=MockPWMPin)) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = 2
|
||||
|
||||
def test_output_pwm_write_closed():
|
||||
device = PWMOutputDevice(MockPWMPin(2))
|
||||
device.close()
|
||||
with pytest.raises(GPIODeviceClosed):
|
||||
device.on()
|
||||
with PWMOutputDevice(Device._pin_factory.pin(2, pin_class=MockPWMPin)) as device:
|
||||
device.close()
|
||||
with pytest.raises(GPIODeviceClosed):
|
||||
device.on()
|
||||
|
||||
def test_output_pwm_write_silly():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
@@ -215,7 +217,7 @@ def test_output_pwm_write_silly():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_blink_background():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -233,7 +235,7 @@ def test_output_pwm_blink_background():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_blink_foreground():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -249,7 +251,7 @@ def test_output_pwm_blink_foreground():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_fade_background():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
@@ -283,7 +285,7 @@ def test_output_pwm_fade_background():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_fade_foreground():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
@@ -315,7 +317,7 @@ def test_output_pwm_fade_foreground():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_pulse_background():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
@@ -349,7 +351,7 @@ def test_output_pwm_pulse_background():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_pwm_pulse_foreground():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2, background=False)
|
||||
@@ -379,7 +381,7 @@ def test_output_pwm_pulse_foreground():
|
||||
])
|
||||
|
||||
def test_output_pwm_blink_interrupt():
|
||||
pin = MockPWMPin(2)
|
||||
pin = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -391,7 +393,7 @@ def test_rgbled_missing_pins():
|
||||
RGBLED()
|
||||
|
||||
def test_rgbled_initial_value():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, initial_value=(0.1, 0.2, 0)) as device:
|
||||
assert r.frequency
|
||||
assert g.frequency
|
||||
@@ -401,24 +403,24 @@ def test_rgbled_initial_value():
|
||||
assert isclose(b.state, 0.0)
|
||||
|
||||
def test_rgbled_initial_value_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False, initial_value=(0, 1, 1)) as device:
|
||||
assert r.state == 0
|
||||
assert g.state == 1
|
||||
assert b.state == 1
|
||||
|
||||
def test_rgbled_initial_bad_value():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with pytest.raises(ValueError):
|
||||
RGBLED(r, g, b, initial_value=(0.1, 0.2, 1.2))
|
||||
|
||||
def test_rgbled_initial_bad_value_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with pytest.raises(ValueError):
|
||||
RGBLED(r, g, b, pwm=False, initial_value=(0.1, 0.2, 0))
|
||||
|
||||
def test_rgbled_value():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
assert isinstance(device._leds[0], PWMLED)
|
||||
assert isinstance(device._leds[1], PWMLED)
|
||||
@@ -436,7 +438,7 @@ def test_rgbled_value():
|
||||
assert device.value == (0.5, 0.5, 0.5)
|
||||
|
||||
def test_rgbled_value_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
assert isinstance(device._leds[0], LED)
|
||||
assert isinstance(device._leds[1], LED)
|
||||
@@ -451,7 +453,7 @@ def test_rgbled_value_nonpwm():
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_bad_value():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (2, 0, 0)
|
||||
@@ -460,7 +462,7 @@ def test_rgbled_bad_value():
|
||||
device.value = (0, -1, 0)
|
||||
|
||||
def test_rgbled_bad_value_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (2, 0, 0)
|
||||
@@ -478,7 +480,7 @@ def test_rgbled_bad_value_nonpwm():
|
||||
device.value = (0, 0, 0.5)
|
||||
|
||||
def test_rgbled_toggle():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
@@ -490,7 +492,7 @@ def test_rgbled_toggle():
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_toggle_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
@@ -501,10 +503,18 @@ def test_rgbled_toggle_nonpwm():
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_blink_nonpwm():
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(fade_in_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(fade_out_time=1)
|
||||
|
||||
@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))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -525,7 +535,7 @@ def test_rgbled_blink_background():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_background_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
@@ -546,7 +556,7 @@ def test_rgbled_blink_background_nonpwm():
|
||||
@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))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -565,7 +575,7 @@ def test_rgbled_blink_foreground():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_foreground_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
@@ -584,7 +594,7 @@ def test_rgbled_blink_foreground_nonpwm():
|
||||
@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))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) 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)
|
||||
@@ -619,7 +629,7 @@ def test_rgbled_fade_background():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_fade_background_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
@@ -627,7 +637,7 @@ def test_rgbled_fade_background_nonpwm():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_fade_foreground():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) 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, background=False)
|
||||
@@ -660,7 +670,7 @@ def test_rgbled_fade_foreground():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_fade_foreground_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
@@ -668,7 +678,7 @@ def test_rgbled_fade_foreground_nonpwm():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_pulse_background():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
@@ -703,7 +713,7 @@ def test_rgbled_pulse_background():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_pulse_background_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
@@ -711,7 +721,7 @@ def test_rgbled_pulse_background_nonpwm():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_pulse_foreground():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) 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)
|
||||
@@ -744,13 +754,13 @@ def test_rgbled_pulse_foreground():
|
||||
b.assert_states_and_times(expected)
|
||||
|
||||
def test_rgbled_pulse_foreground_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.pulse(0.2, 0.2, n=2, background=False)
|
||||
|
||||
def test_rgbled_blink_interrupt():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -760,7 +770,7 @@ def test_rgbled_blink_interrupt():
|
||||
b.assert_states([0, 1, 0])
|
||||
|
||||
def test_rgbled_blink_interrupt_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
@@ -770,7 +780,7 @@ def test_rgbled_blink_interrupt_nonpwm():
|
||||
b.assert_states([0, 1, 0])
|
||||
|
||||
def test_rgbled_close():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
assert not device.closed
|
||||
device.close()
|
||||
@@ -779,7 +789,7 @@ def test_rgbled_close():
|
||||
assert device.closed
|
||||
|
||||
def test_rgbled_close_nonpwm():
|
||||
r, g, b = (MockPin(i) for i in (1, 2, 3))
|
||||
r, g, b = (Device._pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
assert not device.closed
|
||||
device.close()
|
||||
@@ -792,8 +802,8 @@ def test_motor_missing_pins():
|
||||
Motor()
|
||||
|
||||
def test_motor_pins():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
f = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
assert device.forward_device.pin is f
|
||||
assert isinstance(device.forward_device, PWMOutputDevice)
|
||||
@@ -801,8 +811,8 @@ def test_motor_pins():
|
||||
assert isinstance(device.backward_device, PWMOutputDevice)
|
||||
|
||||
def test_motor_pins_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(2)
|
||||
f = Device._pin_factory.pin(1)
|
||||
b = Device._pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
assert device.forward_device.pin is f
|
||||
assert isinstance(device.forward_device, DigitalOutputDevice)
|
||||
@@ -810,8 +820,8 @@ def test_motor_pins_nonpwm():
|
||||
assert isinstance(device.backward_device, DigitalOutputDevice)
|
||||
|
||||
def test_motor_close():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
f = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
@@ -821,8 +831,8 @@ def test_motor_close():
|
||||
assert device.closed
|
||||
|
||||
def test_motor_close_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(2)
|
||||
f = Device._pin_factory.pin(1)
|
||||
b = Device._pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
@@ -830,8 +840,8 @@ def test_motor_close_nonpwm():
|
||||
assert device.backward_device.pin is None
|
||||
|
||||
def test_motor_value():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
f = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
device.value = -1
|
||||
assert device.is_active
|
||||
@@ -855,8 +865,8 @@ def test_motor_value():
|
||||
assert b.state == 0 and f.state == 0
|
||||
|
||||
def test_motor_value_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(2)
|
||||
f = Device._pin_factory.pin(1)
|
||||
b = Device._pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
device.value = -1
|
||||
assert device.is_active
|
||||
@@ -872,17 +882,21 @@ def test_motor_value_nonpwm():
|
||||
assert b.state == 0 and f.state == 0
|
||||
|
||||
def test_motor_bad_value():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
f = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = -2
|
||||
with pytest.raises(ValueError):
|
||||
device.value = 2
|
||||
with pytest.raises(ValueError):
|
||||
device.forward(2)
|
||||
with pytest.raises(ValueError):
|
||||
device.backward(2)
|
||||
|
||||
def test_motor_bad_value_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(2)
|
||||
f = Device._pin_factory.pin(1)
|
||||
b = Device._pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = -2
|
||||
@@ -894,8 +908,8 @@ def test_motor_bad_value_nonpwm():
|
||||
device.value = -0.5
|
||||
|
||||
def test_motor_reverse():
|
||||
f = MockPWMPin(1)
|
||||
b = MockPWMPin(2)
|
||||
f = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
device.forward()
|
||||
assert device.value == 1
|
||||
@@ -911,8 +925,8 @@ def test_motor_reverse():
|
||||
assert b.state == 0 and f.state == 0.5
|
||||
|
||||
def test_motor_reverse_nonpwm():
|
||||
f = MockPin(1)
|
||||
b = MockPin(2)
|
||||
f = Device._pin_factory.pin(1)
|
||||
b = Device._pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
device.forward()
|
||||
assert device.value == 1
|
||||
@@ -922,13 +936,13 @@ def test_motor_reverse_nonpwm():
|
||||
assert b.state == 1 and f.state == 0
|
||||
|
||||
def test_servo_pins():
|
||||
p = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
assert device.pwm_device.pin is p
|
||||
assert isinstance(device.pwm_device, PWMOutputDevice)
|
||||
|
||||
def test_servo_bad_value():
|
||||
p = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with pytest.raises(ValueError):
|
||||
Servo(p, initial_value=2)
|
||||
with pytest.raises(ValueError):
|
||||
@@ -937,12 +951,12 @@ def test_servo_bad_value():
|
||||
Servo(p, max_pulse_width=30/1000)
|
||||
|
||||
def test_servo_pins_nonpwm():
|
||||
p = MockPin(2)
|
||||
p = Device._pin_factory.pin(2)
|
||||
with pytest.raises(PinPWMUnsupported):
|
||||
Servo(p)
|
||||
|
||||
def test_servo_close():
|
||||
p = MockPWMPin(2)
|
||||
p = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
@@ -951,7 +965,7 @@ def test_servo_close():
|
||||
assert device.closed
|
||||
|
||||
def test_servo_pulse_width():
|
||||
p = MockPWMPin(2)
|
||||
p = Device._pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Servo(p, min_pulse_width=5/10000, max_pulse_width=25/10000) as device:
|
||||
assert isclose(device.min_pulse_width, 5/10000)
|
||||
assert isclose(device.max_pulse_width, 25/10000)
|
||||
@@ -965,7 +979,7 @@ def test_servo_pulse_width():
|
||||
assert device.pulse_width is None
|
||||
|
||||
def test_servo_values():
|
||||
p = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
device.min()
|
||||
assert device.is_active
|
||||
@@ -992,13 +1006,13 @@ def test_servo_values():
|
||||
assert device.value is None
|
||||
|
||||
def test_angular_servo_range():
|
||||
p = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with AngularServo(p, initial_angle=15, min_angle=0, max_angle=90) as device:
|
||||
assert device.min_angle == 0
|
||||
assert device.max_angle == 90
|
||||
|
||||
def test_angular_servo_angles():
|
||||
p = MockPWMPin(1)
|
||||
p = Device._pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with AngularServo(p) as device:
|
||||
device.angle = 0
|
||||
assert device.angle == 0
|
||||
|
||||
Reference in New Issue
Block a user