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.h 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
  3. *
  4. * This file is part of Giess-o-mat.
  5. *
  6. * Giess-o-mat is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Giess-o-mat is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _STATEMACHINE_H_
  20. #define _STATEMACHINE_H_
  21. #include <Arduino.h>
  22. #include "BoolField.h"
  23. #define stringify( name ) # name
  24. class Statemachine {
  25. public:
  26. enum States {
  27. init = 0,
  28. menu, // auto, pumps, valves
  29. auto_mode, // select mode
  30. auto_fert, // select fertilizer
  31. auto_fert_run,
  32. auto_tank_run,
  33. auto_plant, // select plant
  34. auto_plant_run,
  35. auto_done,
  36. menu_pumps, // selet pump
  37. menu_pumps_time, // set runtime
  38. menu_pumps_go, // running
  39. menu_pumps_run,
  40. menu_pumps_done,
  41. menu_valves, // select valve
  42. menu_valves_time, // set runtime
  43. menu_valves_go, // running
  44. menu_valves_run,
  45. menu_valves_done,
  46. error
  47. };
  48. class DigitBuffer {
  49. public:
  50. DigitBuffer(int _size);
  51. ~DigitBuffer();
  52. bool spaceLeft(void);
  53. bool hasDigits(void);
  54. int countDigits(void);
  55. void addDigit(int d);
  56. void removeDigit(void);
  57. void clear(void);
  58. uint32_t getNumber(void);
  59. private:
  60. int size;
  61. int pos;
  62. int *digits;
  63. };
  64. typedef void (*print_fn)(const char *, const char *, const char *, const char *, int);
  65. typedef void (*backspace_fn)(void);
  66. Statemachine(print_fn _print, backspace_fn _backspace);
  67. void begin(void);
  68. void input(int n);
  69. void act(void);
  70. const char *getStateName(void);
  71. private:
  72. void switch_to(States s);
  73. uint32_t number_input(void);
  74. DigitBuffer db;
  75. States state, old_state;
  76. print_fn print;
  77. backspace_fn backspace;
  78. BoolField selected_plants;
  79. uint32_t selected_id; // pump or valve id
  80. uint32_t selected_time; // runtime
  81. unsigned long start_time, stop_time, last_animation_time;
  82. String error_condition;
  83. };
  84. #endif // _STATEMACHINE_H_