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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #define stringify( name ) # name
  23. class Statemachine {
  24. public:
  25. enum States {
  26. init = 0,
  27. menu, // auto, pumps, valves
  28. auto_mode, // select mode
  29. auto_fert, // select fertilizer
  30. auto_fert_run,
  31. auto_tank_run,
  32. auto_plant, // select plant
  33. auto_plant_run,
  34. auto_done,
  35. menu_pumps, // selet pump
  36. menu_pumps_time, // set runtime
  37. menu_pumps_go, // running
  38. menu_pumps_run,
  39. menu_pumps_done,
  40. menu_valves, // select valve
  41. menu_valves_time, // set runtime
  42. menu_valves_go, // running
  43. menu_valves_run,
  44. menu_valves_done,
  45. error
  46. };
  47. class DigitBuffer {
  48. public:
  49. DigitBuffer(int _size);
  50. ~DigitBuffer();
  51. bool spaceLeft(void);
  52. bool hasDigits(void);
  53. int countDigits(void);
  54. void addDigit(int d);
  55. void removeDigit(void);
  56. void clear(void);
  57. uint32_t getNumber(void);
  58. private:
  59. int size;
  60. int pos;
  61. int *digits;
  62. };
  63. typedef void (*print_fn)(const char *, const char *, const char *, const char *, int);
  64. typedef void (*backspace_fn)(void);
  65. Statemachine(print_fn _print, backspace_fn _backspace);
  66. void begin(void);
  67. void input(int n);
  68. void act(void);
  69. const char *getStateName(void);
  70. private:
  71. void switch_to(States s);
  72. uint32_t number_input(void);
  73. DigitBuffer db;
  74. States state, old_state;
  75. print_fn print;
  76. backspace_fn backspace;
  77. uint32_t selected_id; // pump or valve id
  78. uint32_t selected_time; // runtime
  79. unsigned long start_time, stop_time, last_animation_time;
  80. String error_condition;
  81. };
  82. #endif // _STATEMACHINE_H_