S&B Volcano vaporizer remote control with Pi Pico W
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * state.c
  3. *
  4. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program 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. * This program 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. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include "log.h"
  20. #include "state_scan.h"
  21. #include "state_workflow.h"
  22. #include "state_volcano_run.h"
  23. #include "state_crafty.h"
  24. #include "state_edit_workflow.h"
  25. #include "state_settings.h"
  26. #include "state_about.h"
  27. #include "state_value.h"
  28. #include "state_volcano_conf.h"
  29. #include "state_venty.h"
  30. #include "state_wifi.h"
  31. #include "state_wifi_edit.h"
  32. #include "state_string.h"
  33. #include "state.h"
  34. #define stringify(name) # name
  35. struct state {
  36. const char * const name;
  37. void (*enter)(void);
  38. void (*exit)(void);
  39. void (*run)(void);
  40. };
  41. static const struct state states[STATE_INVALID + 1] = {
  42. {
  43. .name = stringify(STATE_INIT),
  44. .enter = NULL,
  45. .exit = NULL,
  46. .run = NULL,
  47. }, {
  48. .name = stringify(STATE_SCAN),
  49. .enter = state_scan_enter,
  50. .exit = state_scan_exit,
  51. .run = state_scan_run,
  52. }, {
  53. .name = stringify(STATE_WORKFLOW),
  54. .enter = state_wf_enter,
  55. .exit = state_wf_exit,
  56. .run = state_wf_run,
  57. }, {
  58. .name = stringify(STATE_VOLCANO_RUN),
  59. .enter = state_volcano_run_enter,
  60. .exit = state_volcano_run_exit,
  61. .run = state_volcano_run_run,
  62. }, {
  63. .name = stringify(STATE_CRAFTY),
  64. .enter = state_crafty_enter,
  65. .exit = state_crafty_exit,
  66. .run = state_crafty_run,
  67. }, {
  68. .name = stringify(STATE_EDIT_WORKFLOW),
  69. .enter = state_edit_wf_enter,
  70. .exit = state_edit_wf_exit,
  71. .run = state_edit_wf_run,
  72. }, {
  73. .name = stringify(STATE_SETTINGS),
  74. .enter = state_settings_enter,
  75. .exit = state_settings_exit,
  76. .run = state_settings_run,
  77. }, {
  78. .name = stringify(STATE_ABOUT),
  79. .enter = state_about_enter,
  80. .exit = state_about_exit,
  81. .run = state_about_run,
  82. }, {
  83. .name = stringify(STATE_VALUE),
  84. .enter = state_value_enter,
  85. .exit = state_value_exit,
  86. .run = state_value_run,
  87. }, {
  88. .name = stringify(STATE_VOLCANO_CONF),
  89. .enter = state_volcano_conf_enter,
  90. .exit = state_volcano_conf_exit,
  91. .run = state_volcano_conf_run,
  92. }, {
  93. .name = stringify(STATE_VENTY),
  94. .enter = state_venty_enter,
  95. .exit = state_venty_exit,
  96. .run = state_venty_run,
  97. }, {
  98. .name = stringify(STATE_WIFI_NETS),
  99. .enter = state_wifi_enter,
  100. .exit = state_wifi_exit,
  101. .run = state_wifi_run,
  102. }, {
  103. .name = stringify(STATE_WIFI_EDIT),
  104. .enter = state_wifi_edit_enter,
  105. .exit = state_wifi_edit_exit,
  106. .run = state_wifi_edit_run,
  107. }, {
  108. .name = stringify(STATE_STRING),
  109. .enter = state_string_enter,
  110. .exit = state_string_exit,
  111. .run = state_string_run,
  112. }, {
  113. .name = stringify(STATE_INVALID),
  114. .enter = NULL,
  115. .exit = NULL,
  116. .run = NULL,
  117. }
  118. };
  119. static enum system_state state = STATE_INIT;
  120. void state_switch(enum system_state next) {
  121. if (state == next) {
  122. return;
  123. }
  124. if (next > STATE_INVALID) {
  125. debug("invalid new state %d", next);
  126. next = STATE_INVALID;
  127. }
  128. debug("leaving %s", states[state].name);
  129. if (states[state].exit) {
  130. states[state].exit();
  131. }
  132. debug("entering %s", states[next].name);
  133. if (states[next].enter) {
  134. states[next].enter();
  135. }
  136. state = next;
  137. }
  138. void state_run(void) {
  139. if (state >= STATE_INVALID) {
  140. debug("invalid main state %d", state);
  141. state_switch(STATE_SCAN);
  142. }
  143. if (states[state].run) {
  144. states[state].run();
  145. }
  146. }