Re-index channels from 1

This commit is contained in:
Phil Howard
2020-05-21 11:43:40 +01:00
parent c054a0a5b4
commit 605d6fdc47
3 changed files with 47 additions and 12 deletions

View File

@@ -6,6 +6,9 @@ from PIL import Image, ImageDraw, ImageFont
from fonts.ttf import RobotoMedium as UserFont from fonts.ttf import RobotoMedium as UserFont
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from grow.moisture import Moisture
from grow.pump import Pump
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) GPIO.setwarnings(False)
@@ -15,6 +18,18 @@ LABELS = ['A', 'B', 'X', 'Y']
channel_count = 3 channel_count = 3
channel_selected = 0 channel_selected = 0
sensors = [
Moisture(channel=1),
Moisture(channel=2),
Moisture(channel=3)
]
pumps = [
Pump(channel=1),
Pump(channel=2),
Pump(channel=3)
]
GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
font = ImageFont.truetype(UserFont, 14) font = ImageFont.truetype(UserFont, 14)
@@ -37,7 +52,7 @@ display = ST7735.ST7735(
dc=9, dc=9,
backlight=12, backlight=12,
rotation=270, rotation=270,
spi_speed_hz=10000000 spi_speed_hz=80000000
) )
ramp = [ ramp = [
@@ -58,6 +73,11 @@ ramp_sat = [
def indicator_color(value, r=None): def indicator_color(value, r=None):
if r is None: if r is None:
r = ramp r = ramp
if value == 1.0:
return r[-1]
if value == 0.0:
return r[0]
value *= len(r) - 1 value *= len(r) - 1
a = int(math.floor(value)) a = int(math.floor(value))
b = a + 1 b = a + 1
@@ -116,9 +136,13 @@ picked = random.sample(plants, 3)
while True: while True:
t = time.time() / 10.0 t = time.time() / 10.0
c1 = (math.sin(math.pi + t * math.pi) + 1.0) / 2.0 #c1 = (math.sin(math.pi + t * math.pi) + 1.0) / 2.0
c2 = (math.sin(t * math.pi) + 1.0) / 2.0 #c2 = (math.sin(t * math.pi) + 1.0) / 2.0
c3 = (math.sin(math.pi + t * math.pi) + 1.0) / 2.0 #c3 = (math.sin(math.pi + t * math.pi) + 1.0) / 2.0
c1 = 1.0 - sensors[0].saturation
c2 = 1.0 - sensors[1].saturation
c3 = 1.0 - sensors[2].saturation
print(c1, c2, c3)
draw.rectangle((21, 0, 138, HEIGHT), (255, 255, 255)) # Erase channel area draw.rectangle((21, 0, 138, HEIGHT), (255, 255, 255)) # Erase channel area
@@ -156,4 +180,4 @@ while True:
icon(image, icon_snooze, (0, HEIGHT - 20), (r, 129, 129)) icon(image, icon_snooze, (0, HEIGHT - 20), (r, 129, 129))
display.display(image.convert("RGB")) display.display(image.convert("RGB"))
time.sleep(1.0 / 30) time.sleep(1.0 / 5)

View File

@@ -10,19 +10,19 @@ MOISTURE_INT_PIN = 4
class Moisture(object): class Moisture(object):
"""Grow moisture sensor driver.""" """Grow moisture sensor driver."""
def __init__(self, channel=0, wet_point=None, dry_point=None): def __init__(self, channel=1, wet_point=None, dry_point=None):
"""Create a new moisture sensor. """Create a new moisture sensor.
Uses an interrupt to count pulses on the GPIO pin corresponding to the selected channel. Uses an interrupt to count pulses on the GPIO pin corresponding to the selected channel.
The moisture reading is given as pulses per second. The moisture reading is given as pulses per second.
:param channel: One of 0, 1 or 2. 3 can optionally be used to set up a sensor on the Int pin (BCM4) :param channel: One of 1, 2 or 3. 4 can optionally be used to set up a sensor on the Int pin (BCM4)
:param wet_point: Wet point in pulses/sec :param wet_point: Wet point in pulses/sec
:param dry_point: Dry point in pulses/sec :param dry_point: Dry point in pulses/sec
""" """
self._gpio_pin = [MOISTURE_1_PIN, MOISTURE_2_PIN, MOISTURE_3_PIN, MOISTURE_INT_PIN][channel] self._gpio_pin = [MOISTURE_1_PIN, MOISTURE_2_PIN, MOISTURE_3_PIN, MOISTURE_INT_PIN][channel - 1]
GPIO.setwarnings(False) GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
@@ -30,6 +30,7 @@ class Moisture(object):
self._count = 0 self._count = 0
self._reading = 0 self._reading = 0
self._new_data = False
self._wet_point = wet_point if wet_point is not None else 100 self._wet_point = wet_point if wet_point is not None else 100
self._dry_point = dry_point if dry_point is not None else 900 self._dry_point = dry_point if dry_point is not None else 900
self._time_start = time.time() self._time_start = time.time()

View File

@@ -7,12 +7,22 @@ PUMP_1_PIN = 17
PUMP_2_PIN = 27 PUMP_2_PIN = 27
PUMP_3_PIN = 22 PUMP_3_PIN = 22
PUMP_PWM_FREQ = 10000 PUMP_PWM_FREQ = 10000
PUMP_MAX_DUTY = 60 PUMP_MAX_DUTY = 90
class Pump(object): class Pump(object):
def __init__(self, channel): """Grow pump driver."""
self._gpio_pin = [PUMP_1_PIN, PUMP_2_PIN, PUMP_3_PIN][channel]
def __init__(self, channel=1):
"""Create a new pump.
Uses soft PWM to drive a Grow pump.
:param channel: One of 1, 2 or 3.
"""
self._gpio_pin = [PUMP_1_PIN, PUMP_2_PIN, PUMP_3_PIN][channel - 1]
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) GPIO.setwarnings(False)
@@ -37,7 +47,7 @@ class Pump(object):
"""Stop the pump.""" """Stop the pump."""
self.set_speed(0) self.set_speed(0)
def pulse(self, speed, timeout=0.1, blocking=True, force=False): def dose(self, speed, timeout=0.1, blocking=True, force=False):
"""Pulse the pump for timeout seconds. """Pulse the pump for timeout seconds.
:param timeout: Timeout, in seconds, of the pump pulse :param timeout: Timeout, in seconds, of the pump pulse