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_volcano_run.c 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * state_volcano_run.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 <stdio.h>
  19. #include <string.h>
  20. #include "config.h"
  21. #include "log.h"
  22. #include "workflow.h"
  23. #include "state.h"
  24. #include "state_volcano_run.h"
  25. #include "menu.h"
  26. static uint16_t wf_index = 0;
  27. static bd_addr_t ble_addr = {0};
  28. static bd_addr_type_t ble_type = 0;
  29. static bool wait_for_connect = false;
  30. static bool wait_for_disconnect = false;
  31. void state_volcano_run_index(uint16_t index) {
  32. wf_index = index;
  33. }
  34. void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
  35. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  36. ble_type = type;
  37. }
  38. void state_volcano_run_enter(void) {
  39. debug("workflow connect");
  40. ble_connect(ble_addr, ble_type);
  41. wait_for_connect = true;
  42. }
  43. void state_volcano_run_exit(void) {
  44. wf_reset();
  45. }
  46. static void draw(struct menu_state *menu) {
  47. struct wf_state state = wf_status();
  48. snprintf(menu->buff, MENU_MAX_LEN, "%d / %d", state.step, state.count);
  49. // TODO visualize
  50. }
  51. void state_volcano_run_run(void) {
  52. if (wait_for_connect && ble_is_connected()) {
  53. wait_for_connect = false;
  54. debug("workflow start");
  55. wf_start(wf_index);
  56. }
  57. menu_run(draw);
  58. if (!wait_for_connect) {
  59. struct wf_state state = wf_status();
  60. if (state.status == WF_IDLE) {
  61. debug("workflow disconnect");
  62. ble_disconnect();
  63. wait_for_disconnect = true;
  64. }
  65. }
  66. if (wait_for_disconnect && !ble_is_connected()) {
  67. wait_for_disconnect = false;
  68. debug("workflow done");
  69. state_switch(STATE_SCAN);
  70. }
  71. }