123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091 |
- /*
- * 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"
- #include "config_pins.h"
-
- #ifdef FUNCTION_CONTROL
-
- 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(door_select),
- stringify(menu_a),
- stringify(menu_b),
- stringify(menu_c),
-
- stringify(auto_mode_a),
- stringify(auto_mode_b),
- stringify(auto_mode_c),
- 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_kickstart_run),
- stringify(auto_plant_run),
- stringify(auto_done),
-
- stringify(fillnwater_plant),
- stringify(fillnwater_tank_run),
- stringify(fillnwater_kickstart_run),
- stringify(fillnwater_plant_run),
-
- stringify(fullauto_fert),
- stringify(fullauto_plant),
- stringify(fullauto_stirr_run),
- stringify(fullauto_fert_run),
- stringify(fullauto_tank_run),
- stringify(fullauto_kickstart_run),
- stringify(fullauto_plant_run),
- stringify(fullauto_plant_overrun),
- stringify(fullauto_tank_purge_run),
- stringify(fullauto_kickstart_purge_run),
- stringify(fullauto_plant_purge_run),
- stringify(fullauto_plant_purge_overrun),
- stringify(fullauto_done),
-
- 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)
- };
-
- static int auto_pump_runtime[PUMP_COUNT] = AUTO_PUMP_RUNTIME;
-
- 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()),
- selected_ferts(plants.countFertilizers()) {
- 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) {
- #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
- 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_a);
- }
- } else if (n == -2) {
- switch_to(menu_a);
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- //menu_entered_digits += String(n);
- menu_entered_digits += String("*");
- switch_to(state);
- } else {
- backspace();
- }
-
- uint32_t n = db.getNumber();
- if (n == DOOR_LOCK_PIN) {
- db.clear();
- menu_entered_digits = "";
- selected_plants.clear();
- switch_to(door_select);
- } else if (db.countDigits() >= DOOR_LOCK_PIN_MAX_DIGITS) {
- db.clear();
- menu_entered_digits = "";
- switch_to(state);
- }
- }
- #else
- switch_to(menu_a);
- #endif
- } else if (state == door_select) {
- #if (LOCK_COUNT > 0)
- 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(init);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- for (int i = 0; i < LOCK_COUNT; i++) {
- if (selected_plants.isSet(i)) {
- plants.startAux(STIRRER_COUNT + i);
- delay(DOOR_LOCK_ON_TIME);
- plants.stopAux(STIRRER_COUNT + i);
- delay(DOOR_LOCK_NEXT_DELAY);
- }
- }
- plants.stopAllAux();
- switch_to(menu_a);
- } else {
- selected_id = number_input();
- if ((selected_id <= 0) || (selected_id > LOCK_COUNT)) {
- error_condition = F("Invalid lock ID!");
- switch_to(error);
- } else {
- selected_plants.set(selected_id - 1);
- menu_entered_digits = "";
- switch_to(state);
- }
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- #else
- // should never be reached
- switch_to(menu_a);
- #endif
- } 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);
- #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
- } else if (n == 6) {
- switch_to(door_select);
- #endif
- } 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) || (state == auto_mode_c)) {
- if (n == 1) {
- selected_ferts.clear();
- switch_to(fullauto_fert);
- } else if (n == 2) {
- switch_to(auto_fert_a);
- } else if (n == 3) {
- selected_plants.clear();
- switch_to(fillnwater_plant);
- } else if (n == 4) {
- 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 == 5) {
- 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 : ((state == auto_mode_b) ? auto_mode_c : auto_mode_a));
- }
- } else if ((state == auto_fert_a) || (state == auto_fert_b)) {
- // TODO fertilizer number currently "hardcoded" to 3 in UI
- 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[n - 1];
- 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) {
- for (int i = 0; i < STIRRER_COUNT; i++) {
- plants.startAux(i);
- }
- 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 == fullauto_stirr_run) {
- // allow user to stop stirring and continue with fertilizers
- plants.abort();
- stop_time = millis();
- if (selected_ferts.countSet() > 0) {
- selected_time = auto_pump_runtime[0];
- for (int i = 0; i < plants.countFertilizers(); i++) {
- if (auto_pump_runtime[i] > selected_time) {
- selected_time = auto_pump_runtime[i];
- }
-
- if (selected_ferts.isSet(i)) {
- plants.startFertilizer(i);
- }
- }
-
- start_time = millis();
- switch_to(fullauto_fert_run);
- } else {
- 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(fullauto_tank_run);
- } else if (wl == Plants::full) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_plant_run);
- }
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = F("Invalid sensor state");
- state = fullauto_fert;
- switch_to(error);
- }
- }
- } else if (state == fullauto_fert_run) {
- // allow user to stop fertizilers and continue with tank filling
- plants.abort();
- stop_time = millis();
- 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(fullauto_tank_run);
- } else if (wl == Plants::full) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_plant_run);
- }
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = F("Invalid sensor state");
- state = fullauto_fert;
- switch_to(error);
- }
- } else if ((state == fullauto_tank_run)
- || (state == fullauto_tank_purge_run)) {
- // allow user to stop tank and continue with kickstart
- plants.abort();
- stop_time = millis();
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::empty) && (wl != Plants::invalid)) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- if (state == fullauto_tank_run) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_kickstart_purge_run);
- }
- } else {
- if (state == fullauto_tank_run) {
- switch_to(fullauto_plant_run);
- } else {
- switch_to(fullauto_plant_purge_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 if ((state == fullauto_kickstart_run)
- || (state == fullauto_kickstart_purge_run)) {
- // allow user to stop kickstarting and continue with plants
- plants.abort();
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
-
- // start required valves
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, false);
- }
- }
-
- if (state == fullauto_kickstart_run) {
- switch_to(fullauto_plant_run);
- } else {
- switch_to(fullauto_plant_purge_run);
- }
- } else if ((state == auto_fert_run) || (state == auto_tank_run)
- || (state == auto_stirr_run) || (state == auto_plant_run)
- || (state == auto_plant_kickstart_run)
- || (state == fillnwater_kickstart_run)
- || (state == fullauto_plant_run)
- || (state == fullauto_plant_overrun)
- || (state == fullauto_plant_purge_run)
- || (state == fullauto_plant_purge_overrun)) {
- 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)) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(auto_plant_kickstart_run);
- } else {
- 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 == fullauto_fert) {
- 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()) {
- selected_plants.clear();
- switch_to(fullauto_plant);
- } else {
- selected_id = number_input();
- if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
- error_condition = F("Invalid fert. ID!");
- switch_to(error);
- } else {
- selected_ferts.set(selected_id - 1);
- menu_entered_digits = "";
- switch_to(fullauto_fert);
- }
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if (state == fullauto_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 {
- selected_ferts.clear();
- switch_to(fullauto_fert);
- }
- } else if (n == -2) {
- if (!db.hasDigits()) {
- if (selected_plants.countSet() <= 0) {
- // user has not selected a plant yet...
- return;
- }
-
- #ifdef FULLAUTO_MIN_PLANT_COUNT
- if (selected_plants.countSet() < FULLAUTO_MIN_PLANT_COUNT) {
- return;
- }
- #endif
-
- // check if we need to run fertilizers
- if (selected_ferts.countSet() > 0) {
- // stirr before pumping fertilizers
- for (int i = 0; i < STIRRER_COUNT; i++) {
- plants.startAux(i);
- }
- selected_id = 1;
- selected_time = AUTO_STIRR_RUNTIME;
- start_time = millis();
- switch_to(fullauto_stirr_run);
- } else {
- // immediately continue with filling tank
- 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(fullauto_tank_run);
- } else if (wl == Plants::full) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- // for recording the flowrate
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_plant_run);
- }
- } 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(fullauto_plant);
- }
- }
- } else {
- if (db.spaceLeft()) {
- db.addDigit(n);
- menu_entered_digits += String(n);
- switch_to(state);
- } else {
- backspace();
- }
- }
- } else if ((state == auto_done) || (state == fullauto_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()) {
- if (selected_plants.countSet() <= 0) {
- return;
- }
-
- 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) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- // for recording the flowrate
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fillnwater_kickstart_run);
- } else {
- switch_to(fillnwater_plant_run);
- }
- } 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)) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fillnwater_kickstart_run);
- } else {
- 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 {
- // TODO support testing kickstart
- plants.startPlant(selected_id - 1, false);
- }
-
- 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 == fullauto_stirr_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();
- if (state == menu_pumps_run) {
- switch_to(menu_pumps_done);
- } else if (state == menu_valves_run) {
- switch_to(menu_valves_done);
- } else if (state == menu_aux_run) {
- switch_to(menu_aux_done);
- } else if (state == fullauto_stirr_run) {
- if (selected_ferts.countSet() > 0) {
- selected_time = auto_pump_runtime[0];
- for (int i = 0; i < plants.countFertilizers(); i++) {
- if (auto_pump_runtime[i] > selected_time) {
- selected_time = auto_pump_runtime[i];
- }
-
- if (selected_ferts.isSet(i)) {
- plants.startFertilizer(i);
- }
- }
-
- start_time = millis();
- switch_to(fullauto_fert_run);
- } else {
- 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(fullauto_tank_run);
- } else if (wl == Plants::full) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_plant_run);
- }
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = F("Invalid sensor state");
- state = fullauto_fert;
- 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);
- }
- }
-
- #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
-
- // kickstart states
- if ((state == auto_plant_kickstart_run) || (state == fillnwater_kickstart_run)
- || (state == fullauto_kickstart_run) || (state == fullauto_kickstart_purge_run)) {
- unsigned long runtime = millis() - start_time;
- if ((runtime / 1000UL) >= KICKSTART_RUNTIME) {
- // kickstart is done, switch over to valves
- plants.abort();
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
-
- // start required valves
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, false);
- }
- }
-
- if (state == auto_plant_kickstart_run) {
- switch_to(auto_plant_run);
- } else if (state == fillnwater_kickstart_run) {
- switch_to(fillnwater_plant_run);
- } else if (state == fullauto_kickstart_run) {
- switch_to(fullauto_plant_run);
- } else {
- switch_to(fullauto_plant_purge_run);
- }
- } else if ((millis() - last_animation_time) >= 500) {
- // update animation if needed
- last_animation_time = millis();
- switch_to(state);
- }
- }
-
- if (state == fullauto_fert_run) {
- unsigned long runtime = millis() - start_time;
- for (int i = 0; i < plants.countFertilizers(); i++) {
- if (selected_ferts.isSet(i)) {
- if ((runtime / 1000UL) >= auto_pump_runtime[i]) {
- plants.stopFertilizer(i);
- selected_ferts.set(i, false);
- }
- }
- }
-
- if ((millis() - last_animation_time) >= 500) {
- // update animation if needed
- last_animation_time = millis();
- switch_to(state);
- }
-
- if (selected_ferts.countSet() <= 0) {
- plants.abort();
-
- 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(fullauto_tank_run);
- } else if (wl == Plants::full) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- // for recording the flowrate
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_plant_run);
- }
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- }
- }
-
- // states that fill the tank up
- if ((state == auto_fert_run) || (state == auto_tank_run)
- || (state == fullauto_tank_run) || (state == fullauto_tank_purge_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)
- || (state == fullauto_tank_run)
- || (state == fullauto_tank_purge_run)) {
- auto wl = plants.getWaterlevel();
- if ((wl != Plants::empty) && (wl != Plants::invalid)) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- if (state == fillnwater_tank_run) {
- switch_to(fillnwater_kickstart_run);
- } else if (state == fullauto_tank_run) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_kickstart_purge_run);
- }
- } else {
- if (state == fillnwater_tank_run) {
- switch_to(fillnwater_plant_run);
- } else if (state == fullauto_tank_run) {
- switch_to(fullauto_plant_run);
- } else {
- switch_to(fullauto_plant_purge_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)
- || (state == fullauto_tank_run)
- || (state == fullauto_tank_purge_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)
- wifi_write_database(time_to_fill, "calibrated_filling", -1);
- #endif // PLATFORM_ESP
- }
-
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- if (state == fillnwater_tank_run) {
- switch_to(fillnwater_kickstart_run);
- } else if (state == fullauto_tank_run) {
- switch_to(fullauto_kickstart_run);
- } else {
- switch_to(fullauto_kickstart_purge_run);
- }
- } else {
- if (state == fillnwater_tank_run) {
- switch_to(fillnwater_plant_run);
- } else if (state == fullauto_tank_run) {
- switch_to(fullauto_plant_run);
- } else {
- switch_to(fullauto_plant_purge_run);
- }
- }
- } 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);
- }
- }
-
- // states that empty the tank
- if ((state == auto_plant_run) || (state == fillnwater_plant_run)
- || (state == fullauto_plant_run) || (state == fullauto_plant_overrun)
- || (state == fullauto_plant_purge_run)
- || (state == fullauto_plant_purge_overrun)) {
- unsigned long runtime = millis() - start_time;
- if ((runtime / 1000UL) >= selected_time) {
- if (state == fullauto_plant_run) {
- start_time = millis();
- selected_time = OVERRUN_RUNTIME;
- switch_to(fullauto_plant_overrun);
- } else if (state == fullauto_plant_overrun) {
- plants.abort();
- stop_time = millis();
- 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 = PURGE_FILL_RUNTIME;
- start_time = millis();
- switch_to(fullauto_tank_purge_run);
- } else if (wl == Plants::full) {
- // check if kickstart is required for this
- bool need_kickstart = false;
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- if (plants.getKickstart()->getPinNumber(i) >= 0) {
- need_kickstart = true;
- }
- }
- }
-
- // start kickstart/valve as needed
- for (int i = 0; i < plants.countPlants(); i++) {
- if (selected_plants.isSet(i)) {
- plants.startPlant(i, need_kickstart);
- }
- }
-
- // for recording the flowrate
- watering_started_full = (wl == Plants::full);
-
- selected_time = MAX_AUTO_PLANT_RUNTIME;
- start_time = millis();
- if (need_kickstart) {
- switch_to(fullauto_kickstart_purge_run);
- } else {
- switch_to(fullauto_plant_purge_run);
- }
- } else if (wl == Plants::invalid) {
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- } else if (state == fullauto_plant_purge_run) {
- start_time = millis();
- selected_time = OVERRUN_RUNTIME;
- switch_to(fullauto_plant_purge_overrun);
- } else if (state == fullauto_plant_purge_overrun) {
- plants.abort();
- stop_time = millis();
- switch_to(fullauto_done);
- } else {
- 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);
- }
-
- if ((state == fullauto_plant_overrun) || (state == fullauto_plant_purge_overrun)) {
- // overrun should not exit when sensor returns empty
- return;
- }
-
- // check water level state
- auto wl = plants.getWaterlevel();
- if (wl == Plants::empty) {
- if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
- plants.abort();
- stop_time = millis();
- }
-
- if ((state == auto_plant_run) || (state == fillnwater_plant_run)
- || (state == fullauto_plant_run)) {
- // 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 (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)
- wifi_write_database(time_to_water, "calibrated_watering", selected_plants.getFirstSet() + 1);
- #endif // PLATFORM_ESP
- }
- }
-
- if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
- switch_to(auto_done);
- } else if (state == fullauto_plant_run) {
- start_time = millis();
- selected_time = OVERRUN_RUNTIME;
- switch_to(fullauto_plant_overrun);
- } else if (state == fullauto_plant_purge_run) {
- start_time = millis();
- selected_time = OVERRUN_RUNTIME;
- switch_to(fullauto_plant_purge_overrun);
-
- }
- } else if (wl == Plants::invalid) {
- plants.abort();
- error_condition = F("Invalid sensor state");
- state = auto_mode_a;
- switch_to(error);
- }
- }
-
- // timeout in user-input states
- 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)
- || (state == fullauto_fert) || (state == fullauto_plant)
- || (state == fullauto_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(" -"));
-
- #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
- String b = String(F("PIN: ")) + menu_entered_digits;
- print(a.c_str(),
- "* or # to enter menu",
- "Enter PIN for locks:",
- b.c_str(),
- 3);
- #else
- print(a.c_str(),
- "Usage: Enter number",
- "* Delete prev. digit",
- "# Execute input num.",
- -1);
- #endif
- } else if (s == door_select) {
- String a = String(F("(Input 1 to ")) + String(LOCK_COUNT) + String(F(")"));
- String b = String(F("Door: ")) + menu_entered_digits;
-
- print("- Select Door Lock -",
- "Leave empty if done!",
- a.c_str(),
- b.c_str(),
- 3);
- } 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",
- #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
- "6: Door Locks",
- #else
- "",
- #endif
- "#: 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/3 ----",
- "1: Full-Auto Mode",
- "2: Add Fertilizer",
- "#: Go to page 2/3...",
- -1);
- } else if (s == auto_mode_b) {
- print("---- Manual 2/3 ----",
- "3: Fill 'n' Water",
- "4: Fill Reservoir",
- "#: Go to page 3/3...",
- -1);
- } else if (s == auto_mode_c) {
- print("---- Manual 3/3 ----",
- "5: Water a plant",
- "",
- "#: Go to page 1/3...",
- -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) || (s == fullauto_fert_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("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) || (s == fullauto_stirr_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("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) || (s == fullauto_tank_run) || (s == fullauto_tank_purge_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("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) || (s == fullauto_plant)) {
- String a = String(F("(Input 1 to ")) + String(plants.countPlants()) + String(F(")"));
- 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 == fullauto_fert) {
- String a = String(F("(Input 1 to ")) + String(plants.countFertilizers()) + String(F(")"));
- String b = String(F("Fert.: ")) + menu_entered_digits;
-
- print("--- Select Fert. ---",
- "Leave empty if done!",
- a.c_str(),
- b.c_str(),
- 3);
- } else if ((s == auto_plant_kickstart_run) || (s == fillnwater_kickstart_run) || (s == fullauto_kickstart_run) || (s == fullauto_kickstart_purge_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("s / ")) + String(KICKSTART_RUNTIME) + String('s');
-
- unsigned long anim = runtime * 20UL / (KICKSTART_RUNTIME * 1000UL);
- String b;
- for (unsigned long i = 0; i < anim; i++) {
- b += '#';
- }
-
- print("---- Kick-Start ----",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if ((s == auto_plant_run) || (s == fillnwater_plant_run) || (s == fullauto_plant_run) || (s == fullauto_plant_purge_run)) {
- unsigned long runtime = millis() - start_time;
- String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("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 == fullauto_plant_overrun) || (s == fullauto_plant_purge_overrun)) {
- unsigned long runtime = millis() - start_time;
- String a = String(F("Time: ")) + String(runtime / 1000UL) + String(F("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("-- Emptying lines --",
- a.c_str(),
- b.c_str(),
- "Hit any key to stop!",
- -1);
- } else if ((s == auto_done) || (s == fullauto_done)) {
- String a = String(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("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)) {
- wifi_write_database(runtime / 1000, "plant", i + 1);
- }
- }
- } else if (old_state == auto_fert_run) {
- wifi_write_database(runtime / 1000, "fertilizer", selected_id);
- }
- #endif // PLATFORM_ESP
- } else if (s == menu_pumps) {
- String a = String(F("(Input 1 to ")) + String(plants.countFertilizers()) + String(F(")"));
- 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(F("------ Pump ")) + String(selected_id) + String(F(" ------"));
- 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(F("Pump No. ")) + String(selected_id);
- String b = String(F("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(F("Time: ")) + String(runtime / 1000UL) + String(F("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(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("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;
- wifi_write_database(runtime / 1000, "fertilizer", selected_id);
- #endif // PLATFORM_ESP
- } else if (s == menu_valves) {
- String a = String(F("(Input 1 to ")) + String(plants.countPlants() + 1) + String(F(")"));
- 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(F("----- Valve ")) + String(selected_id) + String(F(" -----"));
- 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(F("Valve No. ")) + String(selected_id);
- String b = String(F("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(F("Time: ")) + String(runtime / 1000UL) + String(F("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(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("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()) {
- wifi_write_database(runtime / 1000, "plant", selected_id);
- }
- #endif // PLATFORM_ESP
- } else if (s == menu_aux) {
- String a = String(F("(Input 1 to ")) + String(plants.countAux()) + String(F(")"));
- 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(F("------ Aux ")) + String(selected_id) + String(F(" ------"));
- 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(F("Aux No. ")) + String(selected_id);
- String b = String(F("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(F("Time: ")) + String(runtime / 1000UL) + String(F("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(F("after ")) + String((stop_time - start_time) / 1000UL) + String(F("s."));
-
- print("------- Done -------",
- "Aux. run 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);
- }
- }
-
- #endif
|