controller.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef CONTROLLER_H_
  2. #define CONTROLLER_H_
  3. #include "util.h"
  4. #include "datetime.h"
  5. /* The maximum possible number of flower-pots. */
  6. #define MAX_NR_FLOWERPOTS 6
  7. /* Flower-pot configuration flags. */
  8. enum flowerpot_config_flag {
  9. /* Pot is enabled.
  10. * If this bit is not set, the regulator is disabled.
  11. */
  12. POT_FLG_ENABLED = 0x01,
  13. /* Logging is enabled.
  14. * If this bit is not set, logs will not be written.
  15. */
  16. POT_FLG_LOG = 0x02,
  17. /* Verbose logging is enabled.
  18. * If this bit is not set, no verbose logs will be written.
  19. */
  20. POT_FLG_LOGVERBOSE = 0x04,
  21. };
  22. /* Configuration of one flower-pot. */
  23. struct flowerpot_config {
  24. /* Boolean flags. See 'enum flowerpot_config_flags'. */
  25. uint8_t flags;
  26. /* The lower threshold value for the regulator. */
  27. uint8_t min_threshold;
  28. /* The upper threshold value for the regulator. */
  29. uint8_t max_threshold;
  30. /* The time of day range where the regulator is active.
  31. * The regulator will be disabled outside of this time. */
  32. struct time_of_day_range active_range;
  33. /* Day-of-week ON mask. Bit 0 -> monday, Bit 1 -> tuesday, etc...
  34. * If a bit it set, the regulator will be enabled on
  35. * that weekday.
  36. */
  37. uint8_t dow_on_mask;
  38. };
  39. enum controller_global_flags {
  40. /* Global controller-enable bit.
  41. * If this bit is not set, the controller is disabled globally.
  42. */
  43. CONTR_FLG_ENABLE = 0x01,
  44. };
  45. /* Global controller configuration. */
  46. struct controller_global_config {
  47. /* Global configuration flags.
  48. * See 'enum controller_global_flags'
  49. */
  50. uint8_t flags;
  51. /* Global lowest possible value of the raw sensor values. */
  52. uint16_t sensor_lowest_value;
  53. /* Global highest possible value of the raw sensor values. */
  54. uint16_t sensor_highest_value;
  55. };
  56. /* Controller configuration. */
  57. struct controller_config {
  58. /* Per-pot configuration. */
  59. struct flowerpot_config pots[MAX_NR_FLOWERPOTS];
  60. /* Global config options. */
  61. struct controller_global_config global;
  62. };
  63. /* Flowerpot state-machine state-ID numbers. */
  64. enum flowerpot_state_id {
  65. /* POT_IDLE: The controller is waiting for the next
  66. * measurement to happen.
  67. */
  68. POT_IDLE = 0,
  69. /* POT_START_MEASUREMENT: The controller is going to start
  70. * a new measurement, as soon as the sensors
  71. * are available.
  72. */
  73. POT_START_MEASUREMENT,
  74. /* POT_MEASURING: The controller is performing a
  75. * sensor measurement.
  76. */
  77. POT_MEASURING,
  78. /* POT_WAITING_FOR_VALVE: The controller is waiting
  79. * for the last valve-action to finish.
  80. */
  81. POT_WAITING_FOR_VALVE,
  82. };
  83. /* The flowerpot state-machine state. */
  84. struct flowerpot_state {
  85. /* The current state-ID number. */
  86. enum flowerpot_state_id state_id;
  87. /* Are we currently watering? */
  88. bool is_watering;
  89. /* A copy of the last raw ADC sensor value. */
  90. uint16_t last_measured_raw_value;
  91. /* A copy of the last scaled sensor value. */
  92. uint8_t last_measured_value;
  93. };
  94. enum flowerpot_remanent_flags {
  95. /* The watering watchdog on this pot triggered.
  96. * The regulator will be disabled.
  97. */
  98. POT_REMFLG_WDTRIGGER = 0x01,
  99. };
  100. /* The flowerpot state-machine remanent state. */
  101. struct flowerpot_remanent_state {
  102. /* Remanent state flags bitfield. */
  103. uint8_t flags;
  104. };
  105. void controller_get_config(struct controller_config *dest);
  106. void controller_update_config(const struct controller_config *src);
  107. void controller_get_pot_state(uint8_t pot_number,
  108. struct flowerpot_state *state,
  109. struct flowerpot_remanent_state *rem_state);
  110. void controller_update_pot_rem_state(uint8_t pot_number,
  111. const struct flowerpot_remanent_state *rem_state);
  112. void controller_manual_mode(uint8_t force_stop_watering_mask,
  113. uint8_t valve_manual_mask,
  114. uint8_t valve_manual_state,
  115. uint8_t force_start_measurement_mask);
  116. void controller_freeze(bool freeze);
  117. void controller_work(void);
  118. void controller_init(void);
  119. #endif /* CONTROLLER_H_ */