mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Fix #279 once and for all (ha! ;)
This implements the proposal discussed in the re-opened #279 to add a pin_factory argument at the device level and remove the ability to specify a pin instance to device constructors (they now only accept a pin specification). Note: there's still a couple of bits to tidy up (tests on "real" Pis, and pin_factory.release_all needs refinement) but the test suite is now at least capable of passing on a PC.
This commit is contained in:
@@ -16,29 +16,73 @@ except ImportError:
|
||||
|
||||
import pytest
|
||||
|
||||
from gpiozero.pins.mock import MockPWMPin
|
||||
from gpiozero.pins.mock import MockPin, MockPWMPin
|
||||
from gpiozero import *
|
||||
|
||||
|
||||
def setup_function(function):
|
||||
# dirty, but it does the job
|
||||
Device.pin_factory.pin_class = MockPWMPin if function.__name__ in (
|
||||
'test_output_pwm_states',
|
||||
'test_output_pwm_read',
|
||||
'test_output_pwm_write',
|
||||
'test_output_pwm_toggle',
|
||||
'test_output_pwm_active_high_read',
|
||||
'test_output_pwm_bad_value',
|
||||
'test_output_pwm_write_closed',
|
||||
'test_output_pwm_write_silly',
|
||||
'test_output_pwm_blink_background',
|
||||
'test_output_pwm_blink_foreground',
|
||||
'test_output_pwm_fade_background',
|
||||
'test_output_pwm_fade_foreground',
|
||||
'test_output_pwm_pulse_background',
|
||||
'test_output_pwm_pulse_foreground',
|
||||
'test_output_pwm_blink_interrupt',
|
||||
'test_rgbled_initial_value',
|
||||
'test_rgbled_initial_bad_value',
|
||||
'test_rgbled_value',
|
||||
'test_rgbled_bad_value',
|
||||
'test_rgbled_toggle',
|
||||
'test_rgbled_blink_background',
|
||||
'test_rgbled_blink_foreground',
|
||||
'test_rgbled_fade_background',
|
||||
'test_rgbled_fade_foreground',
|
||||
'test_rgbled_pulse_background',
|
||||
'test_rgbled_pulse_foreground',
|
||||
'test_rgbled_blink_interrupt',
|
||||
'test_rgbled_close',
|
||||
'test_motor_pins',
|
||||
'test_motor_close',
|
||||
'test_motor_value',
|
||||
'test_motor_bad_value',
|
||||
'test_motor_reverse',
|
||||
'test_servo_pins',
|
||||
'test_servo_bad_value',
|
||||
'test_servo_close',
|
||||
'test_servo_pulse_width',
|
||||
'test_servo_values',
|
||||
'test_angular_servo_range',
|
||||
'test_angular_servo_angles',
|
||||
) else MockPin
|
||||
|
||||
def teardown_function(function):
|
||||
Device.pin_factory.reset()
|
||||
Device._reservations.clear()
|
||||
|
||||
|
||||
def test_output_initial_values():
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with OutputDevice(pin, initial_value=False) as device:
|
||||
with OutputDevice(2, initial_value=False) as device:
|
||||
assert pin.function == 'output'
|
||||
assert not pin.state
|
||||
with OutputDevice(pin, initial_value=True) as device:
|
||||
with OutputDevice(2, initial_value=True) as device:
|
||||
assert pin.state
|
||||
state = pin.state
|
||||
with OutputDevice(pin, initial_value=None) as device:
|
||||
with OutputDevice(2, initial_value=None) as device:
|
||||
assert state == pin.state
|
||||
|
||||
def test_output_write_active_high():
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
with OutputDevice(2) as device:
|
||||
device.on()
|
||||
assert pin.state
|
||||
device.off()
|
||||
@@ -46,14 +90,14 @@ def test_output_write_active_high():
|
||||
|
||||
def test_output_write_active_low():
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with OutputDevice(pin, active_high=False) as device:
|
||||
with OutputDevice(2, active_high=False) as device:
|
||||
device.on()
|
||||
assert not pin.state
|
||||
device.off()
|
||||
assert pin.state
|
||||
|
||||
def test_output_write_closed():
|
||||
with OutputDevice(Device.pin_factory.pin(2)) as device:
|
||||
with OutputDevice(2) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
device.close()
|
||||
@@ -63,14 +107,14 @@ def test_output_write_closed():
|
||||
|
||||
def test_output_write_silly():
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
with OutputDevice(2) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.on()
|
||||
|
||||
def test_output_value():
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with OutputDevice(pin) as device:
|
||||
with OutputDevice(2) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
device.on()
|
||||
@@ -82,7 +126,7 @@ def test_output_value():
|
||||
|
||||
def test_output_digital_toggle():
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
with DigitalOutputDevice(2) as device:
|
||||
assert not device.value
|
||||
assert not pin.state
|
||||
device.toggle()
|
||||
@@ -96,7 +140,7 @@ def test_output_digital_toggle():
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_blink_background():
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
with DigitalOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -114,7 +158,7 @@ def test_output_blink_background():
|
||||
reason='timing is too random on pypy')
|
||||
def test_output_blink_foreground():
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
with DigitalOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
@@ -128,7 +172,7 @@ def test_output_blink_foreground():
|
||||
|
||||
def test_output_blink_interrupt_on():
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
with DigitalOutputDevice(4) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
@@ -136,7 +180,7 @@ def test_output_blink_interrupt_on():
|
||||
|
||||
def test_output_blink_interrupt_off():
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
with DigitalOutputDevice(4) as device:
|
||||
device.blink(0.1, 1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while off
|
||||
@@ -144,23 +188,23 @@ def test_output_blink_interrupt_off():
|
||||
|
||||
def test_output_pwm_bad_initial_value():
|
||||
with pytest.raises(ValueError):
|
||||
PWMOutputDevice(Device.pin_factory.pin(2), initial_value=2)
|
||||
PWMOutputDevice(2, initial_value=2)
|
||||
|
||||
def test_output_pwm_not_supported():
|
||||
with pytest.raises(AttributeError):
|
||||
PWMOutputDevice(Device.pin_factory.pin(2))
|
||||
PWMOutputDevice(2)
|
||||
|
||||
def test_output_pwm_states():
|
||||
pin = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) 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 = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin, frequency=100) as device:
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with PWMOutputDevice(2, frequency=100) as device:
|
||||
assert device.frequency == 100
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
@@ -172,15 +216,15 @@ def test_output_pwm_read():
|
||||
assert device.frequency is None
|
||||
|
||||
def test_output_pwm_write():
|
||||
pin = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
device.on()
|
||||
device.off()
|
||||
pin.assert_states([False, True, False])
|
||||
|
||||
def test_output_pwm_toggle():
|
||||
pin = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
device.toggle()
|
||||
device.value = 0.5
|
||||
device.value = 0.1
|
||||
@@ -189,8 +233,8 @@ 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 = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin, active_high=False) as device:
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with PWMOutputDevice(2, active_high=False) as device:
|
||||
device.value = 0.1
|
||||
assert isclose(device.value, 0.1)
|
||||
assert isclose(pin.state, 0.9)
|
||||
@@ -198,19 +242,21 @@ def test_output_pwm_active_high_read():
|
||||
assert device.value
|
||||
|
||||
def test_output_pwm_bad_value():
|
||||
with PWMOutputDevice(Device.pin_factory.pin(2, pin_class=MockPWMPin)) as device:
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with PWMOutputDevice(2) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = 2
|
||||
|
||||
def test_output_pwm_write_closed():
|
||||
with PWMOutputDevice(Device.pin_factory.pin(2, pin_class=MockPWMPin)) as device:
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with PWMOutputDevice(2) as device:
|
||||
device.close()
|
||||
with pytest.raises(GPIODeviceClosed):
|
||||
device.on()
|
||||
|
||||
def test_output_pwm_write_silly():
|
||||
pin = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(2)
|
||||
with PWMOutputDevice(2) as device:
|
||||
pin.function = 'input'
|
||||
with pytest.raises(AttributeError):
|
||||
device.off()
|
||||
@@ -218,8 +264,8 @@ 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 = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -236,8 +282,8 @@ 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 = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
@@ -252,8 +298,8 @@ 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 = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -286,8 +332,8 @@ 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 = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
assert isclose(time() - start, 0.8, abs_tol=0.05)
|
||||
@@ -318,8 +364,8 @@ 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 = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -352,8 +398,8 @@ 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 = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2, background=False)
|
||||
assert isclose(time() - start, 0.8, abs_tol=0.05)
|
||||
@@ -382,8 +428,8 @@ def test_output_pwm_pulse_foreground():
|
||||
])
|
||||
|
||||
def test_output_pwm_blink_interrupt():
|
||||
pin = Device.pin_factory.pin(4, pin_class=MockPWMPin)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
pin = Device.pin_factory.pin(4)
|
||||
with PWMOutputDevice(4) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
@@ -394,8 +440,8 @@ def test_rgbled_missing_pins():
|
||||
RGBLED()
|
||||
|
||||
def test_rgbled_initial_value():
|
||||
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:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(1, 2, 3, initial_value=(0.1, 0.2, 0)) as device:
|
||||
assert r.frequency
|
||||
assert g.frequency
|
||||
assert b.frequency
|
||||
@@ -405,24 +451,24 @@ def test_rgbled_initial_value():
|
||||
|
||||
def test_rgbled_initial_value_nonpwm():
|
||||
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:
|
||||
with RGBLED(1, 2, 3, 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 = (Device.pin_factory.pin(i, pin_class=MockPWMPin) 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, initial_value=(0.1, 0.2, 1.2))
|
||||
RGBLED(1, 2, 3, initial_value=(0.1, 0.2, 1.2))
|
||||
|
||||
def test_rgbled_initial_bad_value_nonpwm():
|
||||
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))
|
||||
RGBLED(1, 2, 3, pwm=False, initial_value=(0.1, 0.2, 0))
|
||||
|
||||
def test_rgbled_value():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
assert isinstance(device._leds[0], PWMLED)
|
||||
assert isinstance(device._leds[1], PWMLED)
|
||||
assert isinstance(device._leds[2], PWMLED)
|
||||
@@ -440,7 +486,7 @@ def test_rgbled_value():
|
||||
|
||||
def test_rgbled_value_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
assert isinstance(device._leds[0], LED)
|
||||
assert isinstance(device._leds[1], LED)
|
||||
assert isinstance(device._leds[2], LED)
|
||||
@@ -454,35 +500,35 @@ def test_rgbled_value_nonpwm():
|
||||
assert device.value == (0, 0, 0)
|
||||
|
||||
def test_rgbled_bad_value():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (2, 0, 0)
|
||||
with RGBLED(r, g, b) as device:
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (0, -1, 0)
|
||||
|
||||
def test_rgbled_bad_value_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (2, 0, 0)
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (0, -1, 0)
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (0.5, 0, 0)
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (0, 0.5, 0)
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = (0, 0, 0.5)
|
||||
|
||||
def test_rgbled_toggle():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
device.toggle()
|
||||
@@ -494,7 +540,7 @@ def test_rgbled_toggle():
|
||||
|
||||
def test_rgbled_toggle_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
assert not device.is_active
|
||||
assert device.value == (0, 0, 0)
|
||||
device.toggle()
|
||||
@@ -506,7 +552,7 @@ def test_rgbled_toggle_nonpwm():
|
||||
|
||||
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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(fade_in_time=1)
|
||||
with pytest.raises(ValueError):
|
||||
@@ -515,8 +561,8 @@ def test_rgbled_blink_nonpwm():
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_background():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -537,7 +583,7 @@ def test_rgbled_blink_background():
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_background_nonpwm():
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -557,8 +603,8 @@ 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 = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
@@ -577,7 +623,7 @@ def test_rgbled_blink_foreground():
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_blink_foreground_nonpwm():
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2, background=False)
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
@@ -595,8 +641,8 @@ 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 = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -631,15 +677,15 @@ def test_rgbled_fade_background():
|
||||
|
||||
def test_rgbled_fade_background_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(0, 0, 0.2, 0.2, n=2)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_fade_foreground():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
start = time()
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
assert isclose(time() - start, 0.8, abs_tol=0.05)
|
||||
@@ -672,15 +718,15 @@ def test_rgbled_fade_foreground():
|
||||
|
||||
def test_rgbled_fade_foreground_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.blink(0, 0, 0.2, 0.2, n=2, background=False)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_pulse_background():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
@@ -715,15 +761,15 @@ def test_rgbled_pulse_background():
|
||||
|
||||
def test_rgbled_pulse_background_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.pulse(0.2, 0.2, n=2)
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
def test_rgbled_pulse_foreground():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
start = time()
|
||||
device.pulse(0.2, 0.2, n=2, background=False)
|
||||
assert isclose(time() - start, 0.8, abs_tol=0.05)
|
||||
@@ -756,13 +802,13 @@ def test_rgbled_pulse_foreground():
|
||||
|
||||
def test_rgbled_pulse_foreground_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 RGBLED(1, 2, 3, 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 = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
@@ -772,7 +818,7 @@ def test_rgbled_blink_interrupt():
|
||||
|
||||
def test_rgbled_blink_interrupt_nonpwm():
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (4, 5, 6))
|
||||
with RGBLED(r, g, b, pwm=False) as device:
|
||||
with RGBLED(1, 2, 3, pwm=False) as device:
|
||||
device.blink(1, 0.1)
|
||||
sleep(0.2)
|
||||
device.off() # should interrupt while on
|
||||
@@ -781,8 +827,8 @@ def test_rgbled_blink_interrupt_nonpwm():
|
||||
b.assert_states([0, 1, 0])
|
||||
|
||||
def test_rgbled_close():
|
||||
r, g, b = (Device.pin_factory.pin(i, pin_class=MockPWMPin) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
r, g, b = (Device.pin_factory.pin(i) for i in (1, 2, 3))
|
||||
with RGBLED(1, 2, 3) as device:
|
||||
assert not device.closed
|
||||
device.close()
|
||||
assert device.closed
|
||||
@@ -791,7 +837,7 @@ def test_rgbled_close():
|
||||
|
||||
def test_rgbled_close_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 RGBLED(1, 2, 3, pwm=False) as device:
|
||||
assert not device.closed
|
||||
device.close()
|
||||
assert device.closed
|
||||
@@ -803,9 +849,9 @@ def test_motor_missing_pins():
|
||||
Motor()
|
||||
|
||||
def test_motor_pins():
|
||||
f = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(1, 2) as device:
|
||||
assert device.forward_device.pin is f
|
||||
assert isinstance(device.forward_device, PWMOutputDevice)
|
||||
assert device.backward_device.pin is b
|
||||
@@ -814,16 +860,16 @@ def test_motor_pins():
|
||||
def test_motor_pins_nonpwm():
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
with Motor(1, 2, pwm=False) as device:
|
||||
assert device.forward_device.pin is f
|
||||
assert isinstance(device.forward_device, DigitalOutputDevice)
|
||||
assert device.backward_device.pin is b
|
||||
assert isinstance(device.backward_device, DigitalOutputDevice)
|
||||
|
||||
def test_motor_close():
|
||||
f = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(1, 2) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
assert device.forward_device.pin is None
|
||||
@@ -834,16 +880,16 @@ def test_motor_close():
|
||||
def test_motor_close_nonpwm():
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
with Motor(1, 2, pwm=False) 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 = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(1, 2) as device:
|
||||
device.value = -1
|
||||
assert device.is_active
|
||||
assert device.value == -1
|
||||
@@ -868,7 +914,7 @@ def test_motor_value():
|
||||
def test_motor_value_nonpwm():
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
with Motor(1, 2, pwm=False) as device:
|
||||
device.value = -1
|
||||
assert device.is_active
|
||||
assert device.value == -1
|
||||
@@ -883,9 +929,9 @@ def test_motor_value_nonpwm():
|
||||
assert b.state == 0 and f.state == 0
|
||||
|
||||
def test_motor_bad_value():
|
||||
f = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(1, 2) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = -2
|
||||
with pytest.raises(ValueError):
|
||||
@@ -898,7 +944,7 @@ def test_motor_bad_value():
|
||||
def test_motor_bad_value_nonpwm():
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
with Motor(1, 2, pwm=False) as device:
|
||||
with pytest.raises(ValueError):
|
||||
device.value = -2
|
||||
with pytest.raises(ValueError):
|
||||
@@ -909,9 +955,9 @@ def test_motor_bad_value_nonpwm():
|
||||
device.value = -0.5
|
||||
|
||||
def test_motor_reverse():
|
||||
f = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
b = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Motor(f, b) as device:
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(1, 2) as device:
|
||||
device.forward()
|
||||
assert device.value == 1
|
||||
assert b.state == 0 and f.state == 1
|
||||
@@ -928,7 +974,7 @@ def test_motor_reverse():
|
||||
def test_motor_reverse_nonpwm():
|
||||
f = Device.pin_factory.pin(1)
|
||||
b = Device.pin_factory.pin(2)
|
||||
with Motor(f, b, pwm=False) as device:
|
||||
with Motor(1, 2, pwm=False) as device:
|
||||
device.forward()
|
||||
assert device.value == 1
|
||||
assert b.state == 0 and f.state == 1
|
||||
@@ -937,28 +983,28 @@ def test_motor_reverse_nonpwm():
|
||||
assert b.state == 1 and f.state == 0
|
||||
|
||||
def test_servo_pins():
|
||||
p = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
p = Device.pin_factory.pin(1)
|
||||
with Servo(1) as device:
|
||||
assert device.pwm_device.pin is p
|
||||
assert isinstance(device.pwm_device, PWMOutputDevice)
|
||||
|
||||
def test_servo_bad_value():
|
||||
p = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
p = Device.pin_factory.pin(1)
|
||||
with pytest.raises(ValueError):
|
||||
Servo(p, initial_value=2)
|
||||
Servo(1, initial_value=2)
|
||||
with pytest.raises(ValueError):
|
||||
Servo(p, min_pulse_width=30/1000)
|
||||
Servo(1, min_pulse_width=30/1000)
|
||||
with pytest.raises(ValueError):
|
||||
Servo(p, max_pulse_width=30/1000)
|
||||
Servo(1, max_pulse_width=30/1000)
|
||||
|
||||
def test_servo_pins_nonpwm():
|
||||
p = Device.pin_factory.pin(2)
|
||||
with pytest.raises(PinPWMUnsupported):
|
||||
Servo(p)
|
||||
Servo(1)
|
||||
|
||||
def test_servo_close():
|
||||
p = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
p = Device.pin_factory.pin(2)
|
||||
with Servo(1) as device:
|
||||
device.close()
|
||||
assert device.closed
|
||||
assert device.pwm_device.pin is None
|
||||
@@ -966,8 +1012,8 @@ def test_servo_close():
|
||||
assert device.closed
|
||||
|
||||
def test_servo_pulse_width():
|
||||
p = Device.pin_factory.pin(2, pin_class=MockPWMPin)
|
||||
with Servo(p, min_pulse_width=5/10000, max_pulse_width=25/10000) as device:
|
||||
p = Device.pin_factory.pin(2)
|
||||
with Servo(1, 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)
|
||||
assert isclose(device.frame_width, 20/1000)
|
||||
@@ -980,8 +1026,8 @@ def test_servo_pulse_width():
|
||||
assert device.pulse_width is None
|
||||
|
||||
def test_servo_values():
|
||||
p = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with Servo(p) as device:
|
||||
p = Device.pin_factory.pin(1)
|
||||
with Servo(1) as device:
|
||||
device.min()
|
||||
assert device.is_active
|
||||
assert device.value == -1
|
||||
@@ -1007,14 +1053,14 @@ def test_servo_values():
|
||||
assert device.value is None
|
||||
|
||||
def test_angular_servo_range():
|
||||
p = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with AngularServo(p, initial_angle=15, min_angle=0, max_angle=90) as device:
|
||||
p = Device.pin_factory.pin(1)
|
||||
with AngularServo(1, 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 = Device.pin_factory.pin(1, pin_class=MockPWMPin)
|
||||
with AngularServo(p) as device:
|
||||
p = Device.pin_factory.pin(1)
|
||||
with AngularServo(1) as device:
|
||||
device.angle = 0
|
||||
assert device.angle == 0
|
||||
assert isclose(device.value, 0)
|
||||
@@ -1026,7 +1072,7 @@ def test_angular_servo_angles():
|
||||
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:
|
||||
with AngularServo(1, initial_angle=15, min_angle=0, max_angle=90) as device:
|
||||
assert device.angle == 15
|
||||
assert isclose(device.value, -2/3)
|
||||
device.angle = 0
|
||||
@@ -1037,7 +1083,7 @@ def test_angular_servo_angles():
|
||||
assert isclose(device.value, 1)
|
||||
device.angle = None
|
||||
assert device.angle is None
|
||||
with AngularServo(p, min_angle=45, max_angle=-45) as device:
|
||||
with AngularServo(1, min_angle=45, max_angle=-45) as device:
|
||||
assert device.angle == 0
|
||||
assert isclose(device.value, 0)
|
||||
device.angle = -45
|
||||
|
||||
Reference in New Issue
Block a user