~travisshears/sewa-reef-controller

c3ca70f05d425834a1d9c922995680557014c52f — Travis Shears 2 months ago 2f911ed
add cabinet_light
M .gitignore => .gitignore +1 -0
@@ 1,1 1,2 @@
log.txt
.DS_Store

A CIRCUITPY/aquarium_controller/lights.py => CIRCUITPY/aquarium_controller/lights.py +63 -0
@@ 0,0 1,63 @@
import digitalio

from adafruit_debouncer import Debouncer

from .power import normal_sim_prio

class Lights:
    def __init__(self, core, cabinet_door_pin):
        self.core = core
        self.core.subscribe('rtc time', self._on_rtc_msg)

        cabinet_door_pin = digitalio.DigitalInOut(cabinet_door_pin)
        cabinet_door_pin.direction = digitalio.Direction.INPUT
        cabinet_door_pin.pull = digitalio.Pull.DOWN
        self.cabinet_door = Debouncer(cabinet_door_pin, interval=0.1)
        door_closed = self.cabinet_door.value
        if (door_closed):
            # turn light on
            self.core.publish('pwr', {
                'code': 'cabinet_light',
                'prio': normal_sim_prio,
                'value': False,
            })
        else: 
            # turn light on
            self.core.publish('pwr', {
                'code': 'cabinet_light',
                'prio': normal_sim_prio,
                'value': True,
            })

    def _on_rtc_msg(self, rtc_msg):
        dt = rtc_msg['dt']
        current_sec = (dt.tm_hour * 3600) + (dt.tm_min * 60) + dt.tm_sec
        self._handle_refigium_light(current_sec)

    def _handle_refigium_light(self, current_sec):
        pwr_status = 32400 <= current_sec <= 75600 # 10 - 21
        self.core.publish('pwr', {
            'code': 'refigium_light',
            'prio': normal_sim_prio,
            'value': not pwr_status,
        })

    def update(self):
        self._cabinet_door_update()

    def _cabinet_door_update(self):
        self.cabinet_door.update()
        if self.cabinet_door.fell:
            # door opened
            self.core.publish('pwr', {
                'code': 'cabinet_light',
                'prio': normal_sim_prio,
                'value': True,
            })
        if self.cabinet_door.rose:
            # door closed
            self.core.publish('pwr', {
                'code': 'cabinet_light',
                'prio': normal_sim_prio,
                'value': False,
            })
\ No newline at end of file

M CIRCUITPY/aquarium_controller/power.py => CIRCUITPY/aquarium_controller/power.py +5 -3
@@ 19,6 19,7 @@ power_heads = [
_default_power_state = {
    # 'test_pin': False
    'refigium_light': False,
    'cabinet_light': False,
    'main_pump': True, # carbon_reactor will mirror main_pump via powerstrip
    'power_head_1': True,
    'power_head_2': True,


@@ 44,11 45,12 @@ def _get_pin(b):
def power(core):
    pins = {
        'refigium_light': board.GP27,
        'main_pump': board.GP26,
        'power_head_1': board.GP28,
        'power_head_2': board.GP22,
        'power_head_3': board.GP21,
        'power_head_4': board.GP20
        'power_head_4': board.GP20,
        'main_pump': board.GP19,
        'cabinet_light': board.GP26
    }

    # init prios


@@ 58,7 60,7 @@ def power(core):

    left_over_pins = [
        board.GP18,
        board.GP19
        # board.GP19
    ]

    # turn on all left over outlets

M CIRCUITPY/aquarium_controller/rtc.py => CIRCUITPY/aquarium_controller/rtc.py +1 -1
@@ 12,6 12,6 @@ class RTC:
        self.core.publish("rtc time", {"dt": dt})
    def set_time(self):
        return; # comment out to set time
        dt = time.struct_time((2022, 1, 31, 16, 26, 0, 0, -1, -1))
        dt = time.struct_time((2022, 7, 27, 23, 9, 0, 0, -1, -1))
        print(f"Setting time to: {dt}")
        self.rtc.datetime = dt

M CIRCUITPY/code.py => CIRCUITPY/code.py +4 -14
@@ 13,8 13,9 @@ from aquarium_controller.inputs import Button, ThreeWaySwitch
from aquarium_controller.feed_mode import FeedMode
from aquarium_controller.power import power, id_outlets, master_power_switch, normal_sim_prio, hard_reset_prio
from aquarium_controller.flow import flow_sim
from aquarium_controller.lights import Lights

ver = '1.2.0'
ver = '1.3.0'

def util_mode(core):
    def change(mode):


@@ 59,18 60,6 @@ def reset(core):
    core.subscribe('mode click', on_click)


def refigium_light(core):
    def on_tick(rtc_msg):
        dt = rtc_msg['dt']
        current_sec = (dt.tm_hour * 3600) + (dt.tm_min * 60) + dt.tm_sec
        day = 32400 <= current_sec <= 75600 # 9 - 21
        core.publish('pwr', {
            'code': 'refigium_light',
            'prio': 1,
            'value': not day,
        })
    core.subscribe('rtc time', on_tick)

core = Core()
modes = Modes(core)
welcome_mode(core)


@@ 85,7 74,7 @@ id_outlets(core)
master_power_switch(core)
feed_mode = FeedMode(core)
reset(core)
refigium_light(core)
lights = Lights(core, cabinet_door_pin=board.GP5)
flow_sim(core)




@@ 109,3 98,4 @@ while True:

    for i in inputs:
        i.update()
    lights.update()

M rsync-exclude.txt => rsync-exclude.txt +1 -0
@@ 1,3 1,4 @@
.Trashes
.fseventsd
.metadata_never_index
._code.py
\ No newline at end of file