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.

state.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.h"
  31. #define stringify(name) # name
  32. struct state {
  33. const char * const name;
  34. void (*enter)(void);
  35. void (*exit)(void);
  36. void (*run)(void);
  37. };
  38. static const struct state states[STATE_INVALID + 1] = {
  39. {
  40. .name = stringify(STATE_INIT),
  41. .enter = NULL,
  42. .exit = NULL,
  43. .run = NULL,
  44. }, {
  45. .name = stringify(STATE_SCAN),
  46. .enter = state_scan_enter,
  47. .exit = state_scan_exit,
  48. .run = state_scan_run,
  49. }, {
  50. .name = stringify(STATE_WORKFLOW),
  51. .enter = state_wf_enter,
  52. .exit = state_wf_exit,
  53. .run = state_wf_run,
  54. }, {
  55. .name = stringify(STATE_VOLCANO_RUN),
  56. .enter = state_volcano_run_enter,
  57. .exit = state_volcano_run_exit,
  58. .run = state_volcano_run_run,
  59. }, {
  60. .name = stringify(STATE_CRAFTY),
  61. .enter = state_crafty_enter,
  62. .exit = state_crafty_exit,
  63. .run = state_crafty_run,
  64. }, {
  65. .name = stringify(STATE_EDIT_WORKFLOW),
  66. .enter = state_edit_wf_enter,
  67. .exit = state_edit_wf_exit,
  68. .run = state_edit_wf_run,
  69. }, {
  70. .name = stringify(STATE_SETTINGS),
  71. .enter = state_settings_enter,
  72. .exit = state_settings_exit,
  73. .run = state_settings_run,
  74. }, {
  75. .name = stringify(STATE_ABOUT),
  76. .enter = state_about_enter,
  77. .exit = state_about_exit,
  78. .run = state_about_run,
  79. }, {
  80. .name = stringify(STATE_VALUE),
  81. .enter = state_value_enter,
  82. .exit = state_value_exit,
  83. .run = state_value_run,
  84. }, {
  85. .name = stringify(STATE_VOLCANO_CONF),
  86. .enter = state_volcano_conf_enter,
  87. .exit = state_volcano_conf_exit,
  88. .run = state_volcano_conf_run,
  89. }, {
  90. .name = stringify(STATE_VENTY),
  91. .enter = state_venty_enter,
  92. .exit = state_venty_exit,
  93. .run = state_venty_run,
  94. }, {
  95. .name = stringify(STATE_INVALID),
  96. .enter = NULL,
  97. .exit = NULL,
  98. .run = NULL,
  99. }
  100. };
  101. static enum system_state state = STATE_INIT;
  102. void state_switch(enum system_state next) {
  103. if (state == next) {
  104. return;
  105. }
  106. if (next > STATE_INVALID) {
  107. debug("invalid new state %d", next);
  108. next = STATE_INVALID;
  109. }
  110. debug("leaving %s", states[state].name);
  111. if (states[state].exit) {
  112. states[state].exit();
  113. }
  114. debug("entering %s", states[next].name);
  115. if (states[next].enter) {
  116. states[next].enter();
  117. }
  118. state = next;
  119. }
  120. void state_run(void) {
  121. if (state >= STATE_INVALID) {
  122. debug("invalid main state %d", state);
  123. state_switch(STATE_SCAN);
  124. }
  125. if (states[state].run) {
  126. states[state].run();
  127. }
  128. }