S&B Volcano vaporizer remote control with Pi Pico W
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

state_scan.c 5.8KB

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