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

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