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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "lcd.h"
  24. #include "volcano.h"
  25. #include "workflow.h"
  26. #include "util.h"
  27. #include "state.h"
  28. #include "state_volcano_run.h"
  29. // only used to draw textbox, not for buttons
  30. #include "menu.h"
  31. static uint16_t wf_index = 0;
  32. static bd_addr_t ble_addr = {0};
  33. static bd_addr_type_t ble_type = 0;
  34. static bool wait_for_connect = false;
  35. static bool wait_for_disconnect = false;
  36. void state_volcano_run_index(uint16_t index) {
  37. wf_index = index;
  38. }
  39. void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t type) {
  40. debug("%s %d", bd_addr_to_str(addr), type);
  41. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  42. ble_type = type;
  43. }
  44. static void volcano_buttons(enum buttons btn, bool state) {
  45. if (state && (btn == BTN_Y)) {
  46. if ((!wait_for_connect) && (!wait_for_disconnect)) {
  47. debug("workflow abort");
  48. wf_reset();
  49. volcano_set_pump_state(false);
  50. volcano_set_heater_state(false);
  51. ble_disconnect();
  52. wait_for_disconnect = true;
  53. }
  54. }
  55. }
  56. void state_volcano_run_enter(void) {
  57. menu_init(NULL, NULL, NULL, NULL);
  58. buttons_callback(volcano_buttons);
  59. debug("workflow connect");
  60. ble_connect(ble_addr, ble_type);
  61. wait_for_connect = true;
  62. }
  63. void state_volcano_run_exit(void) {
  64. menu_deinit();
  65. wf_reset();
  66. }
  67. static void bar_graph(int y_off, int h, int val_min, int val, int val_max) {
  68. float v = map(val, val_min, val_max, 0.0f, 1.0f);
  69. uint16_t width = v * (LCD_WIDTH - 1);
  70. uint32_t c = from_hsv(v * 0.333, 1.0, 1.0);
  71. lcd_write_rect(0, y_off, width, y_off + h - 1, c);
  72. }
  73. static void draw(struct menu_state *menu) {
  74. static struct wf_state prev_state = {0};
  75. struct wf_state state = wf_status();
  76. if ((state.step == prev_state.step)
  77. && ((((state.step->op == OP_SET_TEMPERATURE) || (state.step->op == OP_WAIT_TEMPERATURE))
  78. && ((state.curr_val / 10) == (prev_state.curr_val / 10)))
  79. || (((state.step->op == OP_PUMP_TIME) || (state.step->op == OP_WAIT_TIME))
  80. && ((state.curr_val / 500) == (prev_state.curr_val / 500))))) {
  81. return;
  82. }
  83. prev_state = state;
  84. menu->lines = 3;
  85. menu->y_off = 20 + 2;
  86. lcd_write_rect(0, 50,
  87. LCD_WIDTH - 1,
  88. 50 + menu->y_off - 1,
  89. LCD_BLACK);
  90. lcd_write_rect(0, 50 + MENU_BOX_HEIGHT(3, 20, 2) + menu->y_off,
  91. LCD_WIDTH - 1,
  92. 50 + MENU_BOX_HEIGHT(3, 20, 2) + (menu->y_off * 2) - 1,
  93. LCD_BLACK);
  94. if (state.status == WF_IDLE) {
  95. if (wait_for_connect) {
  96. snprintf(menu->buff, MENU_MAX_LEN,
  97. "Connecting\nand\nDiscovering");
  98. } else if (wait_for_disconnect) {
  99. snprintf(menu->buff, MENU_MAX_LEN,
  100. "\nDisconnecting");
  101. } else {
  102. snprintf(menu->buff, MENU_MAX_LEN,
  103. "\nDone");
  104. }
  105. return;
  106. }
  107. bar_graph(50, menu->y_off, 0, state.index + 1, state.count);
  108. bar_graph(50 + MENU_BOX_HEIGHT(3, 20, 2) + menu->y_off, menu->y_off,
  109. state.start_val, state.curr_val, state.step->val);
  110. int pos = 0;
  111. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  112. "step %d / %d\n", state.index, state.count);
  113. switch (state.step->op) {
  114. case OP_SET_TEMPERATURE:
  115. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  116. "\n%s", wf_step_str(state.step));
  117. break;
  118. case OP_WAIT_TEMPERATURE:
  119. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  120. "%s\n", wf_step_str(state.step));
  121. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  122. "%.1f -> %.1f -> %.1f",
  123. state.start_val / 10.0f,
  124. state.curr_val / 10.0f,
  125. state.step->val / 10.0f);
  126. break;
  127. case OP_WAIT_TIME:
  128. case OP_PUMP_TIME:
  129. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  130. "%s\n", wf_step_str(state.step));
  131. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  132. "%.0f -> %.1f -> %.0f",
  133. state.start_val / 1000.0f,
  134. state.curr_val / 1000.0f,
  135. state.step->val / 1000.0f);
  136. break;
  137. }
  138. }
  139. void state_volcano_run_run(void) {
  140. // start workflow when connected
  141. if (wait_for_connect && ble_is_connected()) {
  142. wait_for_connect = false;
  143. debug("workflow start");
  144. wf_start(wf_index);
  145. }
  146. // visualize workflow status
  147. menu_run(draw, true);
  148. // auto disconnect when end of workflow is reached
  149. if ((!wait_for_connect) && (!wait_for_disconnect)) {
  150. struct wf_state state = wf_status();
  151. if (state.status == WF_IDLE) {
  152. debug("workflow disconnect");
  153. ble_disconnect();
  154. wait_for_disconnect = true;
  155. }
  156. }
  157. // back to main menu when disconnected
  158. if (wait_for_disconnect && !ble_is_connected()) {
  159. wait_for_disconnect = false;
  160. debug("workflow done");
  161. state_switch(STATE_SCAN);
  162. }
  163. }