S&B Volcano vaporizer remote control with Pi Pico W
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

state_volcano_run.c 4.4KB

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