123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- /*
- * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
- *
- * This file is part of Giess-o-mat.
- *
- * Giess-o-mat is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Giess-o-mat is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
- */
-
- #include "Plants.h"
- #include "DebugLog.h"
- #include "Statemachine.h"
- #include "config.h"
-
- Statemachine::DigitBuffer::DigitBuffer(int _size) {
- size = _size;
- pos = 0;
- digits = new int[size];
- }
-
- Statemachine::DigitBuffer::~DigitBuffer() {
- delete digits;
- }
-
- bool Statemachine::DigitBuffer::spaceLeft(void) {
- return (pos < size);
- }
-
- bool Statemachine::DigitBuffer::hasDigits(void) {
- return (pos > 0);
- }
-
- int Statemachine::DigitBuffer::countDigits(void) {
- return pos;
- }
-
- void Statemachine::DigitBuffer::addDigit(int d) {
- if (spaceLeft()) {
- digits[pos] = d;
- pos++;
- }
- }
-
- void Statemachine::DigitBuffer::removeDigit(void) {
- if (hasDigits()) {
- pos--;
- }
- }
-
- void Statemachine::DigitBuffer::clear(void) {
- pos = 0;
- }
-
- uint32_t Statemachine::DigitBuffer::getNumber(void) {
- uint32_t fact = 1;
- uint32_t sum = 0;
- for (int i = (pos - 1); i >= 0; i--) {
- sum += digits[i] * fact;
- fact *= 10;
- }
- return sum;
- }
-
- static const char *state_names[] = {
- stringify(init),
- stringify(menu),
- stringify(auto_mode),
- stringify(auto_fert),
- stringify(auto_fert_run),
- stringify(auto_tank_run),
- stringify(auto_plant),
- stringify(auto_plant_run),
- stringify(auto_done),
- stringify(menu_pumps),
- stringify(menu_pumps_time),
- stringify(menu_pumps_go),
- stringify(menu_pumps_run),
- stringify(menu_pumps_done),
- stringify(menu_valves),
- stringify(menu_valves_time),
- stringify(menu_valves_go),
- stringify(menu_valves_run),
- stringify(menu_valves_done),
- stringify(error)
- };
-
- const char *Statemachine::getStateName(void) {
- return state_names[state];
- }
-
- Statemachine::Statemachine(print_fn _print, backspace_fn _backspace)
- : db(7), selected_plants(plants.countPlants()) {
- state = init;
- old_state = init;
- print = _print;
- backspace = _backspace;
-
- selected_id = 0;
- selected_time = 0;
- start_time = 0;
- stop_time = 0;
- last_animation_time = 0;
- error_condition = "";
- }
-
- void Statemachine::begin(void) {
- switch_to(init);
- }
-
- void Statemachine::input(int n) {
- if (state == init) {
- switch_to(menu);
- } else if (state == menu) {
- if (n == 1) {
- switch_to(auto_mode);
- } else if (n == 2) {
- switch_to(menu_pumps);
- } else if (n == 3) {
- switch_to(menu_valves);
- } else if ((n == -1) || (n == -2)) {
- switch_to(init);
- }
- } else if (state == auto_mode) {
- if (n == 1) {
- switch_to(auto_fert);
- } else if (n == 2) {
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::full) && (wl != Plants::invalid)) {
- plants.openWaterInlet();
- selected_id = plants.countPlants() + 1;
- selected_time = MAX_TANK_FILL_TIME;
- start_time = millis();
- switch_to(auto_tank_run);
- } else if (wl == Plants::full) {
- stop_time = millis();
- switch_to(auto_mode);
- } else if (wl == Plants::invalid) {
- error_condition = "Invalid sensor state";
- state = auto_mode;
- switch_to(error);
- }
- } else if (n == 3) {
- selected_plants.clear();
- switch_to(auto_plant);
- } else if ((n == -1) || (n == -2)) {
- switch_to(menu);
- }
- } else if (state == auto_fert) {
- if ((n >= 1) && (n <= 3)) {
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::full) && (wl != Plants::invalid)) {
- plants.startFertilizer(n - 1);
- selected_id = n;
- selected_time = AUTO_PUMP_RUNTIME;
- start_time = millis();
- switch_to(auto_fert_run);
- } else if (wl == Plants::full) {
- stop_time = millis();
- switch_to(auto_mode);
- } else if (wl == Plants::invalid) {
- error_condition = "Invalid sensor state";
- state = auto_mode;
- switch_to(error);
- }
- } else if ((n == -1) || (n == -2)) {
- switch_to(auto_mode);
- }
- } else if (state == auto_fert_run) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (state == auto_tank_run) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (state == auto_plant) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- } else {
- switch_to(auto_mode);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::empty) && (wl != Plants::invalid)) {
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i);
- }
- }
- selected_plants.clear();
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- switch_to(auto_plant_run);
- } else if (wl == Plants::empty) {
- stop_time = millis();
- switch_to(auto_mode);
- } else if (wl == Plants::invalid) {
- error_condition = "Invalid sensor state";
- state = auto_mode;
- switch_to(error);
- }
- } else {
- selected_id = number_input();
- if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
- error_condition = "Invalid plant ID!";
- switch_to(error);
- } else {
- selected_plants.set(selected_id - 1);
- switch_to(auto_plant);
- }
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- } else {
- backspace();
- }
- }
- } else if (state == auto_plant_run) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (state == auto_done) {
- switch_to(auto_mode);
- } else if (state == menu_pumps) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- } else {
- switch_to(menu);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_id = number_input();
-
- if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
- error_condition = "Invalid pump ID!";
- switch_to(error);
- } else {
- switch_to(menu_pumps_time);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- } else {
- backspace();
- }
- }
- } else if (state == menu_pumps_time) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- } else {
- switch_to(menu_pumps);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_time = number_input();
-
- if ((selected_time <= 0) || (selected_time > MAX_PUMP_RUNTIME)) {
- error_condition = "Invalid time range!";
- switch_to(error);
- } else {
- switch_to(menu_pumps_go);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- } else {
- backspace();
- }
- }
- } else if (state == menu_pumps_go) {
- if (n == -2) {
- start_time = millis();
- last_animation_time = start_time;
-
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::full) && (wl != Plants::invalid)) {
- plants.startFertilizer(selected_id - 1);
- switch_to(menu_pumps_run);
- } else if (wl == Plants::full) {
- stop_time = millis();
- switch_to(menu_pumps_done);
- } else if (wl == Plants::invalid) {
- error_condition = "Invalid sensor state";
- state = menu_pumps;
- switch_to(error);
- }
- } else {
- switch_to(menu_pumps_time);
- }
- } else if (state == menu_pumps_run) {
- plants.abort();
- stop_time = millis();
- switch_to(menu_pumps_done);
- } else if (state == menu_pumps_done) {
- switch_to(menu);
- } else if (state == menu_valves) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- } else {
- switch_to(menu);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_id = number_input();
-
- if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
- error_condition = "Invalid valve ID!";
- switch_to(error);
- } else {
- switch_to(menu_valves_time);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- } else {
- backspace();
- }
- }
- } else if (state == menu_valves_time) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- } else {
- switch_to(menu_valves);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_time = number_input();
-
- if ((selected_time <= 0) || (selected_time > MAX_VALVE_RUNTIME)) {
- error_condition = "Invalid time range!";
- switch_to(error);
- } else {
- switch_to(menu_valves_go);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- } else {
- backspace();
- }
- }
- } else if (state == menu_valves_go) {
- if (n == -2) {
- start_time = millis();
- last_animation_time = start_time;
-
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::full) && (wl != Plants::invalid)) {
- if (selected_id >= (plants.countPlants() + 1)) {
- plants.openWaterInlet();
- } else {
- plants.startPlant(selected_id - 1);
- }
-
- switch_to(menu_valves_run);
- } else if (wl == Plants::full) {
- stop_time = millis();
- switch_to(menu_valves_done);
- } else if (wl == Plants::invalid) {
- error_condition = "Invalid sensor state";
- state = menu_valves;
- switch_to(error);
- }
- } else {
- switch_to(menu_valves_time);
- }
- } else if (state == menu_valves_run) {
- plants.abort();
- stop_time = millis();
- switch_to(menu_valves_done);
- } else if (state == menu_valves_done) {
- switch_to(menu);
- } else if (state == error) {
- if (old_state != error) {
- switch_to(old_state);
- } else {
- switch_to(menu);
- }
- }
- }
-
- uint32_t Statemachine::number_input(void) {
- for (int i = 0; i < db.countDigits(); i++) {
- backspace();
- }
-
- uint32_t n = db.getNumber();
- db.clear();
-
- debug.print("Whole number input: ");
- debug.println(n);
-
- return n;
- }
-
- void Statemachine::act(void) {
- if ((state == menu_pumps_run) || (state == menu_valves_run)) {
- unsigned long runtime = millis() - start_time;
- if ((runtime / 1000UL) >= selected_time) {
- // stop if timeout has been reached
- plants.abort();
- stop_time = millis();
- switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
- } else if ((millis() - last_animation_time) >= 500) {
- // update animation if needed
- last_animation_time = millis();
- switch_to(state);
- }
- }
-
- #ifdef CHECK_SENSORS_VALVE_PUMP_MENU_FULL
- if ((state == menu_pumps_run) || ((state == menu_valves_run) && (selected_id == (plants.countPlants() + 1)))) {
- // check water level state
- auto wl = plants.getWaterlevel();
- if (wl == Plants::full) {
- plants.abort();
- stop_time = millis();
- switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = "Invalid sensor state";
- state = (state == menu_pumps_run) ? menu_pumps : menu_valves;
- switch_to(error);
- }
- }
- #endif // CHECK_SENSORS_VALVE_PUMP_MENU_FULL
-
- #ifdef CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
- if ((state == menu_valves_run) && (selected_id <= plants.countPlants())) {
- // check water level state
- auto wl = plants.getWaterlevel();
- if (wl == Plants::empty) {
- plants.abort();
- stop_time = millis();
- switch_to(menu_valves_done);
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = "Invalid sensor state";
- state = menu_valves;
- switch_to(error);
- }
- }
- #endif // CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
-
- if ((state == auto_fert_run) || (state == auto_tank_run)) {
- unsigned long runtime = millis() - start_time;
- if ((runtime / 1000UL) >= selected_time) {
- // stop if timeout has been reached
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if ((millis() - last_animation_time) >= 500) {
- // update animation if needed
- last_animation_time = millis();
- switch_to(state);
- }
-
- // check water level state
- auto wl = plants.getWaterlevel();
- if (wl == Plants::full) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = "Invalid sensor state";
- state = auto_mode;
- switch_to(error);
- }
- }
-
- if (state == auto_plant_run) {
- unsigned long runtime = millis() - start_time;
- if ((runtime / 1000UL) >= selected_time) {
- // stop if timeout has been reached
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if ((millis() - last_animation_time) >= 500) {
- // update animation if needed
- last_animation_time = millis();
- switch_to(state);
- }
-
- // check water level state
- auto wl = plants.getWaterlevel();
- if (wl == Plants::empty) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = "Invalid sensor state";
- state = auto_mode;
- switch_to(error);
- }
- }
- }
-
- void Statemachine::switch_to(States s) {
- old_state = state;
- state = s;
-
- if (s == init) {
- String a = String("- Giess-o-mat V") + FIRMWARE_VERSION + String(" -");
-
- print(a.c_str(),
- "Usage: Enter number",
- "* Delete prev. digit",
- "# Execute input num.",
- -1);
- } else if (s == menu) {
- print("------- Menu -------",
- "1: Automatic program",
- "2: Fertilizer pumps",
- "3: Outlet valves",
- -1);
- } else if (s == auto_mode) {
- print("------- Auto -------",
- "1: Add Fertilizer",
- "2: Fill Reservoir",
- "3: Water a plant",
- -1);
- } else if (s == auto_fert) {
- print("---- Fertilizer ----",
- "1: Vegetation Phase",
- "2: Bloom Phase",
- "3: Special",
- -1);
- } else if (s == auto_fert_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
-
- unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
- String b;
- for (unsigned long i = 0; i < anim; i++) {
- b += '#';
- }
-
- print("---- Dispensing ----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if (s == auto_tank_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
-
- unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
- String b;
- for (unsigned long i = 0; i < anim; i++) {
- b += '#';
- }
-
- print("--- Filling Tank ---",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if (s == auto_plant) {
- String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
-
- print("--- Select Plant ---",
- "Leave empty if done!",
- a.c_str(),
- "Plant: ",
- 3);
- } else if (s == auto_plant_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
-
- unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
- String b;
- for (unsigned long i = 0; i < anim; i++) {
- b += '#';
- }
-
- print("----- Watering -----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if (s == auto_done) {
- String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
-
- print("------- Done -------",
- "Dispensing finished",
- a.c_str(),
- "Hit any key for menu",
- -1);
- } else if (s == menu_pumps) {
- String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
-
- print("------- Pump -------",
- "Please select pump",
- a.c_str(),
- "Pump: ",
- 3);
- } else if (s == menu_pumps_time) {
- String header = String("------ Pump ") + String(selected_id) + String(" ------");
-
- print(header.c_str(),
- "Please set runtime",
- "(Input in seconds)",
- "Runtime: ",
- 3);
- } else if (s == menu_pumps_go) {
- String a = String("Pump No. ") + String(selected_id);
- String b = String("Runtime ") + String(selected_time) + String('s');
-
- print("----- Confirm? -----",
- a.c_str(),
- b.c_str(),
- " # Confirm",
- -1);
- } else if (s == menu_pumps_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
-
- unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
- String b;
- for (unsigned long i = 0; i < anim; i++) {
- b += '#';
- }
-
- print("---- Dispensing ----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if (s == menu_pumps_done) {
- String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
-
- print("------- Done -------",
- "Dispensing finished",
- a.c_str(),
- "Hit any key for menu",
- -1);
- } else if (s == menu_valves) {
- String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
-
- print("------ Valves ------",
- "Please select valve",
- a.c_str(),
- "Valve: ",
- 3);
- } else if (s == menu_valves_time) {
- String header = String("----- Valve ") + String(selected_id) + String(" -----");
-
- print(header.c_str(),
- "Please set runtime",
- "(Input in seconds)",
- "Runtime: ",
- 3);
- } else if (s == menu_valves_go) {
- String a = String("Valve No. ") + String(selected_id);
- String b = String("Runtime ") + String(selected_time) + String('s');
-
- print("----- Confirm? -----",
- a.c_str(),
- b.c_str(),
- " # Confirm",
- -1);
- } else if (s == menu_valves_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
-
- unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
- String b;
- for (unsigned long i = 0; i <= anim; i++) {
- b += '#';
- }
-
- print("---- Dispensing ----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if (s == menu_valves_done) {
- String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
-
- print("------- Done -------",
- "Dispensing finished",
- a.c_str(),
- "Hit any key for menu",
- -1);
- } else if (s == error) {
- print("------ Error! ------",
- "There is a problem:",
- error_condition.c_str(),
- " Press any key...",
- -1);
- }
- }
|