DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Statemachine.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #include "Plants.h"
  2. #include "DebugLog.h"
  3. #include "Statemachine.h"
  4. #include "config.h"
  5. Statemachine::DigitBuffer::DigitBuffer(int _size) {
  6. size = _size;
  7. pos = 0;
  8. digits = new int[size];
  9. }
  10. Statemachine::DigitBuffer::~DigitBuffer() {
  11. delete digits;
  12. }
  13. bool Statemachine::DigitBuffer::spaceLeft(void) {
  14. return (pos < size);
  15. }
  16. bool Statemachine::DigitBuffer::hasDigits(void) {
  17. return (pos > 0);
  18. }
  19. int Statemachine::DigitBuffer::countDigits(void) {
  20. return pos;
  21. }
  22. void Statemachine::DigitBuffer::addDigit(int d) {
  23. if (spaceLeft()) {
  24. digits[pos] = d;
  25. pos++;
  26. }
  27. }
  28. void Statemachine::DigitBuffer::removeDigit(void) {
  29. if (hasDigits()) {
  30. pos--;
  31. }
  32. }
  33. void Statemachine::DigitBuffer::clear(void) {
  34. pos = 0;
  35. }
  36. uint32_t Statemachine::DigitBuffer::getNumber(void) {
  37. uint32_t fact = 1;
  38. uint32_t sum = 0;
  39. for (int i = (pos - 1); i >= 0; i--) {
  40. sum += digits[i] * fact;
  41. fact *= 10;
  42. }
  43. return sum;
  44. }
  45. static const char *state_names[] = {
  46. stringify(init),
  47. stringify(menu),
  48. stringify(auto_mode),
  49. stringify(auto_fert),
  50. stringify(auto_fert_run),
  51. stringify(auto_tank_run),
  52. stringify(auto_plant),
  53. stringify(auto_plant_run),
  54. stringify(auto_done),
  55. stringify(menu_pumps),
  56. stringify(menu_pumps_time),
  57. stringify(menu_pumps_go),
  58. stringify(menu_pumps_run),
  59. stringify(menu_pumps_done),
  60. stringify(menu_valves),
  61. stringify(menu_valves_time),
  62. stringify(menu_valves_go),
  63. stringify(menu_valves_run),
  64. stringify(menu_valves_done),
  65. stringify(error)
  66. };
  67. const char *Statemachine::getStateName(void) {
  68. return state_names[state];
  69. }
  70. Statemachine::Statemachine(print_fn _print, backspace_fn _backspace)
  71. : db(7) {
  72. state = init;
  73. old_state = init;
  74. print = _print;
  75. backspace = _backspace;
  76. selected_id = 0;
  77. selected_time = 0;
  78. start_time = 0;
  79. stop_time = 0;
  80. last_animation_time = 0;
  81. error_condition = "";
  82. }
  83. void Statemachine::begin(void) {
  84. switch_to(init);
  85. }
  86. void Statemachine::input(int n) {
  87. if (state == init) {
  88. switch_to(menu);
  89. } else if (state == menu) {
  90. if (n == 1) {
  91. switch_to(auto_mode);
  92. } else if (n == 2) {
  93. switch_to(menu_pumps);
  94. } else if (n == 3) {
  95. switch_to(menu_valves);
  96. } else if ((n == -1) || (n == -2)) {
  97. switch_to(init);
  98. }
  99. } else if (state == auto_mode) {
  100. if (n == 1) {
  101. switch_to(auto_fert);
  102. } else if (n == 2) {
  103. auto wl = plants.getWaterlevel();
  104. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  105. plants.openWaterInlet();
  106. selected_id = plants.countPlants() + 1;
  107. selected_time = MAX_TANK_FILL_TIME;
  108. start_time = millis();
  109. switch_to(auto_tank_run);
  110. } else if (wl == Plants::full) {
  111. stop_time = millis();
  112. switch_to(auto_mode);
  113. } else if (wl == Plants::invalid) {
  114. error_condition = "Invalid sensor state";
  115. state = auto_mode;
  116. switch_to(error);
  117. }
  118. } else if (n == 3) {
  119. switch_to(auto_plant);
  120. } else if ((n == -1) || (n == -2)) {
  121. switch_to(menu);
  122. }
  123. } else if (state == auto_fert) {
  124. if ((n >= 1) && (n <= 3)) {
  125. auto wl = plants.getWaterlevel();
  126. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  127. plants.startFertilizer(n - 1);
  128. selected_id = n;
  129. selected_time = AUTO_PUMP_RUNTIME;
  130. start_time = millis();
  131. switch_to(auto_fert_run);
  132. } else if (wl == Plants::full) {
  133. stop_time = millis();
  134. switch_to(auto_mode);
  135. } else if (wl == Plants::invalid) {
  136. error_condition = "Invalid sensor state";
  137. state = auto_mode;
  138. switch_to(error);
  139. }
  140. } else if ((n == -1) || (n == -2)) {
  141. switch_to(auto_mode);
  142. }
  143. } else if (state == auto_fert_run) {
  144. plants.abort();
  145. stop_time = millis();
  146. switch_to(auto_done);
  147. } else if (state == auto_tank_run) {
  148. plants.abort();
  149. stop_time = millis();
  150. switch_to(auto_done);
  151. } else if (state == auto_plant) {
  152. if (n == -1) {
  153. if (db.hasDigits()) {
  154. backspace();
  155. db.removeDigit();
  156. } else {
  157. switch_to(auto_mode);
  158. }
  159. } else if (n == -2) {
  160. if (!db.hasDigits()) {
  161. return;
  162. }
  163. selected_id = number_input();
  164. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  165. error_condition = "Invalid plant ID!";
  166. switch_to(error);
  167. } else {
  168. auto wl = plants.getWaterlevel();
  169. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  170. plants.startPlant(selected_id - 1);
  171. selected_time = MAX_AUTO_PLANT_RUNTIME;
  172. start_time = millis();
  173. switch_to(auto_plant_run);
  174. } else if (wl == Plants::empty) {
  175. stop_time = millis();
  176. switch_to(auto_mode);
  177. } else if (wl == Plants::invalid) {
  178. error_condition = "Invalid sensor state";
  179. state = auto_mode;
  180. switch_to(error);
  181. }
  182. }
  183. } else {
  184. if (db.spaceLeft()) {
  185. db.addDigit(n);
  186. } else {
  187. backspace();
  188. }
  189. }
  190. } else if (state == auto_plant_run) {
  191. plants.abort();
  192. stop_time = millis();
  193. switch_to(auto_done);
  194. } else if (state == auto_done) {
  195. switch_to(auto_mode);
  196. } else if (state == menu_pumps) {
  197. if (n == -1) {
  198. if (db.hasDigits()) {
  199. backspace();
  200. db.removeDigit();
  201. } else {
  202. switch_to(menu);
  203. }
  204. } else if (n == -2) {
  205. if (!db.hasDigits()) {
  206. return;
  207. }
  208. selected_id = number_input();
  209. if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
  210. error_condition = "Invalid pump ID!";
  211. switch_to(error);
  212. } else {
  213. switch_to(menu_pumps_time);
  214. }
  215. } else {
  216. if (db.spaceLeft()) {
  217. db.addDigit(n);
  218. } else {
  219. backspace();
  220. }
  221. }
  222. } else if (state == menu_pumps_time) {
  223. if (n == -1) {
  224. if (db.hasDigits()) {
  225. backspace();
  226. db.removeDigit();
  227. } else {
  228. switch_to(menu_pumps);
  229. }
  230. } else if (n == -2) {
  231. if (!db.hasDigits()) {
  232. return;
  233. }
  234. selected_time = number_input();
  235. if ((selected_time <= 0) || (selected_time > 120)) {
  236. error_condition = "Invalid time range!";
  237. switch_to(error);
  238. } else {
  239. switch_to(menu_pumps_go);
  240. }
  241. } else {
  242. if (db.spaceLeft()) {
  243. db.addDigit(n);
  244. } else {
  245. backspace();
  246. }
  247. }
  248. } else if (state == menu_pumps_go) {
  249. if (n == -2) {
  250. start_time = millis();
  251. last_animation_time = start_time;
  252. auto wl = plants.getWaterlevel();
  253. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  254. plants.startFertilizer(selected_id - 1);
  255. switch_to(menu_pumps_run);
  256. } else if (wl == Plants::full) {
  257. stop_time = millis();
  258. switch_to(menu_pumps_done);
  259. } else if (wl == Plants::invalid) {
  260. error_condition = "Invalid sensor state";
  261. state = menu_pumps;
  262. switch_to(error);
  263. }
  264. } else {
  265. switch_to(menu_pumps_time);
  266. }
  267. } else if (state == menu_pumps_run) {
  268. plants.abort();
  269. stop_time = millis();
  270. switch_to(menu_pumps_done);
  271. } else if (state == menu_pumps_done) {
  272. switch_to(menu);
  273. } else if (state == menu_valves) {
  274. if (n == -1) {
  275. if (db.hasDigits()) {
  276. backspace();
  277. db.removeDigit();
  278. } else {
  279. switch_to(menu);
  280. }
  281. } else if (n == -2) {
  282. if (!db.hasDigits()) {
  283. return;
  284. }
  285. selected_id = number_input();
  286. if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
  287. error_condition = "Invalid valve ID!";
  288. switch_to(error);
  289. } else {
  290. switch_to(menu_valves_time);
  291. }
  292. } else {
  293. if (db.spaceLeft()) {
  294. db.addDigit(n);
  295. } else {
  296. backspace();
  297. }
  298. }
  299. } else if (state == menu_valves_time) {
  300. if (n == -1) {
  301. if (db.hasDigits()) {
  302. backspace();
  303. db.removeDigit();
  304. } else {
  305. switch_to(menu_valves);
  306. }
  307. } else if (n == -2) {
  308. if (!db.hasDigits()) {
  309. return;
  310. }
  311. selected_time = number_input();
  312. if ((selected_time <= 0) || (selected_time > 120)) {
  313. error_condition = "Invalid time range!";
  314. switch_to(error);
  315. } else {
  316. switch_to(menu_valves_go);
  317. }
  318. } else {
  319. if (db.spaceLeft()) {
  320. db.addDigit(n);
  321. } else {
  322. backspace();
  323. }
  324. }
  325. } else if (state == menu_valves_go) {
  326. if (n == -2) {
  327. start_time = millis();
  328. last_animation_time = start_time;
  329. auto wl = plants.getWaterlevel();
  330. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  331. if (selected_id >= (plants.countPlants() + 1)) {
  332. plants.openWaterInlet();
  333. } else {
  334. plants.startPlant(selected_id - 1);
  335. }
  336. switch_to(menu_valves_run);
  337. } else if (wl == Plants::full) {
  338. stop_time = millis();
  339. switch_to(menu_valves_done);
  340. } else if (wl == Plants::invalid) {
  341. error_condition = "Invalid sensor state";
  342. state = menu_valves;
  343. switch_to(error);
  344. }
  345. } else {
  346. switch_to(menu_valves_time);
  347. }
  348. } else if (state == menu_valves_run) {
  349. plants.abort();
  350. stop_time = millis();
  351. switch_to(menu_valves_done);
  352. } else if (state == menu_valves_done) {
  353. switch_to(menu);
  354. } else if (state == error) {
  355. if (old_state != error) {
  356. switch_to(old_state);
  357. } else {
  358. switch_to(menu);
  359. }
  360. }
  361. }
  362. uint32_t Statemachine::number_input(void) {
  363. for (int i = 0; i < db.countDigits(); i++) {
  364. backspace();
  365. }
  366. uint32_t n = db.getNumber();
  367. db.clear();
  368. debug.print("Whole number input: ");
  369. debug.println(n);
  370. return n;
  371. }
  372. void Statemachine::act(void) {
  373. if ((state == menu_pumps_run) || (state == menu_valves_run)) {
  374. unsigned long runtime = millis() - start_time;
  375. if ((runtime / 1000UL) >= selected_time) {
  376. // stop if timeout has been reached
  377. plants.abort();
  378. stop_time = millis();
  379. switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
  380. } else if ((millis() - last_animation_time) >= 500) {
  381. // update animation if needed
  382. last_animation_time = millis();
  383. switch_to(state);
  384. }
  385. }
  386. if ((state == menu_pumps_run) || ((state == menu_valves_run) && (selected_id == (plants.countPlants() + 1)))) {
  387. // check water level state
  388. auto wl = plants.getWaterlevel();
  389. if (wl == Plants::full) {
  390. plants.abort();
  391. stop_time = millis();
  392. switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
  393. } else if (wl == Plants::invalid) {
  394. plants.abort();
  395. error_condition = "Invalid sensor state";
  396. state = (state == menu_pumps_run) ? menu_pumps : menu_valves;
  397. switch_to(error);
  398. }
  399. }
  400. if ((state == auto_fert_run) || (state == auto_tank_run)) {
  401. unsigned long runtime = millis() - start_time;
  402. if ((runtime / 1000UL) >= selected_time) {
  403. // stop if timeout has been reached
  404. plants.abort();
  405. stop_time = millis();
  406. switch_to(auto_done);
  407. } else if ((millis() - last_animation_time) >= 500) {
  408. // update animation if needed
  409. last_animation_time = millis();
  410. switch_to(state);
  411. }
  412. // check water level state
  413. auto wl = plants.getWaterlevel();
  414. if (wl == Plants::full) {
  415. plants.abort();
  416. stop_time = millis();
  417. switch_to(auto_done);
  418. } else if (wl == Plants::invalid) {
  419. plants.abort();
  420. error_condition = "Invalid sensor state";
  421. state = auto_mode;
  422. switch_to(error);
  423. }
  424. }
  425. if (state == auto_plant_run) {
  426. unsigned long runtime = millis() - start_time;
  427. if ((runtime / 1000UL) >= selected_time) {
  428. // stop if timeout has been reached
  429. plants.abort();
  430. stop_time = millis();
  431. switch_to(auto_done);
  432. } else if ((millis() - last_animation_time) >= 500) {
  433. // update animation if needed
  434. last_animation_time = millis();
  435. switch_to(state);
  436. }
  437. // check water level state
  438. auto wl = plants.getWaterlevel();
  439. if (wl == Plants::empty) {
  440. plants.abort();
  441. stop_time = millis();
  442. switch_to(auto_done);
  443. } else if (wl == Plants::invalid) {
  444. plants.abort();
  445. error_condition = "Invalid sensor state";
  446. state = auto_mode;
  447. switch_to(error);
  448. }
  449. }
  450. }
  451. void Statemachine::switch_to(States s) {
  452. old_state = state;
  453. state = s;
  454. if (s == init) {
  455. String a = String("- Giess-o-mat V") + FIRMWARE_VERSION + String(" -");
  456. print(a.c_str(),
  457. "Usage: Enter number",
  458. "* Delete prev. digit",
  459. "# Execute input num.",
  460. -1);
  461. } else if (s == menu) {
  462. print("------- Menu -------",
  463. "1: Automatic program",
  464. "2: Fertilizer pumps",
  465. "3: Outlet valves",
  466. -1);
  467. } else if (s == auto_mode) {
  468. print("------- Auto -------",
  469. "1: Add Fertilizer",
  470. "2: Fill Reservoir",
  471. "3: Water a plant",
  472. -1);
  473. } else if (s == auto_fert) {
  474. print("---- Fertilizer ----",
  475. "1: Vegetation Phase",
  476. "2: Bloom Phase",
  477. "3: Special",
  478. -1);
  479. } else if (s == auto_fert_run) {
  480. unsigned long runtime = millis() - start_time;
  481. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  482. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  483. String b;
  484. for (unsigned long i = 0; i < anim; i++) {
  485. b += '#';
  486. }
  487. print("---- Dispensing ----",
  488. a.c_str(),
  489. b.c_str(),
  490. "Hit any key to stop!",
  491. -1);
  492. } else if (s == auto_tank_run) {
  493. unsigned long runtime = millis() - start_time;
  494. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  495. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  496. String b;
  497. for (unsigned long i = 0; i < anim; i++) {
  498. b += '#';
  499. }
  500. print("--- Filling Tank ---",
  501. a.c_str(),
  502. b.c_str(),
  503. "Hit any key to stop!",
  504. -1);
  505. } else if (s == auto_plant) {
  506. String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
  507. print("--- Select Plant ---",
  508. "Which plant to water",
  509. a.c_str(),
  510. "Plant: ",
  511. 3);
  512. } else if (s == auto_plant_run) {
  513. unsigned long runtime = millis() - start_time;
  514. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  515. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  516. String b;
  517. for (unsigned long i = 0; i < anim; i++) {
  518. b += '#';
  519. }
  520. print("----- Watering -----",
  521. a.c_str(),
  522. b.c_str(),
  523. "Hit any key to stop!",
  524. -1);
  525. } else if (s == auto_done) {
  526. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  527. print("------- Done -------",
  528. "Dispensing finished",
  529. a.c_str(),
  530. "Hit any key for menu",
  531. -1);
  532. } else if (s == menu_pumps) {
  533. String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
  534. print("------- Pump -------",
  535. "Please select pump",
  536. a.c_str(),
  537. "Pump: ",
  538. 3);
  539. } else if (s == menu_pumps_time) {
  540. String header = String("------ Pump ") + String(selected_id) + String(" ------");
  541. print(header.c_str(),
  542. "Please set runtime",
  543. "(Input in seconds)",
  544. "Runtime: ",
  545. 3);
  546. } else if (s == menu_pumps_go) {
  547. String a = String("Pump No. ") + String(selected_id);
  548. String b = String("Runtime ") + String(selected_time) + String('s');
  549. print("----- Confirm? -----",
  550. a.c_str(),
  551. b.c_str(),
  552. " # Confirm",
  553. -1);
  554. } else if (s == menu_pumps_run) {
  555. unsigned long runtime = millis() - start_time;
  556. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  557. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  558. String b;
  559. for (unsigned long i = 0; i < anim; i++) {
  560. b += '#';
  561. }
  562. print("---- Dispensing ----",
  563. a.c_str(),
  564. b.c_str(),
  565. "Hit any key to stop!",
  566. -1);
  567. } else if (s == menu_pumps_done) {
  568. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  569. print("------- Done -------",
  570. "Dispensing finished",
  571. a.c_str(),
  572. "Hit any key for menu",
  573. -1);
  574. } else if (s == menu_valves) {
  575. String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
  576. print("------ Valves ------",
  577. "Please select valve",
  578. a.c_str(),
  579. "Valve: ",
  580. 3);
  581. } else if (s == menu_valves_time) {
  582. String header = String("----- Valve ") + String(selected_id) + String(" -----");
  583. print(header.c_str(),
  584. "Please set runtime",
  585. "(Input in seconds)",
  586. "Runtime: ",
  587. 3);
  588. } else if (s == menu_valves_go) {
  589. String a = String("Valve No. ") + String(selected_id);
  590. String b = String("Runtime ") + String(selected_time) + String('s');
  591. print("----- Confirm? -----",
  592. a.c_str(),
  593. b.c_str(),
  594. " # Confirm",
  595. -1);
  596. } else if (s == menu_valves_run) {
  597. unsigned long runtime = millis() - start_time;
  598. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  599. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  600. String b;
  601. for (unsigned long i = 0; i <= anim; i++) {
  602. b += '#';
  603. }
  604. print("---- Dispensing ----",
  605. a.c_str(),
  606. b.c_str(),
  607. "Hit any key to stop!",
  608. -1);
  609. } else if (s == menu_valves_done) {
  610. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  611. print("------- Done -------",
  612. "Dispensing finished",
  613. a.c_str(),
  614. "Hit any key for menu",
  615. -1);
  616. } else if (s == error) {
  617. print("------ Error! ------",
  618. "There is a problem:",
  619. error_condition.c_str(),
  620. " Press any key...",
  621. -1);
  622. }
  623. }