-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
298 lines (244 loc) · 8.47 KB
/
Copy pathmain.c
File metadata and controls
298 lines (244 loc) · 8.47 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "picoRTOS.h"
#include "picoRTOS_device.h"
#include "board.h"
/*
* Main shared objects
*/
#define STATUS_ALARM (1 << 3)
#define STATUS_HEATER (1 << 2)
#define STATUS_FAN (1 << 1)
#define STATUS_COMPRESSOR (1 << 0)
static struct {
/* temp */
int t_fridge;
int t_freezer;
int t_heater;
/* i/o */
int knob;
bool door_opened;
/* status */
bool defrost;
unsigned status;
unsigned alarm_counter;
} fridge;
static void led_main(void *priv)
{
picoRTOS_assert_fatal(priv != NULL, return );
struct gpio *L = (struct gpio*)priv;
picoRTOS_tick_t ref = picoRTOS_get_tick();
for (;;) {
gpio_write(L, false);
picoRTOS_sleep(PICORTOS_DELAY_MSEC(40ul));
gpio_write(L, true);
picoRTOS_sleep(PICORTOS_DELAY_MSEC(40ul));
gpio_write(L, false);
picoRTOS_sleep(PICORTOS_DELAY_MSEC(120ul));
gpio_write(L, true);
/* until next round */
picoRTOS_sleep_until(&ref, PICORTOS_DELAY_SEC(1));
}
}
static void analog_main(void *priv)
{
#define T_FRIDGE_MIN 0
#define T_FREEZER_MIN -24
#define T_HEATER_MAX 2
#define T_AVG_WEIGHT 3
#define ALARM_THRES 30u
picoRTOS_assert_fatal(priv != NULL, return );
struct board *board = (struct board*)priv;
picoRTOS_tick_t ref = picoRTOS_get_tick();
int t_fridge = T_FRIDGE_MIN;
int t_freezer = T_FREEZER_MIN;
int t_heater = T_FRIDGE_MIN;
for (;;) {
int k, d;
int t0, t1, t2;
while (adc_read(board->T_FRIDGE, &t0) == -EAGAIN) picoRTOS_postpone();
while (adc_read(board->T_FREEZER, &t1) == -EAGAIN) picoRTOS_postpone();
while (adc_read(board->T_HEATER, &t2) == -EAGAIN) picoRTOS_postpone();
while (adc_read(board->KNOB, &k) == -EAGAIN) picoRTOS_postpone();
while (adc_read(board->DOOR, &d) == -EAGAIN) picoRTOS_postpone();
/* average as a filter */
t_fridge = ((t_fridge * T_AVG_WEIGHT) + t0) / (T_AVG_WEIGHT + 1);
t_freezer = ((t_freezer * T_AVG_WEIGHT) + t1) / (T_AVG_WEIGHT + 1);
t_heater = ((t_heater * T_AVG_WEIGHT) + t2) / (T_AVG_WEIGHT + 1);
/* Main state machine */
fridge.status = 0;
if (!fridge.defrost) {
/* Freezer conditions */
if (t_freezer > (T_FREEZER_MIN + k) &&
t_heater > T_FREEZER_MIN)
fridge.status |= STATUS_COMPRESSOR;
/* Fridge conditions (only fans) */
if (t_fridge > (T_FRIDGE_MIN + k))
fridge.status |= STATUS_FAN;
/* Heater conditions */
if (fridge.status == 0 && t_heater > t_freezer)
/* we have inertia on our side */
fridge.defrost = true;
}else{
/* defrosting */
fridge.status = STATUS_HEATER;
/* reached T_HEATER_MAX, end cycle */
fridge.defrost = (t_heater < T_HEATER_MAX);
}
/* Alarm conditions */
if (fridge.alarm_counter > ALARM_THRES)
fridge.status |= STATUS_ALARM;
/* save values */
fridge.t_fridge = t_fridge;
fridge.t_freezer = t_freezer;
fridge.t_heater = t_heater;
fridge.knob = k;
fridge.door_opened = (d != 0);
/* turn light on */
gpio_write(board->LIGHT, fridge.door_opened);
if (fridge.door_opened) {
fridge.status &= ~STATUS_FAN; /* stop the fan */
fridge.alarm_counter++; /* count */
}else
fridge.alarm_counter = 0;
/* until next round */
picoRTOS_sleep_until(&ref, PICORTOS_DELAY_SEC(1));
}
}
static void cold_main(void *priv)
{
#define COMPRESSOR_N 6 /* cycles */
picoRTOS_assert_fatal(priv != NULL, return );
int compressor_n = 0;
struct board *board = (struct board*)priv;
/* default value */
gpio_write(board->FAN, false);
gpio_write(board->R_COMPRESSOR, false);
for (;;) {
gpio_write(board->FAN, (fridge.status & STATUS_FAN) != 0);
gpio_write(board->R_HEATER, (fridge.status & STATUS_HEATER) != 0);
if ((fridge.status & STATUS_COMPRESSOR) != 0)
compressor_n = COMPRESSOR_N;
gpio_write(board->R_COMPRESSOR, compressor_n != 0);
compressor_n -= (compressor_n != 0) ? 1 : 0;
/* 5s cycles */
picoRTOS_sleep(PICORTOS_DELAY_SEC(5));
}
}
static void alarm_main(void *priv)
{
picoRTOS_assert_fatal(priv != NULL, return );
struct pwm *BUZZER = (struct pwm*)priv;
/* default value */
(void)pwm_set_period(BUZZER, (pwm_period_us_t)200); /* 5khz */
(void)pwm_set_duty_cycle(BUZZER, PWM_DUTY_CYCLE_PCENT(50));
for (;;) {
if ((fridge.status & STATUS_ALARM) != 0) {
pwm_start(BUZZER);
picoRTOS_sleep(PICORTOS_DELAY_MSEC(100ul));
pwm_stop(BUZZER);
picoRTOS_sleep(PICORTOS_DELAY_MSEC(100ul));
pwm_start(BUZZER);
picoRTOS_sleep(PICORTOS_DELAY_MSEC(250ul));
pwm_stop(BUZZER);
}
/* sleep_until is overkill */
picoRTOS_sleep(PICORTOS_DELAY_SEC(1));
}
}
/*
* Debug
*/
static void print(struct uart *UART, const char *s, size_t len)
{
size_t sent = 0;
while (sent != len) {
int n = uart_write(UART, &s[sent], len - sent);
if (n < 0) picoRTOS_postpone();
else sent += n;
}
}
static void print_celsius(struct uart *UART, int t)
{
static char str[] = { "xxx, °C" };
static const char number[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
if (t < 0) {
str[0] = '-';
str[1] = number[-t / 10];
str[2] = number[-t % 10];
}else{
str[0] = '+';
str[1] = number[t / 10];
str[2] = number[t % 10];
}
print(UART, str, sizeof(str) - 1);
}
static void print_digit(struct uart *UART, int d)
{
char c = '0' + (char)d;
print(UART, &c, sizeof(c));
}
static void print_bool(struct uart *UART, bool value)
{
static char str[] = { "[x]" };
str[1] = (value ? 'x' : ' ');
print(UART, str, sizeof(str));
}
static void debug_main(void *priv)
{
picoRTOS_assert_fatal(priv != NULL, return );
struct uart *TXD = (struct uart*)priv;
picoRTOS_tick_t ref = picoRTOS_get_tick();
for (;;) {
print(TXD, "t0, ", (size_t)4);
print_celsius(TXD, fridge.t_fridge);
print(TXD, ", t1, ", (size_t)6);
print_celsius(TXD, fridge.t_freezer);
print(TXD, ", t2, ", (size_t)6);
print_celsius(TXD, fridge.t_heater);
print(TXD, ", k, ", (size_t)5);
print_digit(TXD, fridge.knob);
print(TXD, ", d, ", (size_t)5);
print_bool(TXD, fridge.door_opened);
print(TXD, ", c ", (size_t)4);
print_bool(TXD, (fridge.status & STATUS_COMPRESSOR) != 0);
print(TXD, ", f ", (size_t)4);
print_bool(TXD, (fridge.status & STATUS_FAN) != 0);
print(TXD, ", h ", (size_t)4);
print_bool(TXD, (fridge.status & STATUS_HEATER) != 0);
print(TXD, "\n", (size_t)1);
/* until next round */
picoRTOS_sleep_until(&ref, PICORTOS_DELAY_SEC(1));
}
}
int main(void)
{
static struct board board;
struct picoRTOS_task task;
static picoRTOS_stack_t stack0[CONFIG_DEFAULT_STACK_COUNT];
static picoRTOS_stack_t stack1[CONFIG_DEFAULT_STACK_COUNT];
static picoRTOS_stack_t stack2[CONFIG_DEFAULT_STACK_COUNT];
static picoRTOS_stack_t stack3[CONFIG_DEFAULT_STACK_COUNT];
static picoRTOS_stack_t stack4[CONFIG_DEFAULT_STACK_COUNT];
picoRTOS_init();
(void)board_init(&board);
/* led */
picoRTOS_task_init(&task, led_main, board.L, stack0, PICORTOS_STACK_COUNT(stack0));
picoRTOS_add_task(&task, picoRTOS_get_next_available_priority());
/* analog */
picoRTOS_task_init(&task, analog_main, &board, stack1, PICORTOS_STACK_COUNT(stack1));
picoRTOS_add_task(&task, picoRTOS_get_next_available_priority());
/* cold */
picoRTOS_task_init(&task, cold_main, &board, stack2, PICORTOS_STACK_COUNT(stack2));
picoRTOS_add_task(&task, picoRTOS_get_next_available_priority());
/* alarm */
picoRTOS_task_init(&task, alarm_main, board.BUZZER, stack3, PICORTOS_STACK_COUNT(stack3));
picoRTOS_add_task(&task, picoRTOS_get_next_available_priority());
/* debug */
picoRTOS_task_init(&task, debug_main, board.TXD, stack4, PICORTOS_STACK_COUNT(stack4));
picoRTOS_add_task(&task, picoRTOS_get_next_available_priority());
picoRTOS_start();
/* not supposed to end there */
picoRTOS_assert_void(false);
return 1;
}