-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
170 lines (150 loc) · 4.83 KB
/
main.c
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
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/time.h"
#include "common.h"
static enum state {
STATE_IDLE,
STATE_GLOW,
STATE_START,
STATE_STOP,
} state;
static enum substate {
SUBSTATE_START_ENABLED_BRIGHT,
SUBSTATE_START_ENABLED_DIM,
SUBSTATE_START_DISABLED,
} substate = SUBSTATE_START_ENABLED_BRIGHT;
/*
* Control all four relays in a single call.
*
* Expected state vs relay configuration.
*
* PANEL GLOW START STOP
* STATE_IDLE : !1 0 0 0
* STATE_GLOW : !0 1 0 0
* STATE_START : !0 1 1 0
* STATE_STOP : !1 0 0 1
*/
static void relays_set(bool panel, bool glow, bool start, bool stop) {
relay_set(GPIO_RELAY_PANEL, panel);
relay_set(GPIO_RELAY_GLOW, glow);
relay_set(GPIO_RELAY_START, start);
relay_set(GPIO_RELAY_STOP, stop);
}
static void goto_state_idle() {
printf("goto_state_idle\n");
switch (substate) {
case SUBSTATE_START_ENABLED_BRIGHT:
led_jump_to(GPIO_LED_ON_OFF, LED_BRIGHT);
led_jump_to(GPIO_LED_GLOW, LED_BRIGHT);
led_jump_to(GPIO_LED_START, LED_BRIGHT);
led_jump_to(GPIO_LED_STOP, LED_BRIGHT);
break;
case SUBSTATE_START_ENABLED_DIM:
if (state == STATE_IDLE) {
led_animate_to(GPIO_LED_ON_OFF, LED_DIM);
led_animate_to(GPIO_LED_GLOW, LED_DIM);
led_animate_to(GPIO_LED_START, LED_DIM);
led_animate_to(GPIO_LED_STOP, LED_DIM);
} else {
led_jump_to(GPIO_LED_ON_OFF, LED_DIM);
led_jump_to(GPIO_LED_GLOW, LED_DIM);
led_jump_to(GPIO_LED_START, LED_DIM);
led_jump_to(GPIO_LED_STOP, LED_DIM);
}
break;
case SUBSTATE_START_DISABLED:
if (state == STATE_IDLE) {
led_animate_to(GPIO_LED_ON_OFF, LED_DIM);
led_animate_to(GPIO_LED_GLOW, LED_OFF);
led_animate_to(GPIO_LED_START, LED_OFF);
led_animate_to(GPIO_LED_STOP, LED_DIM);
} else {
led_jump_to(GPIO_LED_ON_OFF, LED_DIM);
led_jump_to(GPIO_LED_GLOW, LED_OFF);
led_jump_to(GPIO_LED_START, LED_OFF);
led_jump_to(GPIO_LED_STOP, LED_DIM);
}
break;
}
relays_set(!true, false, false, false);
state = STATE_IDLE;
}
static void goto_state_start() {
printf("goto_state_start\n");
led_jump_to(GPIO_LED_ON_OFF, LED_OFF);
led_jump_to(GPIO_LED_GLOW, LED_OFF);
led_jump_to(GPIO_LED_START, LED_BRIGHT);
led_jump_to(GPIO_LED_STOP, LED_OFF);
relays_set(!false, true, true, false);
state = STATE_START;
}
static void goto_state_glow() {
printf("goto_state_glow\n");
led_jump_to(GPIO_LED_ON_OFF, LED_OFF);
led_jump_to(GPIO_LED_GLOW, LED_BRIGHT);
led_jump_to(GPIO_LED_START, LED_OFF);
led_jump_to(GPIO_LED_STOP, LED_OFF);
relays_set(!false, true, false, false);
state = STATE_GLOW;
}
static void goto_state_stop() {
printf("goto_state_stop\n");
led_jump_to(GPIO_LED_ON_OFF, substate == SUBSTATE_START_ENABLED_BRIGHT ? LED_BRIGHT : LED_DIM);
led_jump_to(GPIO_LED_GLOW, LED_OFF);
led_jump_to(GPIO_LED_START, LED_OFF);
led_jump_to(GPIO_LED_STOP, LED_BRIGHT);
relays_set(!true, false, false, true);
state = STATE_STOP;
}
static void on_button_start(enum button_event event) {
if (event == BUTTON_PRESSED && state == STATE_IDLE && substate != SUBSTATE_START_DISABLED) {
goto_state_start();
} else if (event == BUTTON_RELEASED && state == STATE_START) {
goto_state_idle();
}
}
static void on_button_glow(enum button_event event) {
if (event == BUTTON_PRESSED && state == STATE_IDLE && substate != SUBSTATE_START_DISABLED) {
goto_state_glow();
} else if (event == BUTTON_RELEASED && state == STATE_GLOW) {
goto_state_idle();
}
}
static void on_button_stop(enum button_event event) {
if (event == BUTTON_PRESSED && state == STATE_IDLE) {
goto_state_stop();
} else if (event == BUTTON_RELEASED && state == STATE_STOP) {
goto_state_idle();
}
}
static int64_t on_alarm(__unused alarm_id_t id, void* user_data) {
printf("on_alarm user_data=%d\n", (int)user_data);
substate = (int)user_data;
if (state == STATE_IDLE) {
goto_state_idle(); // update LEDs
}
return 0;
}
int main() {
stdio_init_all();
button_register(GPIO_SWITCH_GLOW, on_button_glow);
button_register(GPIO_SWITCH_START, on_button_start);
button_register(GPIO_SWITCH_STOP, on_button_stop);
led_register(GPIO_LED_ON_OFF);
led_register(GPIO_LED_GLOW);
led_register(GPIO_LED_START);
led_register(GPIO_LED_STOP);
relay_register(GPIO_RELAY_PANEL);
relay_register(GPIO_RELAY_GLOW);
relay_register(GPIO_RELAY_START);
relay_register(GPIO_RELAY_STOP);
add_alarm_in_ms(DURATION_SUBSTATE_START_ENABLED_BRIGHT_MS, on_alarm,
(void*)SUBSTATE_START_ENABLED_DIM, true);
add_alarm_in_ms(DURATION_SUBSTATE_START_ENABLED_BRIGHT_MS +
DURATION_SUBSTATE_START_ENABLED_DIM_MS,
on_alarm, (void*)SUBSTATE_START_DISABLED, true);
goto_state_idle();
while (true) {
tight_loop_contents();
}
}