Permit replacement of pin_factory without closing old factory. However,
continue closing devices associated with extant pin factory at script
termination.
This commit is contained in:
Dave Jones
2017-06-22 22:45:00 +01:00
parent 6e71f20aa6
commit a99e0746c3
16 changed files with 328 additions and 338 deletions

View File

@@ -17,12 +17,12 @@ from gpiozero import *
def teardown_function(function):
Device._pin_factory.reset()
Device.pin_factory.reset()
Device._reservations.clear()
def test_input_initial_values():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with InputDevice(pin, pull_up=True) as device:
assert pin.function == 'input'
assert pin.pull == 'up'
@@ -32,7 +32,7 @@ def test_input_initial_values():
assert not device.pull_up
def test_input_is_active_low():
pin = Device._pin_factory.pin(2)
pin = Device.pin_factory.pin(2)
with InputDevice(pin, pull_up=True) as device:
pin.drive_high()
assert not device.is_active
@@ -42,7 +42,7 @@ def test_input_is_active_low():
assert repr(device) == '<gpiozero.InputDevice object on pin GPIO2, pull_up=True, is_active=True>'
def test_input_is_active_high():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with InputDevice(pin, pull_up=False) as device:
pin.drive_high()
assert device.is_active
@@ -52,13 +52,13 @@ def test_input_is_active_high():
assert repr(device) == '<gpiozero.InputDevice object on pin GPIO4, pull_up=False, is_active=False>'
def test_input_pulled_up():
pin = Device._pin_factory.pin(2)
pin = Device.pin_factory.pin(2)
with pytest.raises(PinFixedPull):
InputDevice(pin, pull_up=False)
def test_input_event_activated():
event = Event()
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with DigitalInputDevice(pin) as device:
device.when_activated = lambda: event.set()
assert not event.is_set()
@@ -67,7 +67,7 @@ def test_input_event_activated():
def test_input_event_deactivated():
event = Event()
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with DigitalInputDevice(pin) as device:
device.when_deactivated = lambda: event.set()
assert not event.is_set()
@@ -78,7 +78,7 @@ def test_input_event_deactivated():
def test_input_partial_callback():
event = Event()
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
def foo(a, b):
event.set()
return a + b
@@ -91,20 +91,20 @@ def test_input_partial_callback():
assert event.is_set()
def test_input_wait_active():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
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 = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with DigitalInputDevice(pin) as device:
assert device.wait_for_inactive(1)
assert not device.wait_for_active(0)
def test_input_smoothed_attrib():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with SmoothedInputDevice(pin, threshold=0.5, queue_len=5, partial=False) as device:
assert repr(device) == '<gpiozero.SmoothedInputDevice object on pin GPIO4, pull_up=False>'
assert device.threshold == 0.5
@@ -116,7 +116,7 @@ def test_input_smoothed_attrib():
device.threshold = 1
def test_input_smoothed_values():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with SmoothedInputDevice(pin) as device:
device._queue.start()
assert not device.is_active
@@ -126,7 +126,7 @@ def test_input_smoothed_values():
assert device.wait_for_inactive(1)
def test_input_button():
pin = Device._pin_factory.pin(2)
pin = Device.pin_factory.pin(2)
with Button(pin) as button:
assert pin.pull == 'up'
assert not button.is_pressed
@@ -138,7 +138,7 @@ def test_input_button():
assert button.wait_for_release(1)
def test_input_line_sensor():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with LineSensor(pin) as sensor:
pin.drive_low() # logic is inverted for line sensor
assert sensor.wait_for_line(1)
@@ -148,7 +148,7 @@ def test_input_line_sensor():
assert not sensor.line_detected
def test_input_motion_sensor():
pin = Device._pin_factory.pin(4)
pin = Device.pin_factory.pin(4)
with MotionSensor(pin) as sensor:
pin.drive_high()
assert sensor.wait_for_motion(1)
@@ -160,7 +160,7 @@ def test_input_motion_sensor():
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
reason='timing is too random on pypy')
def test_input_light_sensor():
pin = Device._pin_factory.pin(4, pin_class=MockChargingPin)
pin = Device.pin_factory.pin(4, pin_class=MockChargingPin)
with LightSensor(pin) as sensor:
pin.charge_time = 0.1
assert sensor.wait_for_dark(1)
@@ -170,8 +170,8 @@ def test_input_light_sensor():
@pytest.mark.skipif(hasattr(sys, 'pypy_version_info'),
reason='timing is too random on pypy')
def test_input_distance_sensor():
echo_pin = Device._pin_factory.pin(4)
trig_pin = Device._pin_factory.pin(5, pin_class=MockTriggerPin, echo_pin=echo_pin, echo_time=0.02)
echo_pin = Device.pin_factory.pin(4)
trig_pin = Device.pin_factory.pin(5, pin_class=MockTriggerPin, echo_pin=echo_pin, echo_time=0.02)
with pytest.raises(ValueError):
DistanceSensor(echo_pin, trig_pin, max_distance=-1)
# normal queue len is large (because the sensor is *really* jittery) but