mirror of
https://github.com/pimoroni/grow-python
synced 2025-10-25 15:19:23 +00:00
Switch pump to class
This commit is contained in:
@@ -1,78 +1,63 @@
|
|||||||
import time
|
import time
|
||||||
import atexit
|
import atexit
|
||||||
|
import threading
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
|
|
||||||
PUMP_1_PIN = 17
|
PUMP_1_PIN = 17
|
||||||
PUMP_2_PIN = 27
|
PUMP_2_PIN = 27
|
||||||
PUMP_3_PIN = 22
|
PUMP_3_PIN = 22
|
||||||
|
PUMP_PWM_FREQ = 10000
|
||||||
_is_setup = False
|
PUMP_MAX_DUTY = 60
|
||||||
|
|
||||||
|
|
||||||
class Pump(object):
|
class Pump(object):
|
||||||
__slots__ = 'out1', 'out2', 'out3'
|
def __init__(self, channel):
|
||||||
|
self._gpio_pin = [PUMP_1_PIN, PUMP_2_PIN, PUMP_3_PIN][channel]
|
||||||
|
|
||||||
def __init__(self, out1, out2, out3):
|
|
||||||
self.out1 = out1
|
|
||||||
self.out2 = out2
|
|
||||||
self.out3 = out3
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
fmt = """Pump 1: {out1}
|
|
||||||
Pump 1: {out2} Ohms
|
|
||||||
Pump 1: {out3} Ohms"""
|
|
||||||
|
|
||||||
return fmt.format(
|
|
||||||
out1=self.out1,
|
|
||||||
out2=self.out2,
|
|
||||||
out3=self.out3)
|
|
||||||
|
|
||||||
__str__ = __repr__
|
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
|
||||||
global _is_setup
|
|
||||||
global _pump
|
|
||||||
|
|
||||||
if _is_setup:
|
|
||||||
return
|
|
||||||
_is_setup = True
|
|
||||||
|
|
||||||
GPIO.setwarnings(False)
|
|
||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
GPIO.setup(PUMP_1_PIN, GPIO.OUT)
|
GPIO.setwarnings(False)
|
||||||
GPIO.input(PUMP_1_PIN, 0)
|
GPIO.setup(self._gpio_pin, GPIO.OUT, initial=GPIO.LOW)
|
||||||
GPIO.setup(PUMP_2_PIN, GPIO.OUT)
|
self._pwm = GPIO.PWM(self._gpio_pin, PUMP_PWM_FREQ)
|
||||||
GPIO.input(PUMP_2_PIN, 0)
|
self._pwm.start(0)
|
||||||
GPIO.setup(PUMP_3_PIN, GPIO.OUT)
|
|
||||||
GPIO.input(PUMP_3_PIN, 0)
|
|
||||||
|
|
||||||
atexit.register(cleanup)
|
self._timeout = None
|
||||||
|
|
||||||
|
def set_speed(self, speed):
|
||||||
|
"""Set pump speed (PWM duty cycle)."""
|
||||||
|
if speed > 1.0 or speed < 0:
|
||||||
|
raise ValueError("Speed must be between 0 and 1")
|
||||||
|
self._pwm.ChangeDutyCycle(int(PUMP_MAX_DUTY * speed))
|
||||||
|
self._speed = speed
|
||||||
|
|
||||||
def set_pump_on(channel):
|
def get_speed(self):
|
||||||
"""Set wet point for a moisture channel."""
|
"""Return Pump speed (PWM duty cycle)."""
|
||||||
_pump[channel] = 1
|
return self._speed
|
||||||
|
|
||||||
def set_pump_off(channel, value):
|
def stop(self):
|
||||||
"""Set wet point for a moisture channel."""
|
"""Stop the pump."""
|
||||||
_pump[channel] = 0
|
self.set_speed(0)
|
||||||
|
|
||||||
def read_pump(channel):
|
def pulse(self, speed, timeout=0.1, blocking=True, force=False):
|
||||||
"""Get current value for the additional ADC pin."""
|
"""Pulse the pump for timeout seconds.
|
||||||
setup()
|
|
||||||
return _pump[channel]
|
|
||||||
|
|
||||||
def cleanup():
|
:param timeout: Timeout, in seconds, of the pump pulse
|
||||||
GPIO.output(PUMP_1_PIN, 0)
|
:param blocking: If true, function will block until pump has stopped
|
||||||
GPIO.output(PUMP_2_PIN, 0)
|
|
||||||
GPIO.output(PUMP_3_PIN, 0)
|
|
||||||
|
|
||||||
def read_all():
|
"""
|
||||||
"""Return pump state"""
|
if blocking:
|
||||||
setup()
|
self.set_speed(speed)
|
||||||
in1 = _pump[1]
|
time.sleep(timeout)
|
||||||
in2 = _pump[2]
|
self.stop()
|
||||||
in3 = _pump[3]
|
return True
|
||||||
|
else:
|
||||||
|
if self._timeout is not None:
|
||||||
|
if self._timeout.is_alive():
|
||||||
|
if force:
|
||||||
|
self._timeout.cancel()
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
self._timeout = threading.Timer(timeout, self.stop)
|
||||||
|
self.set_speed(speed)
|
||||||
|
self._timeout.start()
|
||||||
|
return True
|
||||||
|
|
||||||
return _pump(in1, in2, in3)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user