123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290 |
- /*
- * 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 "WifiStuff.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_a),
- stringify(menu_b),
- stringify(menu_c),
- stringify(auto_mode_a),
- stringify(auto_mode_b),
- stringify(auto_fert_a),
- stringify(auto_fert_b),
- stringify(auto_fert_run),
- stringify(auto_tank_run),
- stringify(auto_stirr_run),
- stringify(auto_plant),
- stringify(auto_plant_run),
- stringify(auto_done),
- stringify(fillnwater_plant),
- stringify(fillnwater_tank_run),
- stringify(fillnwater_plant_run),
- stringify(automation_mode),
- 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(menu_aux),
- stringify(menu_aux_time),
- stringify(menu_aux_go),
- stringify(menu_aux_run),
- stringify(menu_aux_done),
- stringify(error)
- };
-
- const char *Statemachine::getStateName(void) {
- return state_names[state];
- }
-
- bool Statemachine::isIdle(void) {
- return state == init;
- }
-
- 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 = "";
- into_state_time = 0;
- filling_started_empty = false;
- watering_started_full = false;
- menu_entered_digits = "";
- }
-
- void Statemachine::begin(void) {
- switch_to(init);
- }
-
- void Statemachine::input(int n) {
- if (state == init) {
- switch_to(menu_a);
- } else if ((state == menu_a) || (state == menu_b) || (state == menu_c)) {
- if (n == 1) {
- switch_to(auto_mode_a);
- } else if (n == 2) {
- switch_to(automation_mode);
- } else if (n == 3) {
- switch_to(menu_pumps);
- } else if (n == 4) {
- switch_to(menu_valves);
- } else if (n == 5) {
- switch_to(menu_aux);
- } else if (n == -1) {
- switch_to(init);
- } else if (n == -2) {
- switch_to((state == menu_a) ? menu_b : ((state == menu_b) ? menu_c : menu_a));
- }
- } else if (state == automation_mode) {
- // TODO
- switch_to(menu_a);
- } else if ((state == auto_mode_a) || (state == auto_mode_b)) {
- if (n == 1) {
- switch_to(auto_fert_a);
- } else if (n == 2) {
- selected_plants.clear();
- switch_to(fillnwater_plant);
- } else if (n == 3) {
- 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_a);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else if (n == 4) {
- selected_plants.clear();
- switch_to(auto_plant);
- } else if (n == -1) {
- switch_to(menu_a);
- } else if (n == -2) {
- switch_to((state == auto_mode_a) ? auto_mode_b : auto_mode_a);
- }
- } else if ((state == auto_fert_a) || (state == auto_fert_b)) {
- 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_a);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else if (n == 4) {
- plants.startAux(0);
- selected_id = 1;
- selected_time = AUTO_STIRR_RUNTIME;
- start_time = millis();
- switch_to(auto_stirr_run);
- } else if (n == -1) {
- switch_to(auto_mode_a);
- } else if (n == -2) {
- switch_to((state == auto_fert_a) ? auto_fert_b : auto_fert_a);
- }
- } else if ((state == auto_fert_run) || (state == auto_tank_run)
- || (state == auto_stirr_run)) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (state == auto_plant) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } else {
- switch_to(auto_mode_b);
- }
- } 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_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_b);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_b;
- switch_to(error);
- }
- } else {
- selected_id = number_input();
- if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
- error_condition = F("Invalid plant ID!");
- switch_to(error);
- } else {
- selected_plants.set(selected_id - 1);
- menu_entered_digits = "";
- switch_to(auto_plant);
- }
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } 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_a);
- } else if (state == menu_pumps) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } else {
- switch_to(menu_b);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_id = number_input();
-
- if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
- error_condition = F("Invalid pump ID!");
- switch_to(error);
- } else {
- switch_to(menu_pumps_time);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if (state == fillnwater_plant) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } else {
- switch_to(auto_mode_a);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- int found = 0;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- found = 1;
- break;
- }
- }
- if (found != 0) {
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::full) && (wl != Plants::invalid)) {
- // if the waterlevel is currently empty, we
- // set a flag to record the time to fill
- filling_started_empty = (wl == Plants::empty);
-
- plants.openWaterInlet();
- selected_id = plants.countPlants() + 1;
- selected_time = MAX_TANK_FILL_TIME;
- start_time = millis();
- switch_to(fillnwater_tank_run);
- } else if (wl == Plants::full) {
- stop_time = millis();
- 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);
- }
- }
-
- // for recording the flowrate
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- switch_to(fillnwater_plant_run);
- } else if (wl == Plants::empty) {
- stop_time = millis();
- switch_to(auto_mode_a);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- }
- } else {
- selected_id = number_input();
- if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
- error_condition = F("Invalid plant ID!");
- switch_to(error);
- } else {
- selected_plants.set(selected_id - 1);
- menu_entered_digits = "";
- switch_to(fillnwater_plant);
- }
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if (state == fillnwater_tank_run) {
- plants.abort();
- stop_time = millis();
- 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);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- switch_to(fillnwater_plant_run);
- } else if (wl == Plants::empty) {
- switch_to(auto_mode_a);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else if (state == fillnwater_plant_run) {
- plants.abort();
- stop_time = millis();
- switch_to(auto_done);
- } else if (state == menu_pumps_time) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } 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 = F("Invalid time range!");
- switch_to(error);
- } else {
- switch_to(menu_pumps_go);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } 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 = F("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_b);
- } else if (state == menu_valves) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } else {
- switch_to(menu_b);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_id = number_input();
-
- if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
- error_condition = F("Invalid valve ID!");
- switch_to(error);
- } else {
- switch_to(menu_valves_time);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if (state == menu_valves_time) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } 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 = F("Invalid time range!");
- switch_to(error);
- } else {
- switch_to(menu_valves_go);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } 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 = F("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_b);
- } else if (state == menu_aux) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } else {
- switch_to(menu_c);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_id = number_input();
-
- if ((selected_id <= 0) || (selected_id > plants.countAux())) {
- error_condition = F("Invalid valve ID!");
- switch_to(error);
- } else {
- switch_to(menu_aux_time);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if (state == menu_aux_time) {
- if (n == -1) {
- if (db.hasDigits()) {
- backspace();
- db.removeDigit();
- if (menu_entered_digits.length() > 0) {
- menu_entered_digits.remove(menu_entered_digits.length() - 1);
- switch_to(state);
- }
- } else {
- switch_to(menu_aux);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- return;
- }
-
- selected_time = number_input();
-
- if ((selected_time <= 0) || (selected_time > MAX_AUX_RUNTIME)) {
- error_condition = F("Invalid time range!");
- switch_to(error);
- } else {
- switch_to(menu_aux_go);
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if (state == menu_aux_go) {
- if (n == -2) {
- start_time = millis();
- last_animation_time = start_time;
- plants.startAux(selected_id - 1);
- switch_to(menu_aux_run);
- } else {
- switch_to(menu_aux_time);
- }
- } else if (state == menu_aux_run) {
- plants.abort();
- stop_time = millis();
- switch_to(menu_aux_done);
- } else if (state == menu_aux_done) {
- switch_to(menu_c);
- } else if (state == error) {
- if (old_state != error) {
- switch_to(old_state);
- } else {
- switch_to(menu_a);
- }
- }
- }
-
- 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)
- || (state == menu_aux_run)
- || (state == auto_stirr_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 : ((state == menu_valves_run) ? menu_valves_done : ((state == menu_aux_run) ? menu_aux_done : auto_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 = F("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 = F("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) || (state == fillnwater_tank_run)) {
- unsigned long runtime = millis() - start_time;
- if ((runtime / 1000UL) >= selected_time) {
- // stop if timeout has been reached
- plants.abort();
- stop_time = millis();
- if (state == fillnwater_tank_run) {
- 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);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- switch_to(fillnwater_plant_run);
- } else if (wl == Plants::empty) {
- stop_time = millis();
- switch_to(auto_done);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else {
- 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();
- if (state == fillnwater_tank_run) {
- // record time to fill here, if we started with
- // an empty tank at the start of filling
- if (filling_started_empty) {
- unsigned long time_to_fill = stop_time - start_time;
- debug.print("Filling tank took ");
- debug.print(String(time_to_fill));
- debug.println("ms");
-
- #if defined(PLATFORM_ESP)
- bool success = wifi_write_database(time_to_fill, "calibrated_filling", -1);
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.println("/calibrated_filling");
- }
- #endif // PLATFORM_ESP
- }
-
- 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);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- switch_to(fillnwater_plant_run);
- } else if (wl == Plants::empty) {
- stop_time = millis();
- switch_to(auto_mode_a);
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else {
- switch_to(auto_done);
- }
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- }
-
- if ((state == auto_plant_run) || (state == fillnwater_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();
-
- // if we started watering with a full tank
- // and then finished watering when it was empty
- // and we were only watering a single plant
- // look at this as a "calibration run" and record
- // the time it took to empty the tank
- if ((state == fillnwater_plant_run) && watering_started_full && (selected_plants.countSet() == 1)) {
- unsigned long time_to_water = stop_time - start_time;
- debug.print("Watering plant ");
- debug.print(selected_plants.getFirstSet() + 1);
- debug.print(" with the complete tank took ");
- debug.print(String(time_to_water));
- debug.println("ms");
-
- #if defined(PLATFORM_ESP)
- bool success = wifi_write_database(time_to_water, "calibrated_watering", selected_plants.getFirstSet() + 1);
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.println("/calibrated_watering");
- }
- #endif // PLATFORM_ESP
- }
-
- switch_to(auto_done);
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- }
-
- if ((state == menu_a) || (state == menu_b) || (state == menu_c)
- || (state == automation_mode) || (state == auto_done)
- || (state == auto_mode_a) || (state == auto_mode_b)
- || (state == auto_fert_a) || (state == auto_fert_b)
- || (state == auto_plant) || (state == fillnwater_plant)
- || (state == menu_pumps) || (state == menu_pumps_time)
- || (state == menu_pumps_go) || (state == menu_pumps_done)
- || (state == menu_valves) || (state == menu_valves_time)
- || (state == menu_valves_go) || (state == menu_valves_done)
- || (state == menu_aux) || (state == menu_aux_time)
- || (state == menu_aux_go) || (state == menu_aux_done)) {
- unsigned long runtime = millis() - into_state_time;
- if (runtime >= BACK_TO_IDLE_TIMEOUT) {
- debug.print("Idle timeout reached in state ");
- debug.println(state_names[state]);
- switch_to(init);
- }
- }
- }
-
- void Statemachine::switch_to(States s) {
- old_state = state;
- state = s;
- into_state_time = millis();
-
- if (old_state != state) {
- // don't spam log with every animation state "change"
- debug.print("switch_to ");
- debug.print(state_names[old_state]);
- debug.print(" --> ");
- debug.println(state_names[state]);
-
- menu_entered_digits = "";
- }
-
- if (s == init) {
- String a = String(F("- Giess-o-mat V")) + FIRMWARE_VERSION + String(F(" -"));
-
- print(a.c_str(),
- "Usage: Enter number",
- "* Delete prev. digit",
- "# Execute input num.",
- -1);
- } else if (s == menu_a) {
- print("----- Menu 1/3 -----",
- "1: Manual Operation",
- "2: Automation",
- "#: Go to page 2/3...",
- -1);
- } else if (s == menu_b) {
- print("----- Menu 2/3 -----",
- "3: Fertilizer pumps",
- "4: Outlet valves",
- "#: Go to page 3/3...",
- -1);
- } else if (s == menu_c) {
- print("----- Menu 3/3 -----",
- "5: Aux. Outputs",
- "",
- "#: Go to page 1/3...",
- -1);
- } else if (state == automation_mode) {
- // TODO
- print("---- Automation ----",
- "TODO NOT IMPLEMENTED",
- "TODO NOT IMPLEMENTED",
- "TODO NOT IMPLEMENTED",
- -1);
- } else if (s == auto_mode_a) {
- print("---- Manual 1/2 ----",
- "1: Add Fertilizer",
- "2: Fill 'n' Water",
- "#: Go to page 2/2...",
- -1);
- } else if (s == auto_mode_b) {
- print("---- Manual 2/2 ----",
- "3: Fill Reservoir",
- "4: Water a plant",
- "#: Go to page 1/2...",
- -1);
- } else if (s == auto_fert_a) {
- print("-- Fertilizer 1/2 --",
- "1: Vegetation Phase",
- "2: Bloom Phase",
- "#: Go to page 2/2...",
- -1);
- } else if (s == auto_fert_b) {
- print("-- Fertilizer 2/2 --",
- "3: Special Fert.",
- "4: Run Stirrer",
- "#: Go to page 1/2...",
- -1);
- } else if (s == auto_fert_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Time: ") + 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_stirr_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Time: ") + 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("----- Stirring -----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if ((s == auto_tank_run) || (s == fillnwater_tank_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String("Time: ") + 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) || (s == fillnwater_plant)) {
- String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
- String b = String(F("Plant: ")) + menu_entered_digits;
-
-
- print("--- Select Plant ---",
- "Leave empty if done!",
- a.c_str(),
- b.c_str(),
- 3);
- } else if ((s == auto_plant_run) || (s == fillnwater_plant_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String("Time: ") + 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);
-
- #if defined(PLATFORM_ESP)
- unsigned long runtime = stop_time - start_time;
- if ((old_state == auto_plant_run) || (old_state == fillnwater_plant_run)) {
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.println("/plant");
- }
- }
- }
- } else if (old_state == auto_fert_run) {
- bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.println("/fertilizer");
- }
- }
- #endif // PLATFORM_ESP
- } else if (s == menu_pumps) {
- String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
- String b = String(F("Pump: ")) + menu_entered_digits;
-
- print("------- Pump -------",
- "Please select pump",
- a.c_str(),
- b.c_str(),
- 3);
- } else if (s == menu_pumps_time) {
- String header = String("------ Pump ") + String(selected_id) + String(" ------");
- String b = String(F("Runtime: ")) + menu_entered_digits;
-
- print(header.c_str(),
- "Please set runtime",
- "(Input in seconds)",
- b.c_str(),
- 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("Time: ") + 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);
-
- #if defined(PLATFORM_ESP)
- unsigned long runtime = stop_time - start_time;
- bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.println("/fertilizer");
- }
- #endif // PLATFORM_ESP
- } else if (s == menu_valves) {
- String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
- String b = String(F("Valve: ")) + menu_entered_digits;
-
- print("------ Valves ------",
- "Please select valve",
- a.c_str(),
- b.c_str(),
- 3);
- } else if (s == menu_valves_time) {
- String header = String("----- Valve ") + String(selected_id) + String(" -----");
- String b = String(F("Runtime: ")) + menu_entered_digits;
-
- print(header.c_str(),
- "Please set runtime",
- "(Input in seconds)",
- b.c_str(),
- 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("Time: ") + 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);
-
- #if defined(PLATFORM_ESP)
- unsigned long runtime = stop_time - start_time;
- if (selected_id <= plants.countPlants()) {
- bool success = wifi_write_database(runtime / 1000, "plant", selected_id);
- if (!success) {
- debug.print("Error writing to InfluxDB ");
- debug.print(INFLUXDB_HOST);
- debug.print(":");
- debug.print(INFLUXDB_PORT);
- debug.print("/");
- debug.print(INFLUXDB_DATABASE);
- debug.println("/plant");
- }
- }
- #endif // PLATFORM_ESP
- } else if (s == menu_aux) {
- String a = String("(Input 1 to ") + String(plants.countAux()) + String(")");
- String b = String(F("Aux.: ")) + menu_entered_digits;
-
- print("------- Aux. -------",
- "Please select aux.",
- a.c_str(),
- b.c_str(),
- 3);
- } else if (s == menu_aux_time) {
- String header = String("------ Aux ") + String(selected_id) + String(" ------");
- String b = String(F("Runtime: ")) + menu_entered_digits;
-
- print(header.c_str(),
- "Please set runtime",
- "(Input in seconds)",
- b.c_str(),
- 3);
- } else if (s == menu_aux_go) {
- String a = String("Aux 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_aux_run) {
- unsigned long runtime = millis() - start_time;
- String a = String("Time: ") + 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("----- Stirring -----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if (s == menu_aux_done) {
- String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
-
- print("------- Done -------",
- "Stirring 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);
- } else {
- debug.print("Invalid state ");
- debug.println(s);
- }
- }
|