mirror of
https://github.com/pimoroni/grow-python
synced 2025-10-25 15:19:23 +00:00
Switch moisture to class
This commit is contained in:
@@ -3,91 +3,55 @@ import atexit
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
MOISTURE_1_PIN = 23
|
||||
MOISTURE_2_PIN = 25
|
||||
MOISTURE_3_PIN = 8
|
||||
|
||||
_is_setup = False
|
||||
MOISTURE_2_PIN = 8
|
||||
MOISTURE_3_PIN = 25
|
||||
MOISTURE_INT_PIN = 4
|
||||
|
||||
|
||||
class Moisture(object):
|
||||
__slots__ = 'in1', 'in2', 'in3'
|
||||
def __init__(self, channel=0):
|
||||
self._gpio_pin = [MOISTURE_1_PIN, MOISTURE_2_PIN, MOISTURE_3_PIN, MOISTURE_INT_PIN][channel]
|
||||
|
||||
def __init__(self, in1, in2, in3):
|
||||
self.in1 = in1
|
||||
self.in2 = in2
|
||||
self.in3 = in3
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(self._gpio_pin, GPIO.IN)
|
||||
|
||||
def __repr__(self):
|
||||
fmt = """Moisture 1: {in1} Hz
|
||||
Moisture 1: {in2} Ohms
|
||||
Moisture 1: {in3} Ohms"""
|
||||
self._count = 0
|
||||
self._reading = 0
|
||||
self._wet_point = 0
|
||||
self._dry_point = 0
|
||||
self._time_start = time.time()
|
||||
GPIO.add_event_detect(self._gpio_pin, GPIO.RISING, callback=self._event_handler, bouncetime=1)
|
||||
|
||||
return fmt.format(
|
||||
in1=self.in1,
|
||||
in2=self.in2,
|
||||
in3=self.in3)
|
||||
def set_wet_point(self, value=None):
|
||||
self._wet_point = value if value is not None else self.raw_moisture
|
||||
|
||||
__str__ = __repr__
|
||||
def set_dry_point(self, value=None):
|
||||
self._dry_point = value if value is not None else self.raw_moisture
|
||||
|
||||
def _event_handler(self, pin):
|
||||
self._count += 1
|
||||
if self.time_elapsed >= 1.0:
|
||||
self._reading = self._count / self.time_elapsed
|
||||
self._count = 0
|
||||
self._time_start = time.time()
|
||||
|
||||
def setup():
|
||||
global _is_setup
|
||||
global _moisture
|
||||
@property
|
||||
def time_elapsed(self):
|
||||
return time.time() - self._time_start
|
||||
|
||||
if _is_setup:
|
||||
return
|
||||
_is_setup = True
|
||||
@property
|
||||
def raw_moisture(self):
|
||||
return self._reading
|
||||
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(MOISTURE_1_PIN, GPIO.IN)
|
||||
GPIO.input(MOISTURE_1_PIN)
|
||||
GPIO.setup(MOISTURE_2_PIN, GPIO.IN)
|
||||
GPIO.input(MOISTURE_2_PIN)
|
||||
GPIO.setup(MOISTURE_3_PIN, GPIO.IN)
|
||||
GPIO.input(MOISTURE_3_PIN)
|
||||
@property
|
||||
def moisture(self):
|
||||
return self.raw_moisture
|
||||
|
||||
atexit.register(cleanup)
|
||||
@property
|
||||
def range(self):
|
||||
return self._wet_point - self._dry_point
|
||||
|
||||
|
||||
def set_moisture_wet(channel, value):
|
||||
"""Set wet point for a moisture channel."""
|
||||
_moisture[channel].wet = value
|
||||
|
||||
def set_moisture_wet(channel, value):
|
||||
"""Set wet point for a moisture channel."""
|
||||
_moisture[channel].dry = value
|
||||
|
||||
def read_moiture(channel):
|
||||
"""Get current value for the additional ADC pin."""
|
||||
setup()
|
||||
return _moisture[channel].value
|
||||
|
||||
def cleanup():
|
||||
GPIO.output(MOISTURE_1_PIN, 0)
|
||||
GPIO.output(MOISTURE_2_PIN, 0)
|
||||
GPIO.output(MOISTURE_3_PIN, 0)
|
||||
|
||||
def read_all():
|
||||
"""Return gas resistence for oxidising, reducing and NH3"""
|
||||
setup()
|
||||
in1 = adc.get_voltage('in0/gnd')
|
||||
in2 = adc.get_voltage('in1/gnd')
|
||||
in3 = adc.get_voltage('in2/gnd')
|
||||
|
||||
try:
|
||||
in1 = (ox * 56000) / (3.3 - ox)
|
||||
except ZeroDivisionError:
|
||||
in1 = 0
|
||||
|
||||
try:
|
||||
in2 = (red * 56000) / (3.3 - red)
|
||||
except ZeroDivisionError:
|
||||
in2 = 0
|
||||
|
||||
try:
|
||||
in3 = (nh3 * 56000) / (3.3 - nh3)
|
||||
except ZeroDivisionError:
|
||||
in3 = 0
|
||||
|
||||
return _moisture(in1, in2, in3)
|
||||
@property
|
||||
def saturation(self):
|
||||
return float(self.moisture - self._dry_point) / self.range
|
||||
|
||||
Reference in New Issue
Block a user