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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "config.h"
  21. #include "ble.h"
  22. #include "models.h"
  23. #include "menu.h"
  24. #include "state.h"
  25. #include "state_volcano_run.h"
  26. #include "state_crafty.h"
  27. #include "state_scan.h"
  28. static struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  29. static int result_count = 0;
  30. static void enter_cb(int selection) {
  31. int devs = 0;
  32. for (int i = 0; i < result_count; i++) {
  33. enum known_devices dev = models_filter_name(results[i].name);
  34. if (dev == DEV_UNKNOWN) {
  35. continue;
  36. }
  37. if (devs++ == selection) {
  38. if (dev == DEV_VOLCANO) {
  39. state_volcano_run_target(results[i].addr, results[i].type);
  40. state_switch(STATE_VOLCANO_WORKFLOW);
  41. } else if (dev == DEV_CRAFTY) {
  42. state_crafty_target(results[i].addr, results[i].type);
  43. state_switch(STATE_CRAFTY);
  44. }
  45. return;
  46. }
  47. }
  48. }
  49. void state_scan_enter(void) {
  50. menu_init(enter_cb);
  51. ble_scan(BLE_SCAN_ON);
  52. }
  53. void state_scan_exit(void) {
  54. menu_deinit();
  55. ble_scan(BLE_SCAN_OFF);
  56. }
  57. static void draw(struct menu_state *menu) {
  58. result_count = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  59. int pos = 0, devs = 0;
  60. for (int i = 0; i < result_count; i++) {
  61. enum known_devices dev = models_filter_name(results[i].name);
  62. if (dev == DEV_UNKNOWN) {
  63. continue;
  64. }
  65. devs++;
  66. if (((devs - 1) < menu->off)
  67. || ((devs - 1 - menu->off) >= MENU_MAX_LINES)) {
  68. continue;
  69. }
  70. #if defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  71. if (menu->selection < 0) {
  72. #ifdef MENU_PREFER_VOLCANO
  73. if (dev == DEV_VOLCANO) {
  74. menu->selection = devs - 1;
  75. }
  76. #endif // MENU_PREFER_VOLCANO
  77. #ifdef MENU_PREFER_CRAFTY
  78. if (dev == DEV_CRAFTY) {
  79. menu->selection = devs - 1;
  80. }
  81. #endif // MENU_PREFER_CRAFTY
  82. }
  83. #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  84. if ((devs - 1) == menu->selection) {
  85. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
  86. } else {
  87. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
  88. }
  89. if (dev == DEV_VOLCANO) {
  90. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Volcano ");
  91. } else if (dev == DEV_CRAFTY) {
  92. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "Crafty+ ");
  93. }
  94. char info[32] = "";
  95. if (models_get_serial(results[i].data, results[i].data_len,
  96. info, sizeof(info)) < 0) {
  97. strcpy(info, "-error-");
  98. }
  99. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "%s\n", info);
  100. }
  101. menu->length = devs;
  102. #if !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  103. if ((menu->selection < 0) && (menu->length > 0)) {
  104. menu->selection = 0;
  105. }
  106. #endif // !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  107. if (menu->length == 0) {
  108. strncpy(menu->buff, "NONE", MENU_MAX_LEN);
  109. }
  110. }
  111. void state_scan_run(void) {
  112. menu_run(draw);
  113. }