mirror of
https://github.com/pimoroni/grow-python
synced 2025-10-25 15:19:23 +00:00
Ensure pump actually stops and releases lock
This commit is contained in:
@@ -45,8 +45,12 @@ class Pump(object):
|
||||
"""Set pump speed (PWM duty cycle)."""
|
||||
if speed > 1.0 or speed < 0:
|
||||
raise ValueError("Speed must be between 0 and 1")
|
||||
if not global_lock.acquire(blocking=False):
|
||||
|
||||
if speed == 0:
|
||||
global_lock.release()
|
||||
elif not global_lock.acquire(blocking=False):
|
||||
return False
|
||||
|
||||
self._pwm.ChangeDutyCycle(int(PUMP_MAX_DUTY * speed))
|
||||
self._speed = speed
|
||||
return True
|
||||
@@ -57,11 +61,10 @@ class Pump(object):
|
||||
|
||||
def stop(self):
|
||||
"""Stop the pump."""
|
||||
self.set_speed(0)
|
||||
if self._timeout is not None:
|
||||
self._timeout.cancel()
|
||||
self._timeout = None
|
||||
global_lock.release()
|
||||
self.set_speed(0)
|
||||
|
||||
def dose(self, speed, timeout=0.1, blocking=True, force=False):
|
||||
"""Pulse the pump for timeout seconds.
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
import time
|
||||
|
||||
|
||||
def test_pumps_actually_stop(GPIO, smbus):
|
||||
from grow.pump import Pump, global_lock
|
||||
|
||||
ch1 = Pump(channel=1)
|
||||
|
||||
ch1.dose(speed=0.5, timeout=0.05, blocking=False)
|
||||
time.sleep(0.1)
|
||||
assert ch1.get_speed() == 0
|
||||
|
||||
|
||||
def test_pumps_are_mutually_exclusive(GPIO, smbus):
|
||||
from grow.pump import Pump, global_lock
|
||||
|
||||
|
||||
Reference in New Issue
Block a user