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.1KB

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