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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "config.h"
  20. #include "log.h"
  21. #include "buttons.h"
  22. #include "ble.h"
  23. #include "lcd.h"
  24. #include "text.h"
  25. #include "models.h"
  26. #include "state.h"
  27. void state_scan_enter(void) {
  28. ble_scan(BLE_SCAN_ON);
  29. }
  30. void state_scan_exit(void) {
  31. ble_scan(BLE_SCAN_OFF);
  32. }
  33. void state_scan_run(void) {
  34. static char prev_buff[512] = {0};
  35. char buff[512] = {0};
  36. struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  37. int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  38. uint pos = 0;
  39. for (int i = 0; i < n; i++) {
  40. enum known_devices dev = models_filter_name(results[i].name);
  41. if (dev == DEV_UNKNOWN) {
  42. continue;
  43. }
  44. if (dev == DEV_VOLCANO) {
  45. pos += snprintf(buff + pos, sizeof(buff) - pos, "Volcano ");
  46. } else if (dev == DEV_CRAFTY) {
  47. pos += snprintf(buff + pos, sizeof(buff) - pos, "Crafty+ ");
  48. }
  49. char info[32] = "";
  50. models_get_serial(results[i].data, results[i].data_len,
  51. info, sizeof(info));
  52. pos += snprintf(buff + pos, sizeof(buff) - pos, "%s\n", info);
  53. }
  54. if (pos == 0) {
  55. strncpy(buff, "NONE", sizeof(buff));
  56. }
  57. if (strncmp(buff, prev_buff, sizeof(buff)) == 0) {
  58. return;
  59. }
  60. strncpy(prev_buff, buff, sizeof(prev_buff));
  61. static struct text_font font = {
  62. .fontname = "fixed_10x20",
  63. .font = NULL,
  64. };
  65. if (font.font == NULL) {
  66. text_prepare_font(&font);
  67. }
  68. struct text_conf text = {
  69. .text = "",
  70. .x = 0,
  71. .y = 50,
  72. .justify = false,
  73. .alignment = MF_ALIGN_CENTER,
  74. .width = 240,
  75. .height = 240 - 80,
  76. .margin = 2,
  77. .fg = RGB_565(0xFF, 0xFF, 0xFF),
  78. .bg = RGB_565(0x00, 0x00, 0x00),
  79. .font = &font,
  80. };
  81. text.text = buff;
  82. text_draw(&text);
  83. }