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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. void state_volcano_conf_target(bd_addr_t addr, bd_addr_type_t type) {
  35. debug("%s %d", bd_addr_to_str(addr), type);
  36. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  37. ble_type = type;
  38. connected = false;
  39. }
  40. static void enter_cb(int selection) {
  41. switch (selection) {
  42. case 0:
  43. // Celsius
  44. state_value_set(&val_celsius,
  45. sizeof(val_celsius),
  46. 0, 1, VAL_STEP_INCREMENT, 1,
  47. "Celsius");
  48. state_value_return(STATE_VOLCANO_CONF);
  49. state_switch(STATE_VALUE);
  50. break;
  51. case 1:
  52. // Vibrate
  53. state_value_set(&val_vibrate,
  54. sizeof(val_vibrate),
  55. 0, 1, VAL_STEP_INCREMENT, 1,
  56. "Vibrate");
  57. state_value_return(STATE_VOLCANO_CONF);
  58. state_switch(STATE_VALUE);
  59. break;
  60. case 2:
  61. // Disp. Cool
  62. state_value_set(&val_disp_cool,
  63. sizeof(val_disp_cool),
  64. 0, 1, VAL_STEP_INCREMENT, 1,
  65. "Disp. Cool");
  66. state_value_return(STATE_VOLCANO_CONF);
  67. state_switch(STATE_VALUE);
  68. break;
  69. }
  70. }
  71. static void send_values(void) {
  72. volcano_set_unit(val_celsius ? UNIT_C : UNIT_F);
  73. sleep_ms(100);
  74. volcano_set_vibration(val_vibrate);
  75. sleep_ms(100);
  76. volcano_set_display_cooling(val_disp_cool);
  77. }
  78. static void fetch_values(void) {
  79. enum unit unit = volcano_get_unit();
  80. val_celsius = (unit == UNIT_C);
  81. int8_t r = volcano_get_vibration();
  82. val_vibrate = (r == 1);
  83. r = volcano_get_display_cooling();
  84. val_disp_cool = (r == 1);
  85. }
  86. static void exit_cb(void) {
  87. debug("volcano disconnect");
  88. ble_disconnect();
  89. wait_for_disconnect = true;
  90. }
  91. void state_volcano_conf_enter(void) {
  92. menu_init(enter_cb, NULL, NULL, exit_cb);
  93. if (!connected) {
  94. debug("volcano connect");
  95. ble_connect(ble_addr, ble_type);
  96. wait_for_connect = true;
  97. } else {
  98. debug("volcano write");
  99. send_values();
  100. }
  101. }
  102. void state_volcano_conf_exit(void) {
  103. menu_deinit();
  104. }
  105. static void draw(struct menu_state *menu) {
  106. if (wait_for_connect) {
  107. snprintf(menu->buff, MENU_MAX_LEN,
  108. "Connecting\nand\nDiscovering");
  109. return;
  110. } else if (wait_for_disconnect) {
  111. snprintf(menu->buff, MENU_MAX_LEN,
  112. "\nDisconnecting");
  113. return;
  114. }
  115. int pos = 0;
  116. menu->length = 0;
  117. ADD_STATIC_ELEMENT("Celsius");
  118. ADD_STATIC_ELEMENT("Vibrate");
  119. ADD_STATIC_ELEMENT("Disp. Cool");
  120. if (menu->selection < 0) {
  121. menu->selection = 0;
  122. }
  123. }
  124. void state_volcano_conf_run(void) {
  125. if (wait_for_connect && ble_is_connected()) {
  126. wait_for_connect = false;
  127. connected = true;
  128. debug("volcano start");
  129. fetch_values();
  130. }
  131. menu_run(draw, true);
  132. // back to main menu when disconnected
  133. if (wait_for_disconnect && !ble_is_connected()) {
  134. wait_for_disconnect = false;
  135. connected = false;
  136. debug("volcano done");
  137. state_switch(STATE_SCAN);
  138. }
  139. }