S&B Volcano vaporizer remote control with Pi Pico W
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

state_volcano_run.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. static bool aborted = false;
  35. void state_volcano_run_index(uint16_t index) {
  36. wf_index = index;
  37. }
  38. void state_volcano_run_target(bd_addr_t addr, bd_addr_type_t 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. aborted = true;
  47. wf_reset();
  48. volcano_set_pump_state(false);
  49. volcano_set_heater_state(false);
  50. ble_disconnect();
  51. wait_for_disconnect = true;
  52. }
  53. }
  54. }
  55. void state_volcano_run_enter(void) {
  56. menu_init(NULL, NULL);
  57. buttons_callback(volcano_buttons);
  58. debug("workflow connect");
  59. ble_connect(ble_addr, ble_type);
  60. wait_for_connect = true;
  61. aborted = false;
  62. }
  63. void state_volcano_run_exit(void) {
  64. menu_deinit();
  65. wf_reset();
  66. }
  67. static void draw(struct menu_state *menu) {
  68. struct wf_state state = wf_status();
  69. int pos = 0;
  70. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  71. "step %d / %d\n", state.index, state.count);
  72. switch (state.step.op) {
  73. case OP_SET_TEMPERATURE:
  74. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  75. "\nset temp %.1f C", state.step.val / 10.0f);
  76. break;
  77. case OP_WAIT_TEMPERATURE:
  78. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  79. "wait temp %.1f C\n", state.step.val / 10.0f);
  80. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  81. "%.1f -> %.1f -> %.1f",
  82. state.start_val / 10.0f,
  83. state.curr_val / 10.0f,
  84. state.step.val / 10.0f);
  85. break;
  86. case OP_WAIT_TIME:
  87. case OP_PUMP_TIME:
  88. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  89. "%s time %.1f s\n",
  90. (state.step.op == OP_WAIT_TIME) ? "wait" : "pump",
  91. state.step.val / 1000.0f);
  92. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  93. "%.1f -> %.1f -> %.1f",
  94. state.start_val / 1000.0f,
  95. state.curr_val / 1000.0f,
  96. state.step.val / 1000.0f);
  97. break;
  98. }
  99. // TODO visualize
  100. }
  101. void state_volcano_run_run(void) {
  102. // start workflow when connected
  103. if (wait_for_connect && ble_is_connected()) {
  104. wait_for_connect = false;
  105. debug("workflow start");
  106. wf_start(wf_index);
  107. }
  108. // visualize workflow status
  109. menu_run(draw);
  110. // auto disconnect when end of workflow is reached
  111. if ((!wait_for_connect) && (!aborted)) {
  112. struct wf_state state = wf_status();
  113. if (state.status == WF_IDLE) {
  114. debug("workflow disconnect");
  115. ble_disconnect();
  116. wait_for_disconnect = true;
  117. }
  118. }
  119. // back to main menu when disconnected
  120. if (wait_for_disconnect && !ble_is_connected()) {
  121. wait_for_disconnect = false;
  122. debug("workflow done");
  123. state_switch(STATE_SCAN);
  124. }
  125. }