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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_volcano_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_volcano_wf_edit(false);
  47. state_switch(STATE_VOLCANO_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. }
  56. static void edit_cb(int selection) {
  57. UNUSED(selection);
  58. state_volcano_wf_edit(true);
  59. state_switch(STATE_VOLCANO_WORKFLOW);
  60. }
  61. void state_scan_enter(void) {
  62. menu_init(enter_cb, edit_cb, NULL, NULL);
  63. ble_scan(BLE_SCAN_ON);
  64. }
  65. void state_scan_exit(void) {
  66. ble_scan(BLE_SCAN_OFF);
  67. menu_deinit();
  68. }
  69. static void draw(struct menu_state *menu) {
  70. result_count = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  71. int pos = 0, devs = 0;
  72. for (int i = 0; i < result_count; i++) {
  73. enum known_devices dev = models_filter_name(results[i].name);
  74. if (dev == DEV_UNKNOWN) {
  75. continue;
  76. }
  77. devs++;
  78. if (((devs - 1) < menu->off)
  79. || ((devs - 1 - menu->off) >= MENU_MAX_LINES)) {
  80. continue;
  81. }
  82. #if defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  83. if (menu->selection < 0) {
  84. #ifdef MENU_PREFER_VOLCANO
  85. if (dev == DEV_VOLCANO) {
  86. menu->selection = devs - 1;
  87. #ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  88. #ifdef VOLCANO_AUTO_CONNECT_WITHIN_MS
  89. if (to_ms_since_boot(get_absolute_time()) <= VOLCANO_AUTO_CONNECT_WITHIN_MS) {
  90. #endif // VOLCANO_AUTO_CONNECT_WITHIN_MS
  91. auto_connect_time = to_ms_since_boot(get_absolute_time());
  92. auto_connect_idx = i;
  93. #ifdef VOLCANO_AUTO_CONNECT_WITHIN_MS
  94. }
  95. #endif // VOLCANO_AUTO_CONNECT_WITHIN_MS
  96. #endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  97. }
  98. #endif // MENU_PREFER_VOLCANO
  99. #ifdef MENU_PREFER_CRAFTY
  100. if (dev == DEV_CRAFTY) {
  101. menu->selection = devs - 1;
  102. }
  103. #endif // MENU_PREFER_CRAFTY
  104. }
  105. #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  106. if ((devs - 1) == menu->selection) {
  107. #ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  108. if ((auto_connect_time != 0) && (!menu_got_input)) {
  109. uint32_t now = to_ms_since_boot(get_absolute_time());
  110. uint32_t diff = now - auto_connect_time;
  111. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  112. "%ld ",
  113. (VOLCANO_AUTO_CONNECT_TIMEOUT_MS / 1000) - (diff / 1000));
  114. } else {
  115. #endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  116. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
  117. #ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  118. }
  119. #endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  120. } else {
  121. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
  122. }
  123. if (dev == DEV_VOLCANO) {
  124. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Volcano ");
  125. } else if (dev == DEV_CRAFTY) {
  126. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Crafty+ ");
  127. }
  128. char info[32] = "";
  129. if (models_get_serial(results[i].data, results[i].data_len,
  130. info, sizeof(info)) < 0) {
  131. strcpy(info, "-error-");
  132. }
  133. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "%s\n", info);
  134. }
  135. menu->length = devs;
  136. #if !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  137. if ((menu->selection < 0) && (menu->length > 0)) {
  138. menu->selection = 0;
  139. }
  140. #endif // !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  141. if (menu->length == 0) {
  142. strncpy(menu->buff, "NONE", MENU_MAX_LEN);
  143. }
  144. }
  145. void state_scan_run(void) {
  146. menu_run(draw, false);
  147. #ifdef VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  148. if ((auto_connect_time != 0) && (!menu_got_input)) {
  149. uint32_t now = to_ms_since_boot(get_absolute_time());
  150. if ((now - auto_connect_time) >= VOLCANO_AUTO_CONNECT_TIMEOUT_MS) {
  151. state_volcano_run_target(results[auto_connect_idx].addr,
  152. results[auto_connect_idx].type);
  153. state_volcano_wf_edit(false);
  154. state_switch(STATE_VOLCANO_WORKFLOW);
  155. auto_connect_time = 0;
  156. }
  157. }
  158. #endif // VOLCANO_AUTO_CONNECT_TIMEOUT_MS
  159. }