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

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