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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_volcano_workflow.h"
  22. #include "state_volcano_run.h"
  23. #include "state_crafty.h"
  24. #include "state_edit_workflow.h"
  25. #include "state.h"
  26. #define stringify(name) # name
  27. struct state {
  28. const char * const name;
  29. void (*enter)(void);
  30. void (*exit)(void);
  31. void (*run)(void);
  32. };
  33. static const struct state states[STATE_INVALID + 1] = {
  34. {
  35. .name = stringify(STATE_INIT),
  36. .enter = NULL,
  37. .exit = NULL,
  38. .run = NULL,
  39. }, {
  40. .name = stringify(STATE_SCAN),
  41. .enter = state_scan_enter,
  42. .exit = state_scan_exit,
  43. .run = state_scan_run,
  44. }, {
  45. .name = stringify(STATE_VOLCANO_WORKFLOW),
  46. .enter = state_volcano_wf_enter,
  47. .exit = state_volcano_wf_exit,
  48. .run = state_volcano_wf_run,
  49. }, {
  50. .name = stringify(STATE_VOLCANO_RUN),
  51. .enter = state_volcano_run_enter,
  52. .exit = state_volcano_run_exit,
  53. .run = state_volcano_run_run,
  54. }, {
  55. .name = stringify(STATE_CRAFTY),
  56. .enter = state_crafty_enter,
  57. .exit = state_crafty_exit,
  58. .run = state_crafty_run,
  59. }, {
  60. .name = stringify(STATE_EDIT_WORKFLOW),
  61. .enter = state_edit_wf_enter,
  62. .exit = state_edit_wf_exit,
  63. .run = state_edit_wf_run,
  64. }, {
  65. .name = stringify(STATE_INVALID),
  66. .enter = NULL,
  67. .exit = NULL,
  68. .run = NULL,
  69. }
  70. };
  71. static enum system_state state = STATE_INIT;
  72. void state_switch(enum system_state next) {
  73. if (state == next) {
  74. return;
  75. }
  76. if (next > STATE_INVALID) {
  77. debug("invalid new state %d", next);
  78. next = STATE_INVALID;
  79. }
  80. debug("leaving %s", states[state].name);
  81. if (states[state].exit) {
  82. states[state].exit();
  83. }
  84. debug("entering %s", states[next].name);
  85. if (states[next].enter) {
  86. states[next].enter();
  87. }
  88. state = next;
  89. }
  90. void state_run(void) {
  91. if (state >= STATE_INVALID) {
  92. debug("invalid main state %d", state);
  93. state_switch(STATE_SCAN);
  94. }
  95. if (states[state].run) {
  96. states[state].run();
  97. }
  98. }