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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. static enum system_state state = STATE_INIT;
  26. void state_switch(enum system_state next) {
  27. if (state == next) {
  28. return;
  29. }
  30. // clean up old state when leaving it
  31. switch (state) {
  32. case STATE_SCAN:
  33. debug("leaving STATE_SCAN");
  34. state_scan_exit();
  35. break;
  36. case STATE_VOLCANO_WORKFLOW:
  37. debug("leaving STATE_VOLCANO_WORKFLOW");
  38. state_volcano_wf_exit();
  39. break;
  40. case STATE_VOLCANO_RUN:
  41. debug("leaving STATE_VOLCANO_RUN");
  42. state_volcano_run_exit();
  43. break;
  44. case STATE_CRAFTY:
  45. debug("leaving STATE_CRAFTY");
  46. state_crafty_exit();
  47. break;
  48. default:
  49. break;
  50. }
  51. // prepare new state on entering
  52. switch (next) {
  53. case STATE_SCAN:
  54. debug("entering STATE_SCAN");
  55. state_scan_enter();
  56. break;
  57. case STATE_VOLCANO_WORKFLOW:
  58. debug("entering STATE_VOLCANO_WORKFLOW");
  59. state_volcano_wf_enter();
  60. break;
  61. case STATE_VOLCANO_RUN:
  62. debug("entering STATE_VOLCANO_RUN");
  63. state_volcano_run_enter();
  64. break;
  65. case STATE_CRAFTY:
  66. debug("entering STATE_CRAFTY");
  67. state_crafty_enter();
  68. break;
  69. default:
  70. break;
  71. }
  72. state = next;
  73. }
  74. void state_run(void) {
  75. switch (state) {
  76. case STATE_INIT:
  77. break;
  78. case STATE_SCAN:
  79. state_scan_run();
  80. break;
  81. case STATE_VOLCANO_WORKFLOW:
  82. state_volcano_wf_run();
  83. break;
  84. case STATE_VOLCANO_RUN:
  85. state_volcano_run_run();
  86. break;
  87. case STATE_CRAFTY:
  88. state_crafty_run();
  89. break;
  90. default:
  91. debug("invalid main state %d", state);
  92. state_switch(STATE_SCAN);
  93. break;
  94. }
  95. }