|
@@ -1,4 +1,4 @@
|
1
|
|
-#include <Arduino.h>
|
|
1
|
+#include "Plants.h"
|
2
|
2
|
#include "Statemachine.h"
|
3
|
3
|
|
4
|
4
|
Statemachine::DigitBuffer::DigitBuffer(int _size) {
|
|
@@ -53,11 +53,16 @@ uint32_t Statemachine::DigitBuffer::getNumber(void) {
|
53
|
53
|
Statemachine::Statemachine(print_fn _print, backspace_fn _backspace)
|
54
|
54
|
: db(7) {
|
55
|
55
|
state = init;
|
|
56
|
+ old_state = init;
|
56
|
57
|
print = _print;
|
57
|
58
|
backspace = _backspace;
|
58
|
59
|
|
59
|
60
|
selected_id = 0;
|
60
|
61
|
selected_time = 0;
|
|
62
|
+ start_time = 0;
|
|
63
|
+ stop_time = 0;
|
|
64
|
+ last_animation_time = 0;
|
|
65
|
+ error_condition = "";
|
61
|
66
|
}
|
62
|
67
|
|
63
|
68
|
void Statemachine::begin(void) {
|
|
@@ -78,15 +83,7 @@ void Statemachine::input(int n) {
|
78
|
83
|
switch_to(init);
|
79
|
84
|
}
|
80
|
85
|
} else if (state == menu_auto) {
|
81
|
|
- if ((n == -1) || (n == -2)) {
|
82
|
|
- switch_to(menu);
|
83
|
|
- } else if (n == 1) {
|
84
|
|
- // water only
|
85
|
|
-
|
86
|
|
- } else if (n == 2) {
|
87
|
|
- // with fertilizer
|
88
|
|
-
|
89
|
|
- }
|
|
86
|
+ switch_to(menu);
|
90
|
87
|
} else if (state == menu_auto_mode) {
|
91
|
88
|
switch_to(menu);
|
92
|
89
|
} else if (state == menu_auto_go) {
|
|
@@ -108,8 +105,12 @@ void Statemachine::input(int n) {
|
108
|
105
|
|
109
|
106
|
selected_id = number_input();
|
110
|
107
|
|
111
|
|
- // TODO validate
|
112
|
|
- switch_to(menu_pumps_time);
|
|
108
|
+ if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
|
|
109
|
+ error_condition = "Invalid pump ID!";
|
|
110
|
+ switch_to(error);
|
|
111
|
+ } else {
|
|
112
|
+ switch_to(menu_pumps_time);
|
|
113
|
+ }
|
113
|
114
|
} else {
|
114
|
115
|
if (db.spaceLeft()) {
|
115
|
116
|
db.addDigit(n);
|
|
@@ -132,8 +133,12 @@ void Statemachine::input(int n) {
|
132
|
133
|
|
133
|
134
|
selected_time = number_input();
|
134
|
135
|
|
135
|
|
- // TODO validate
|
136
|
|
- switch_to(menu_pumps_go);
|
|
136
|
+ if ((selected_time <= 0) || (selected_time > 120)) {
|
|
137
|
+ error_condition = "Invalid time range!";
|
|
138
|
+ switch_to(error);
|
|
139
|
+ } else {
|
|
140
|
+ switch_to(menu_pumps_go);
|
|
141
|
+ }
|
137
|
142
|
} else {
|
138
|
143
|
if (db.spaceLeft()) {
|
139
|
144
|
db.addDigit(n);
|
|
@@ -142,9 +147,29 @@ void Statemachine::input(int n) {
|
142
|
147
|
}
|
143
|
148
|
}
|
144
|
149
|
} else if (state == menu_pumps_go) {
|
145
|
|
- switch_to(menu);
|
|
150
|
+ if (n == -2) {
|
|
151
|
+ start_time = millis();
|
|
152
|
+ last_animation_time = start_time;
|
|
153
|
+
|
|
154
|
+ auto wl = plants.getWaterlevel();
|
|
155
|
+ if ((wl != Plants::full) && (wl != Plants::invalid)) {
|
|
156
|
+ plants.startFertilizer(selected_id - 1);
|
|
157
|
+ switch_to(menu_pumps_running);
|
|
158
|
+ } else if (wl == Plants::full) {
|
|
159
|
+ stop_time = millis();
|
|
160
|
+ switch_to(menu_pumps_done);
|
|
161
|
+ } else if (wl == Plants::invalid) {
|
|
162
|
+ error_condition = "Invalid sensor state";
|
|
163
|
+ state = menu_pumps;
|
|
164
|
+ switch_to(error);
|
|
165
|
+ }
|
|
166
|
+ } else {
|
|
167
|
+ switch_to(menu_pumps_time);
|
|
168
|
+ }
|
146
|
169
|
} else if (state == menu_pumps_running) {
|
147
|
|
- switch_to(menu);
|
|
170
|
+ plants.abort();
|
|
171
|
+ stop_time = millis();
|
|
172
|
+ switch_to(menu_pumps_done);
|
148
|
173
|
} else if (state == menu_pumps_done) {
|
149
|
174
|
switch_to(menu);
|
150
|
175
|
} else if (state == menu_valves) {
|
|
@@ -162,8 +187,12 @@ void Statemachine::input(int n) {
|
162
|
187
|
|
163
|
188
|
selected_id = number_input();
|
164
|
189
|
|
165
|
|
- // TODO validate
|
166
|
|
- switch_to(menu_valves_time);
|
|
190
|
+ if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
|
|
191
|
+ error_condition = "Invalid valve ID!";
|
|
192
|
+ switch_to(error);
|
|
193
|
+ } else {
|
|
194
|
+ switch_to(menu_valves_time);
|
|
195
|
+ }
|
167
|
196
|
} else {
|
168
|
197
|
if (db.spaceLeft()) {
|
169
|
198
|
db.addDigit(n);
|
|
@@ -186,8 +215,12 @@ void Statemachine::input(int n) {
|
186
|
215
|
|
187
|
216
|
selected_time = number_input();
|
188
|
217
|
|
189
|
|
- // TODO validate
|
190
|
|
- switch_to(menu_valves_go);
|
|
218
|
+ if ((selected_time <= 0) || (selected_time > 120)) {
|
|
219
|
+ error_condition = "Invalid time range!";
|
|
220
|
+ switch_to(error);
|
|
221
|
+ } else {
|
|
222
|
+ switch_to(menu_valves_go);
|
|
223
|
+ }
|
191
|
224
|
} else {
|
192
|
225
|
if (db.spaceLeft()) {
|
193
|
226
|
db.addDigit(n);
|
|
@@ -196,11 +229,42 @@ void Statemachine::input(int n) {
|
196
|
229
|
}
|
197
|
230
|
}
|
198
|
231
|
} else if (state == menu_valves_go) {
|
199
|
|
- switch_to(menu);
|
|
232
|
+ if (n == -2) {
|
|
233
|
+ start_time = millis();
|
|
234
|
+ last_animation_time = start_time;
|
|
235
|
+
|
|
236
|
+ auto wl = plants.getWaterlevel();
|
|
237
|
+ if ((wl != Plants::full) && (wl != Plants::invalid)) {
|
|
238
|
+ if (selected_id >= (plants.countPlants() + 1)) {
|
|
239
|
+ plants.openWaterInlet();
|
|
240
|
+ } else {
|
|
241
|
+ plants.startPlant(selected_id - 1);
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ switch_to(menu_valves_running);
|
|
245
|
+ } else if (wl == Plants::full) {
|
|
246
|
+ stop_time = millis();
|
|
247
|
+ switch_to(menu_valves_done);
|
|
248
|
+ } else if (wl == Plants::invalid) {
|
|
249
|
+ error_condition = "Invalid sensor state";
|
|
250
|
+ state = menu_valves;
|
|
251
|
+ switch_to(error);
|
|
252
|
+ }
|
|
253
|
+ } else {
|
|
254
|
+ switch_to(menu_valves_time);
|
|
255
|
+ }
|
200
|
256
|
} else if (state == menu_valves_running) {
|
201
|
|
- switch_to(menu);
|
|
257
|
+ plants.abort();
|
|
258
|
+ stop_time = millis();
|
|
259
|
+ switch_to(menu_valves_done);
|
202
|
260
|
} else if (state == menu_valves_done) {
|
203
|
261
|
switch_to(menu);
|
|
262
|
+ } else if (state == error) {
|
|
263
|
+ if (old_state != error) {
|
|
264
|
+ switch_to(old_state);
|
|
265
|
+ } else {
|
|
266
|
+ switch_to(menu);
|
|
267
|
+ }
|
204
|
268
|
}
|
205
|
269
|
}
|
206
|
270
|
|
|
@@ -219,10 +283,41 @@ uint32_t Statemachine::number_input(void) {
|
219
|
283
|
}
|
220
|
284
|
|
221
|
285
|
void Statemachine::act(void) {
|
|
286
|
+ if ((state == menu_pumps_running) || (state == menu_valves_running)) {
|
|
287
|
+ unsigned long runtime = millis() - start_time;
|
|
288
|
+ if ((runtime / 1000UL) >= selected_time) {
|
|
289
|
+ // stop if timeout has been reached
|
|
290
|
+ plants.abort();
|
|
291
|
+ stop_time = millis();
|
|
292
|
+ switch_to((state == menu_pumps_running) ? menu_pumps_done : menu_valves_done);
|
|
293
|
+ } else if ((millis() - last_animation_time) >= 500) {
|
|
294
|
+ // update animation if needed
|
|
295
|
+ last_animation_time = millis();
|
|
296
|
+ switch_to(state);
|
|
297
|
+ }
|
|
298
|
+ }
|
222
|
299
|
|
|
300
|
+ if ((state == menu_pumps_running) || ((state == menu_valves_running) && (selected_id == (plants.countPlants() + 1)))) {
|
|
301
|
+ // check water level state
|
|
302
|
+ auto wl = plants.getWaterlevel();
|
|
303
|
+ if (wl == Plants::full) {
|
|
304
|
+ plants.abort();
|
|
305
|
+ stop_time = millis();
|
|
306
|
+ switch_to((state == menu_pumps_running) ? menu_pumps_done : menu_valves_done);
|
|
307
|
+ } else if (wl == Plants::invalid) {
|
|
308
|
+ plants.abort();
|
|
309
|
+ error_condition = "Invalid sensor state";
|
|
310
|
+ state = (state == menu_pumps_running) ? menu_pumps : menu_valves;
|
|
311
|
+ switch_to(error);
|
|
312
|
+ }
|
|
313
|
+ }
|
223
|
314
|
}
|
224
|
315
|
|
225
|
316
|
void Statemachine::switch_to(States s) {
|
|
317
|
+ if (s == error) {
|
|
318
|
+ old_state = state;
|
|
319
|
+ }
|
|
320
|
+
|
226
|
321
|
state = s;
|
227
|
322
|
|
228
|
323
|
if (s == init) {
|
|
@@ -239,87 +334,129 @@ void Statemachine::switch_to(States s) {
|
239
|
334
|
-1);
|
240
|
335
|
} else if (s == menu_auto) {
|
241
|
336
|
print("------- Auto -------",
|
242
|
|
- "1: Water only",
|
243
|
|
- "2: With fertilizer",
|
|
337
|
+ "",
|
|
338
|
+ "TODO not implemented",
|
244
|
339
|
"",
|
245
|
340
|
-1);
|
246
|
341
|
} else if (s == menu_auto_mode) {
|
247
|
|
- print("",
|
248
|
|
- "",
|
|
342
|
+ print("menu_auto_mode",
|
249
|
343
|
"",
|
|
344
|
+ "TODO not implemented",
|
250
|
345
|
"",
|
251
|
346
|
-1);
|
252
|
347
|
} else if (s == menu_auto_go) {
|
253
|
|
- print("",
|
254
|
|
- "",
|
|
348
|
+ print("menu_auto_go",
|
255
|
349
|
"",
|
|
350
|
+ "TODO not implemented",
|
256
|
351
|
"",
|
257
|
352
|
-1);
|
258
|
353
|
} else if (s == menu_auto_done) {
|
259
|
|
- print("",
|
260
|
|
- "",
|
|
354
|
+ print("menu_auto_done",
|
261
|
355
|
"",
|
|
356
|
+ "TODO not implemented",
|
262
|
357
|
"",
|
263
|
358
|
-1);
|
264
|
359
|
} else if (s == menu_pumps) {
|
|
360
|
+ String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
|
|
361
|
+
|
265
|
362
|
print("------- Pump -------",
|
266
|
363
|
"Please select pump",
|
267
|
|
- "(Input 1 to 3)",
|
|
364
|
+ a.c_str(),
|
268
|
365
|
"Pump: ",
|
269
|
366
|
3);
|
270
|
367
|
} else if (s == menu_pumps_time) {
|
271
|
|
- print("------ Pump X ------",
|
|
368
|
+ String header = String("------ Pump ") + String(selected_id) + String(" ------");
|
|
369
|
+
|
|
370
|
+ print(header.c_str(),
|
272
|
371
|
"Please set runtime",
|
273
|
372
|
"(Input in seconds)",
|
274
|
373
|
"Runtime: ",
|
275
|
374
|
3);
|
276
|
375
|
} else if (s == menu_pumps_go) {
|
277
|
|
- print("",
|
278
|
|
- "",
|
279
|
|
- "",
|
280
|
|
- "",
|
|
376
|
+ String a = String("Pump No. ") + String(selected_id);
|
|
377
|
+ String b = String("Runtime ") + String(selected_time) + String('s');
|
|
378
|
+
|
|
379
|
+ print("----- Confirm? -----",
|
|
380
|
+ a.c_str(),
|
|
381
|
+ b.c_str(),
|
|
382
|
+ " # Confirm",
|
281
|
383
|
-1);
|
282
|
384
|
} else if (s == menu_pumps_running) {
|
283
|
|
- print("",
|
284
|
|
- "",
|
285
|
|
- "",
|
286
|
|
- "",
|
|
385
|
+ unsigned long runtime = millis() - start_time;
|
|
386
|
+ String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
|
|
387
|
+
|
|
388
|
+ unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
|
|
389
|
+ String b;
|
|
390
|
+ for (unsigned long i = 0; i < anim; i++) {
|
|
391
|
+ b += '#';
|
|
392
|
+ }
|
|
393
|
+
|
|
394
|
+ print("---- Dispensing ----",
|
|
395
|
+ a.c_str(),
|
|
396
|
+ b.c_str(),
|
|
397
|
+ "Hit any key to stop!",
|
287
|
398
|
-1);
|
288
|
399
|
} else if (s == menu_pumps_done) {
|
289
|
|
- print("",
|
290
|
|
- "",
|
291
|
|
- "",
|
292
|
|
- "",
|
|
400
|
+ String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
|
|
401
|
+
|
|
402
|
+ print("------- Done -------",
|
|
403
|
+ "Dispensing finished",
|
|
404
|
+ a.c_str(),
|
|
405
|
+ "Hit any key for menu",
|
293
|
406
|
-1);
|
294
|
407
|
} else if (s == menu_valves) {
|
|
408
|
+ String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
|
|
409
|
+
|
295
|
410
|
print("------ Valves ------",
|
296
|
411
|
"Please select valve",
|
297
|
|
- "(Input 1 to 5)",
|
|
412
|
+ a.c_str(),
|
298
|
413
|
"Valve: ",
|
299
|
414
|
3);
|
300
|
415
|
} else if (s == menu_valves_time) {
|
301
|
|
- print("----- Valve XX -----",
|
|
416
|
+ String header = String("----- Valve ") + String(selected_id) + String(" -----");
|
|
417
|
+
|
|
418
|
+ print(header.c_str(),
|
302
|
419
|
"Please set runtime",
|
303
|
420
|
"(Input in seconds)",
|
304
|
421
|
"Runtime: ",
|
305
|
422
|
3);
|
306
|
423
|
} else if (s == menu_valves_go) {
|
307
|
|
- print("",
|
308
|
|
- "",
|
309
|
|
- "",
|
310
|
|
- "",
|
|
424
|
+ String a = String("Valve No. ") + String(selected_id);
|
|
425
|
+ String b = String("Runtime ") + String(selected_time) + String('s');
|
|
426
|
+
|
|
427
|
+ print("----- Confirm? -----",
|
|
428
|
+ a.c_str(),
|
|
429
|
+ b.c_str(),
|
|
430
|
+ " # Confirm",
|
311
|
431
|
-1);
|
312
|
432
|
} else if (s == menu_valves_running) {
|
313
|
|
- print("",
|
314
|
|
- "",
|
315
|
|
- "",
|
316
|
|
- "",
|
|
433
|
+ unsigned long runtime = millis() - start_time;
|
|
434
|
+ String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
|
|
435
|
+
|
|
436
|
+ unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
|
|
437
|
+ String b;
|
|
438
|
+ for (unsigned long i = 0; i <= anim; i++) {
|
|
439
|
+ b += '#';
|
|
440
|
+ }
|
|
441
|
+
|
|
442
|
+ print("---- Dispensing ----",
|
|
443
|
+ a.c_str(),
|
|
444
|
+ b.c_str(),
|
|
445
|
+ "Hit any key to stop!",
|
317
|
446
|
-1);
|
318
|
447
|
} else if (s == menu_valves_done) {
|
319
|
|
- print("",
|
320
|
|
- "",
|
321
|
|
- "",
|
322
|
|
- "",
|
|
448
|
+ String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
|
|
449
|
+
|
|
450
|
+ print("------- Done -------",
|
|
451
|
+ "Dispensing finished",
|
|
452
|
+ a.c_str(),
|
|
453
|
+ "Hit any key for menu",
|
|
454
|
+ -1);
|
|
455
|
+ } else if (s == error) {
|
|
456
|
+ print("------ Error! ------",
|
|
457
|
+ "There is a problem:",
|
|
458
|
+ error_condition.c_str(),
|
|
459
|
+ " Press any key...",
|
323
|
460
|
-1);
|
324
|
461
|
}
|
325
|
462
|
}
|