S&B Volcano vaporizer remote control with Pi Pico W
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * state_venty.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 "menu.h"
  22. #include "venty.h"
  23. #include "log.h"
  24. #include "state.h"
  25. #include "state_value.h"
  26. #include "state_venty.h"
  27. static bd_addr_t ble_addr = {0};
  28. static bd_addr_type_t ble_type = 0;
  29. static bool wait_for_connect = false;
  30. static bool wait_for_disconnect = false;
  31. static bool connected = false;
  32. static bool heater_state = false;
  33. static int16_t target_temp = 0;
  34. static int8_t battery = 0;
  35. static int8_t eco_current = 0;
  36. static int8_t eco_voltage = 0;
  37. static int8_t vibration = 0;
  38. static int8_t brightness = 0;
  39. void state_venty_target(bd_addr_t addr, bd_addr_type_t type) {
  40. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  41. ble_type = type;
  42. connected = false;
  43. }
  44. static void exit_cb(void) {
  45. debug("venty disconnect");
  46. ble_disconnect();
  47. wait_for_disconnect = true;
  48. }
  49. static void enter_cb(int selection) {
  50. switch (selection) {
  51. case 0:
  52. // Battery
  53. break;
  54. case 1:
  55. // Target
  56. state_value_set(&target_temp,
  57. sizeof(target_temp),
  58. 400, 2100, VAL_STEP_INCREMENT, 10,
  59. "Target");
  60. state_value_return(STATE_VENTY);
  61. state_switch(STATE_VALUE);
  62. break;
  63. case 2:
  64. // Heater
  65. state_value_set(&heater_state,
  66. sizeof(heater_state),
  67. 0, 1, VAL_STEP_INCREMENT, 1,
  68. "Heater");
  69. state_value_return(STATE_VENTY);
  70. state_switch(STATE_VALUE);
  71. break;
  72. case 3:
  73. // Eco Current
  74. state_value_set(&eco_current,
  75. sizeof(eco_current),
  76. 0, 1, VAL_STEP_INCREMENT, 1,
  77. "Eco Amps");
  78. state_value_return(STATE_VENTY);
  79. state_switch(STATE_VALUE);
  80. break;
  81. case 4:
  82. // Eco Voltage
  83. state_value_set(&eco_voltage,
  84. sizeof(eco_voltage),
  85. 0, 1, VAL_STEP_INCREMENT, 1,
  86. "Eco Volt");
  87. state_value_return(STATE_VENTY);
  88. state_switch(STATE_VALUE);
  89. break;
  90. case 5:
  91. // Vibration
  92. state_value_set(&vibration,
  93. sizeof(vibration),
  94. 0, 1, VAL_STEP_INCREMENT, 1,
  95. "Vibration");
  96. state_value_return(STATE_VENTY);
  97. state_switch(STATE_VALUE);
  98. break;
  99. case 6:
  100. // Brightness
  101. state_value_set(&brightness,
  102. sizeof(brightness),
  103. 1, 9, VAL_STEP_INCREMENT, 1,
  104. "Brightness");
  105. state_value_return(STATE_VENTY);
  106. state_switch(STATE_VALUE);
  107. break;
  108. default:
  109. exit_cb();
  110. break;
  111. }
  112. }
  113. static void send_values(void) {
  114. venty_set_heater_state(heater_state);
  115. sleep_ms(250);
  116. venty_set_target_temp(target_temp);
  117. sleep_ms(250);
  118. venty_set_eco_current(eco_current);
  119. sleep_ms(250);
  120. venty_set_eco_voltage(eco_voltage);
  121. sleep_ms(250);
  122. venty_set_vibration(vibration);
  123. sleep_ms(250);
  124. venty_set_brightness(brightness);
  125. }
  126. static void fetch_values(void) {
  127. heater_state = venty_get_heater_state();
  128. target_temp = venty_get_target_temp();
  129. battery = venty_get_battery_state();
  130. eco_current = venty_get_eco_current();
  131. eco_voltage = venty_get_eco_voltage();
  132. vibration = venty_get_vibration();
  133. brightness = venty_get_brightness();
  134. }
  135. void state_venty_enter(void) {
  136. menu_init(enter_cb, NULL, NULL, exit_cb);
  137. if (!connected) {
  138. debug("venty connect");
  139. ble_connect(ble_addr, ble_type);
  140. wait_for_connect = true;
  141. } else {
  142. debug("venty write");
  143. send_values();
  144. }
  145. }
  146. void state_venty_exit(void) {
  147. menu_deinit();
  148. }
  149. static void draw(struct menu_state *menu) {
  150. if (wait_for_connect) {
  151. snprintf(menu->buff, MENU_MAX_LEN,
  152. "\nConnecting");
  153. return;
  154. } else if (wait_for_disconnect) {
  155. snprintf(menu->buff, MENU_MAX_LEN,
  156. "\nDisconnecting");
  157. return;
  158. }
  159. int pos = 0;
  160. menu->length = 0;
  161. ADD_STATIC_ELEMENT("Battery: %d %%", battery);
  162. ADD_STATIC_ELEMENT("Target (%d C)", target_temp / 10);
  163. ADD_STATIC_ELEMENT("Heater (%d)", heater_state);
  164. ADD_STATIC_ELEMENT("Eco Amps (%d)", eco_current);
  165. ADD_STATIC_ELEMENT("Eco Volt (%d)", eco_voltage);
  166. ADD_STATIC_ELEMENT("Vibration (%d)", vibration);
  167. ADD_STATIC_ELEMENT("Brightness (%d)", brightness);
  168. ADD_STATIC_ELEMENT("... go back");
  169. if (menu->selection < 0) {
  170. menu->selection = 0;
  171. }
  172. }
  173. void state_venty_run(void) {
  174. if (wait_for_connect && ble_is_connected()) {
  175. wait_for_connect = false;
  176. connected = true;
  177. debug("venty start");
  178. fetch_values();
  179. }
  180. menu_run(draw, true);
  181. if (wait_for_disconnect && !ble_is_connected()) {
  182. wait_for_disconnect = false;
  183. connected = false;
  184. debug("venty done");
  185. state_switch(STATE_SCAN);
  186. }
  187. }