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_conf.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * state_volcano_conf.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 "config.h"
  20. #include "log.h"
  21. #include "menu.h"
  22. #include "volcano.h"
  23. #include "state.h"
  24. #include "state_value.h"
  25. #include "state_volcano_conf.h"
  26. static bd_addr_t ble_addr = {0};
  27. static bd_addr_type_t ble_type = 0;
  28. static bool wait_for_connect = false;
  29. static bool wait_for_disconnect = false;
  30. static bool connected = false;
  31. static bool val_celsius = false;
  32. static bool val_vibrate = false;
  33. static bool val_disp_cool = false;
  34. static uint16_t val_auto_shutoff = 0;
  35. static uint8_t val_brightness = 0;
  36. static char val_fw[VOLCANO_FW_LEN + 1] = {0};
  37. static int32_t val_rt = 0;
  38. void state_volcano_conf_target(bd_addr_t addr, bd_addr_type_t type) {
  39. debug("%s %d", bd_addr_to_str(addr), type);
  40. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  41. ble_type = type;
  42. connected = false;
  43. }
  44. static void enter_cb(int selection) {
  45. switch (selection) {
  46. case 0:
  47. // Celsius
  48. state_value_set(&val_celsius,
  49. sizeof(val_celsius),
  50. 0, 1, VAL_STEP_INCREMENT, 1,
  51. "Celsius");
  52. state_value_return(STATE_VOLCANO_CONF);
  53. state_switch(STATE_VALUE);
  54. break;
  55. case 1:
  56. // Vibrate
  57. state_value_set(&val_vibrate,
  58. sizeof(val_vibrate),
  59. 0, 1, VAL_STEP_INCREMENT, 1,
  60. "Vibrate");
  61. state_value_return(STATE_VOLCANO_CONF);
  62. state_switch(STATE_VALUE);
  63. break;
  64. case 2:
  65. // Disp. Cool
  66. state_value_set(&val_disp_cool,
  67. sizeof(val_disp_cool),
  68. 0, 1, VAL_STEP_INCREMENT, 1,
  69. "Disp. Cool");
  70. state_value_return(STATE_VOLCANO_CONF);
  71. state_switch(STATE_VALUE);
  72. break;
  73. case 3:
  74. // Auto Shutoff
  75. state_value_set(&val_auto_shutoff,
  76. sizeof(val_auto_shutoff),
  77. 0, 60 * 60, VAL_STEP_INCREMENT, 60,
  78. "Auto Shutoff");
  79. state_value_return(STATE_VOLCANO_CONF);
  80. state_switch(STATE_VALUE);
  81. break;
  82. case 4:
  83. // Brightness
  84. state_value_set(&val_brightness,
  85. sizeof(val_brightness),
  86. 0, 100, VAL_STEP_INCREMENT, 10,
  87. "Brightness");
  88. state_value_return(STATE_VOLCANO_CONF);
  89. state_switch(STATE_VALUE);
  90. break;
  91. }
  92. }
  93. static void send_values(void) {
  94. volcano_set_unit(val_celsius ? UNIT_C : UNIT_F);
  95. sleep_ms(250);
  96. volcano_set_vibration(val_vibrate);
  97. sleep_ms(250);
  98. volcano_set_display_cooling(val_disp_cool);
  99. sleep_ms(250);
  100. volcano_set_auto_shutoff(val_auto_shutoff);
  101. sleep_ms(250);
  102. volcano_set_brightness(val_brightness);
  103. }
  104. static void fetch_values(void) {
  105. volcano_discover_characteristics(false, true);
  106. enum unit unit = volcano_get_unit();
  107. val_celsius = (unit == UNIT_C);
  108. int8_t r = volcano_get_vibration();
  109. val_vibrate = (r == 1);
  110. r = volcano_get_display_cooling();
  111. val_disp_cool = (r == 1);
  112. val_auto_shutoff = volcano_get_auto_shutoff();
  113. val_brightness = volcano_get_brightness();
  114. volcano_get_firmware(val_fw);
  115. val_fw[VOLCANO_FW_LEN] = '\0';
  116. val_rt = volcano_get_runtime();
  117. }
  118. static void exit_cb(void) {
  119. debug("volcano disconnect");
  120. ble_disconnect();
  121. wait_for_disconnect = true;
  122. }
  123. void state_volcano_conf_enter(void) {
  124. menu_init(enter_cb, NULL, NULL, exit_cb);
  125. if (!connected) {
  126. debug("volcano connect");
  127. ble_connect(ble_addr, ble_type);
  128. wait_for_connect = true;
  129. } else {
  130. debug("volcano write");
  131. send_values();
  132. }
  133. }
  134. void state_volcano_conf_exit(void) {
  135. menu_deinit();
  136. }
  137. static void draw(struct menu_state *menu) {
  138. if (wait_for_connect) {
  139. snprintf(menu->buff, MENU_MAX_LEN,
  140. "Connecting\nand\nDiscovering");
  141. return;
  142. } else if (wait_for_disconnect) {
  143. snprintf(menu->buff, MENU_MAX_LEN,
  144. "\nDisconnecting");
  145. return;
  146. }
  147. int pos = 0;
  148. menu->length = 0;
  149. ADD_STATIC_ELEMENT("Celsius (%d)", val_celsius);
  150. ADD_STATIC_ELEMENT("Vibrate (%d)", val_vibrate);
  151. ADD_STATIC_ELEMENT("Disp. Cool (%d)", val_disp_cool);
  152. ADD_STATIC_ELEMENT("Auto Shutoff (%d)", val_auto_shutoff / 60);
  153. ADD_STATIC_ELEMENT("Brightness (%d)", val_brightness / 10);
  154. ADD_STATIC_ELEMENT("FW: %s", val_fw);
  155. ADD_STATIC_ELEMENT("RT: %.1f h", val_rt / 60.0f);
  156. if (menu->selection < 0) {
  157. menu->selection = 0;
  158. }
  159. }
  160. void state_volcano_conf_run(void) {
  161. if (wait_for_connect && ble_is_connected()) {
  162. wait_for_connect = false;
  163. connected = true;
  164. debug("volcano start");
  165. fetch_values();
  166. }
  167. menu_run(draw, true);
  168. // back to main menu when disconnected
  169. if (wait_for_disconnect && !ble_is_connected()) {
  170. wait_for_disconnect = false;
  171. connected = false;
  172. debug("volcano done");
  173. state_switch(STATE_SCAN);
  174. }
  175. }