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

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