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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "buttons.h"
  22. #include "log.h"
  23. #include "volcano.h"
  24. #include "workflow.h"
  25. #include "state.h"
  26. #include "state_volcano_run.h"
  27. // only used to draw textbox, not for buttons
  28. #include "menu.h"
  29. static uint16_t wf_index = 0;
  30. static bd_addr_t ble_addr = {0};
  31. static bd_addr_type_t ble_type = 0;
  32. static bool wait_for_connect = false;
  33. static bool wait_for_disconnect = false;
  34. void state_volcano_run_index(uint16_t index) {
  35. wf_index = index;
  36. }
  37. void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
  38. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  39. ble_type = type;
  40. }
  41. static void volcano_buttons(enum buttons btn, bool state) {
  42. if (state && (btn == BTN_Y)) {
  43. if ((!wait_for_connect) && (!wait_for_disconnect)) {
  44. debug("workflow abort");
  45. wf_reset();
  46. volcano_set_pump_state(false);
  47. volcano_set_heater_state(false);
  48. ble_disconnect();
  49. wait_for_disconnect = true;
  50. }
  51. }
  52. }
  53. void state_volcano_run_enter(void) {
  54. menu_init(NULL, NULL, NULL, NULL);
  55. buttons_callback(volcano_buttons);
  56. debug("workflow connect");
  57. ble_connect(ble_addr, ble_type);
  58. wait_for_connect = true;
  59. }
  60. void state_volcano_run_exit(void) {
  61. menu_deinit();
  62. wf_reset();
  63. }
  64. static void draw(struct menu_state *menu) {
  65. struct wf_state state = wf_status();
  66. if (state.status == WF_IDLE) {
  67. if (wait_for_connect) {
  68. snprintf(menu->buff, MENU_MAX_LEN,
  69. "Connecting\nand\nDiscovering");
  70. } else if (wait_for_disconnect) {
  71. snprintf(menu->buff, MENU_MAX_LEN,
  72. "Disconnecting");
  73. }
  74. return;
  75. }
  76. int pos = 0;
  77. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  78. "step %d / %d\n", state.index, state.count);
  79. switch (state.step.op) {
  80. case OP_SET_TEMPERATURE:
  81. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  82. "\n%s", wf_step_str(state.step));
  83. break;
  84. case OP_WAIT_TEMPERATURE:
  85. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  86. "%s\n", wf_step_str(state.step));
  87. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  88. "%.1f -> %.1f -> %.1f",
  89. state.start_val / 10.0f,
  90. state.curr_val / 10.0f,
  91. state.step.val / 10.0f);
  92. break;
  93. case OP_WAIT_TIME:
  94. case OP_PUMP_TIME:
  95. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  96. "%s\n", wf_step_str(state.step));
  97. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  98. "%.0f -> %.0f -> %.0f",
  99. state.start_val / 1000.0f,
  100. state.curr_val / 1000.0f,
  101. state.step.val / 1000.0f);
  102. break;
  103. }
  104. // TODO visualize
  105. }
  106. void state_volcano_run_run(void) {
  107. // start workflow when connected
  108. if (wait_for_connect && ble_is_connected()) {
  109. wait_for_connect = false;
  110. debug("workflow start");
  111. wf_start(wf_index);
  112. }
  113. // visualize workflow status
  114. menu_run(draw, true);
  115. // auto disconnect when end of workflow is reached
  116. if ((!wait_for_connect) && (!wait_for_disconnect)) {
  117. struct wf_state state = wf_status();
  118. if (state.status == WF_IDLE) {
  119. debug("workflow disconnect");
  120. ble_disconnect();
  121. wait_for_disconnect = true;
  122. }
  123. }
  124. // back to main menu when disconnected
  125. if (wait_for_disconnect && !ble_is_connected()) {
  126. wait_for_disconnect = false;
  127. debug("workflow done");
  128. state_switch(STATE_SCAN);
  129. }
  130. }