1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942 |
- /*
- * 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 == 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_stirr_run)
- || (state == fullauto_fert_run)
- || (state == fullauto_tank_run)
- || (state == fullauto_kickstart_run)
- || (state == fullauto_plant_run)
- || (state == fullauto_plant_overrun)
- || (state == fullauto_tank_purge_run)
- || (state == fullauto_kickstart_purge_run)
- || (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;
- }
-
- // 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();
- 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 (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)
- 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
- }
-
- // 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) {
- 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);
- }
- }
-
- // 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)) {
- 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(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;
- 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(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()) {
- 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(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
|