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_scan.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * state_scan.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 "pico/stdlib.h"
  21. #include "config.h"
  22. #include "ble.h"
  23. #include "models.h"
  24. #include "mem.h"
  25. #include "menu.h"
  26. #include "util.h"
  27. #include "state.h"
  28. #include "state_workflow.h"
  29. #include "state_volcano_run.h"
  30. #include "state_volcano_conf.h"
  31. #include "state_crafty.h"
  32. #include "state_venty.h"
  33. #include "state_scan.h"
  34. static struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  35. static int result_count = 0;
  36. static uint32_t auto_connect_time = 0;
  37. static int auto_connect_idx = 0;
  38. static void enter_cb(int selection) {
  39. int devs = 0;
  40. for (int i = 0; i < result_count; i++) {
  41. enum known_devices dev = models_filter_name(results[i].name);
  42. if (dev == DEV_UNKNOWN) {
  43. continue;
  44. }
  45. if (devs++ == selection) {
  46. if (dev == DEV_VOLCANO) {
  47. state_volcano_run_target(results[i].addr, results[i].type);
  48. state_wf_edit(false);
  49. state_switch(STATE_WORKFLOW);
  50. } else if (dev == DEV_CRAFTY) {
  51. state_crafty_target(results[i].addr, results[i].type);
  52. state_switch(STATE_CRAFTY);
  53. } else if (dev == DEV_VENTY) {
  54. state_venty_target(results[i].addr, results[i].type);
  55. state_switch(STATE_VENTY);
  56. }
  57. return;
  58. }
  59. }
  60. if (selection == devs) {
  61. state_switch(STATE_SETTINGS);
  62. } else if (selection == (devs + 1)) {
  63. state_switch(STATE_ABOUT);
  64. }
  65. }
  66. static void edit_cb(int selection) {
  67. int devs = 0;
  68. for (int i = 0; i < result_count; i++) {
  69. enum known_devices dev = models_filter_name(results[i].name);
  70. if (dev == DEV_UNKNOWN) {
  71. continue;
  72. }
  73. if (devs++ == selection) {
  74. if (dev == DEV_VOLCANO) {
  75. state_volcano_conf_target(results[i].addr, results[i].type);
  76. state_switch(STATE_VOLCANO_CONF);
  77. } else if (dev == DEV_CRAFTY) {
  78. state_crafty_target(results[i].addr, results[i].type);
  79. state_switch(STATE_CRAFTY);
  80. } else if (dev == DEV_VENTY) {
  81. state_venty_target(results[i].addr, results[i].type);
  82. state_switch(STATE_VENTY);
  83. }
  84. return;
  85. }
  86. }
  87. if (selection == devs) {
  88. state_switch(STATE_SETTINGS);
  89. } else if (selection == (devs + 1)) {
  90. state_switch(STATE_ABOUT);
  91. }
  92. }
  93. static void ota_cb(int selection) {
  94. UNUSED(selection);
  95. reset_to_ota();
  96. }
  97. void state_scan_enter(void) {
  98. menu_init(enter_cb, edit_cb, ota_cb, NULL);
  99. ble_scan(BLE_SCAN_ON);
  100. }
  101. void state_scan_exit(void) {
  102. ble_scan(BLE_SCAN_OFF);
  103. menu_deinit();
  104. }
  105. static void draw(struct menu_state *menu) {
  106. result_count = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  107. int pos = 0, devs = 0;
  108. for (int i = 0; i < result_count; i++) {
  109. enum known_devices dev = models_filter_name(results[i].name);
  110. if (dev == DEV_UNKNOWN) {
  111. continue;
  112. }
  113. devs++;
  114. if (((devs - 1) < menu->off)
  115. || ((devs - 1 - menu->off) >= MENU_MAX_LINES)) {
  116. continue;
  117. }
  118. #if defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  119. if (menu->selection < 0) {
  120. #ifdef MENU_PREFER_VOLCANO
  121. if (dev == DEV_VOLCANO) {
  122. menu->selection = devs - 1;
  123. if (to_ms_since_boot(get_absolute_time()) <= VOLCANO_AUTO_CONNECT_WITHIN_MS) {
  124. if (mem_data()->wf_auto_connect) {
  125. auto_connect_time = to_ms_since_boot(get_absolute_time());
  126. auto_connect_idx = i;
  127. }
  128. }
  129. }
  130. #endif // MENU_PREFER_VOLCANO
  131. #ifdef MENU_PREFER_CRAFTY
  132. if (dev == DEV_CRAFTY) {
  133. menu->selection = devs - 1;
  134. }
  135. #endif // MENU_PREFER_CRAFTY
  136. }
  137. #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  138. if ((devs - 1) == menu->selection) {
  139. if ((auto_connect_time != 0) && (!menu_got_input)) {
  140. uint32_t now = to_ms_since_boot(get_absolute_time());
  141. uint32_t diff = now - auto_connect_time;
  142. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  143. "%ld ",
  144. (VOLCANO_AUTO_CONNECT_TIMEOUT_MS / 1000) - (diff / 1000));
  145. } else {
  146. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
  147. }
  148. } else {
  149. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
  150. }
  151. if (dev == DEV_VOLCANO) {
  152. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Volcano ");
  153. } else if (dev == DEV_CRAFTY) {
  154. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Crafty+ ");
  155. } else if (dev == DEV_VENTY) {
  156. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Venty ");
  157. }
  158. char info[32] = "";
  159. if (models_get_serial(dev, results[i].name,
  160. results[i].data, results[i].data_len,
  161. info, sizeof(info)) < 0) {
  162. strcpy(info, "-error-");
  163. }
  164. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "%s\n", info);
  165. }
  166. menu->length = devs;
  167. #if !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  168. if ((menu->selection < 0) && (menu->length > 0)) {
  169. menu->selection = 0;
  170. }
  171. #endif // !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  172. ADD_STATIC_ELEMENT("Settings");
  173. ADD_STATIC_ELEMENT("About");
  174. }
  175. void state_scan_run(void) {
  176. menu_run(draw, false);
  177. if ((auto_connect_time != 0) && (!menu_got_input)) {
  178. uint32_t now = to_ms_since_boot(get_absolute_time());
  179. if ((now - auto_connect_time) >= VOLCANO_AUTO_CONNECT_TIMEOUT_MS) {
  180. state_volcano_run_target(results[auto_connect_idx].addr,
  181. results[auto_connect_idx].type);
  182. state_wf_edit(false);
  183. state_switch(STATE_WORKFLOW);
  184. auto_connect_time = 0;
  185. }
  186. }
  187. }