mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-12-08 20:39:01 +00:00
Add RGBLED.pulse method
Also add timing tests to all the blink unit-tests
This commit is contained in:
@@ -8,7 +8,7 @@ str = type('')
|
||||
|
||||
|
||||
import sys
|
||||
from time import sleep
|
||||
from time import sleep, time
|
||||
try:
|
||||
from math import isclose
|
||||
except ImportError:
|
||||
@@ -92,8 +92,11 @@ def test_output_digital_toggle():
|
||||
def test_output_blink_background():
|
||||
pin = MockPin(2)
|
||||
with DigitalOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
device._blink_thread.join() # naughty, but ensures no arbitrary waits in the test
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, False),
|
||||
(0.0, True),
|
||||
@@ -107,7 +110,9 @@ def test_output_blink_background():
|
||||
def test_output_blink_foreground():
|
||||
pin = MockPin(2)
|
||||
with DigitalOutputDevice(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, False),
|
||||
(0.0, True),
|
||||
@@ -209,8 +214,11 @@ def test_output_pwm_write_silly():
|
||||
def test_output_pwm_blink_background():
|
||||
pin = MockPWMPin(2)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
device._blink_thread.join()
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
pin.assert_states_and_times([
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
@@ -224,7 +232,9 @@ def test_output_pwm_blink_background():
|
||||
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),
|
||||
@@ -238,8 +248,11 @@ def test_output_pwm_blink_foreground():
|
||||
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),
|
||||
@@ -269,7 +282,75 @@ def test_output_pwm_fade_background():
|
||||
def test_output_pwm_fade_foreground():
|
||||
pin = MockPWMPin(2)
|
||||
with PWMOutputDevice(pin) 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)
|
||||
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_pulse_background():
|
||||
pin = MockPWMPin(2)
|
||||
with PWMOutputDevice(pin) as device:
|
||||
start = time()
|
||||
device.pulse(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),
|
||||
])
|
||||
|
||||
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
|
||||
reason='timing is too random on pypy')
|
||||
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),
|
||||
@@ -345,8 +426,11 @@ def test_rgbled_toggle():
|
||||
def test_rgbled_blink_background():
|
||||
r, g, b = (MockPWMPin(i) for i in (1, 2, 3))
|
||||
with RGBLED(r, g, b) as device:
|
||||
start = time()
|
||||
device.blink(0.1, 0.1, n=2)
|
||||
assert isclose(time() - start, 0, abs_tol=0.05)
|
||||
device._blink_thread.join()
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
@@ -363,7 +447,9 @@ def test_rgbled_blink_background():
|
||||
def test_rgbled_blink_foreground():
|
||||
r, g, b = (MockPWMPin(i) 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)
|
||||
assert isclose(time() - start, 0.4, abs_tol=0.05)
|
||||
expected = [
|
||||
(0.0, 0),
|
||||
(0.0, 1),
|
||||
@@ -380,8 +466,118 @@ def test_rgbled_blink_foreground():
|
||||
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)
|
||||
|
||||
@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))
|
||||
with RGBLED(r, g, b) 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)
|
||||
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)
|
||||
|
||||
@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))
|
||||
with RGBLED(r, g, b) as device:
|
||||
start = time()
|
||||
device.pulse(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)
|
||||
|
||||
@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))
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user