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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.h"
  22. static enum system_state state = STATE_INIT;
  23. void state_switch(enum system_state next) {
  24. if (state == next) {
  25. return;
  26. }
  27. // clean up old state when leaving it
  28. switch (state) {
  29. case STATE_SCAN:
  30. debug("leaving STATE_SCAN");
  31. state_scan_exit();
  32. break;
  33. default:
  34. break;
  35. }
  36. // prepare new state on entering
  37. switch (next) {
  38. case STATE_SCAN:
  39. debug("entering STATE_SCAN");
  40. state_scan_enter();
  41. break;
  42. default:
  43. break;
  44. }
  45. state = next;
  46. }
  47. void state_run(void) {
  48. // TODO only for testing
  49. static uint32_t last_heartbeat = 0;
  50. uint32_t now = to_ms_since_boot(get_absolute_time());
  51. if (now < (last_heartbeat + 1000)) {
  52. return;
  53. }
  54. last_heartbeat = now;
  55. switch (state) {
  56. case STATE_INIT:
  57. break;
  58. case STATE_SCAN: {
  59. state_scan_run();
  60. break;
  61. }
  62. default:
  63. debug("invalid main state %d", state);
  64. state_switch(STATE_SCAN);
  65. break;
  66. }
  67. }