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

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