~travisshears/sewa-reef-controller

ref: 170bfa1d30d1c7c50170542fc863dd8f9a74351d sewa-reef-controller/CIRCUITPY/aquarium_controller/power.py -rwxr-xr-x 5.2 KiB
170bfa1d — Travis Shears add picture and blog post to reademe 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import board
import digitalio

_default_power_state = {
    # 'test_pin': False
    'refigium_light': False,
    'main_pump': True, # carbon_reactor will mirror main_pump via powerstrip
    'power_head_1': True,
    'power_head_2': True,
}

# prios
base_prio = 0
normal_sim_prio = 1
id_outlets_prio = 10
feed_mode_prio =  5
hard_reset_prio = 90
master_switch_prio = 100

def _set_pin(b):
    return b

def _get_pin(b):
    return b


def power(core):
    pins = {
        'refigium_light': board.GP27,
        'main_pump': board.GP26,
        'power_head_1': board.GP28,
        'power_head_2': board.GP22,
    }

    # init prios
    prio_holds = {}
    for key in _default_power_state:
        prio_holds[key] = base_prio

    left_over_pins = [
        board.GP18,
        board.GP19,
        board.GP20,
        board.GP21,
    ]

    # turn on all left over outlets
    for pin in left_over_pins:
        d = digitalio.DigitalInOut(pin)
        d.direction = digitalio.Direction.OUTPUT
        d.value = _set_pin(True)

    # transform pins into digitalio pins
    for key in pins:
        pin = pins[key]
        pins[key] = digitalio.DigitalInOut(pin)
        pins[key].direction = digitalio.Direction.OUTPUT
        pins[key].value = _set_pin(_default_power_state[key])

    # power status util screen
    def on_change(mode):
        code = mode['code']
        msg = ""
        status = {}
        for key in pins:
            status[key] = 'on' if _get_pin(pins[key].value) else 'off'
        if(code is 'id'):
            msg = "id pumps?"
        elif(code is 'pwr'):
            msg = f"refigium_light: {status['refigium_light']}\nmain_pump: {status['main_pump']}"
        elif(code is 'pwr1'):
            msg = f"power_head_1: {status['power_head_1']}\npower_head_2: {status['power_head_2']}"

        if msg:
            core.publish('render', {
                'text': msg
            })

    def on_reset_all(msg):
        prio = msg['prio']
        for key in pins:
            current_prio = prio_holds[key]
            if prio < current_prio:
                continue
            pins[key].value = _set_pin(_default_power_state[key])
            prio_holds[key] = 0

    def on_reset(msg):
        key = msg['code']
        prio = msg['prio']
        current_prio = prio_holds[key]
        if prio >= current_prio:
            prio_holds[key] = prio
            pins[key].value = _set_pin(_default_power_state[key])

    def on_release(msg):
        key = msg['code']
        prio = msg['prio']
        current_prio = prio_holds[key]
        if prio >= current_prio:
            prio_holds[key] = base_prio

    def on_pwr(msg):
        key = msg['code']
        current_prio = prio_holds[key]
        prio = msg['prio']
        if prio >= current_prio:
            prio_holds[key] = prio
            pins[key].value = _set_pin(msg['value'])

    core.subscribe('pwr', on_pwr)
    core.subscribe('release', on_release)
    core.subscribe('reset', on_reset)
    core.subscribe('reset_all', on_reset_all)
    core.subscribe('mode change', on_change)

def id_outlets(core):
    pin_keys = []
    for key in _default_power_state:
        pin_keys.append(key)
    i = 0

    def on_mode(mode):
        if(mode['code'] is not 'id'):
            return
        core.publish('render', {
            'text': 'id power pins?'
        })

    def on_click(mode):
        nonlocal i
        if(mode['code'] is not 'id'):
            return

        if i == len(pin_keys):
            for key in pin_keys:
                core.publish('reset', {
                    'code': key,
                    'prio': id_outlets_prio
                });
                core.publish('release', {
                    'code': key,
                    'prio': id_outlets_prio
                })
                core.publish('render', {
                    'text': "resetting\nthanks for\ntesting"
                })
            i = 0
            return

        # turn all pumps off
        for key in pin_keys:
            core.publish('pwr', {
                'code': key,
                'value': False,
                'prio': id_outlets_prio
            })

        # turn on current test on
        core.publish('pwr', {
            'code': pin_keys[i],
            'value': True,
            'prio': id_outlets_prio
        })

        core.publish('render', {
            'text': f"testing outlet:\n{pin_keys[i]}"
        })

        i += 1

    core.subscribe('mode change', on_mode)
    core.subscribe('mode click', on_click)

def master_power_switch(core):
    def on_switched(msg):
        pos = msg['pos']
        if(pos is 'mid'):
            for key in _default_power_state:
                core.publish('reset', {
                    'code': key,
                    'prio': master_switch_prio
                });
                core.publish('release', {
                    'code': key,
                    'prio': master_switch_prio
                })
        elif(pos is 'up' or pos is 'down'):
            for key in _default_power_state:
                core.publish('pwr', {
                    'code': key,
                    'value': True if pos is 'up' else False,
                    'prio': master_switch_prio
                });

    core.subscribe('master_power switched', on_switched)