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

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