S&B Volcano vaporizer remote control with Pi Pico W
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

state_scan.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. static const int max_lines = 5;
  28. static int menu_off = 0;
  29. static int menu_selection = -1;
  30. static int menu_length = 0;
  31. static void text_box(const char *s) {
  32. static struct text_font font = {
  33. .fontname = "fixed_10x20",
  34. .font = NULL,
  35. };
  36. if (font.font == NULL) {
  37. text_prepare_font(&font);
  38. }
  39. struct text_conf text = {
  40. .text = "",
  41. .x = 0,
  42. .y = 50,
  43. .justify = false,
  44. .alignment = MF_ALIGN_CENTER,
  45. .width = 240,
  46. .height = 240 - 80,
  47. .margin = 2,
  48. .fg = RGB_565(0xFF, 0xFF, 0xFF),
  49. .bg = RGB_565(0x00, 0x00, 0x00),
  50. .font = &font,
  51. };
  52. // TODO clear background?!
  53. text.text = s;
  54. text_draw(&text);
  55. }
  56. static void state_scan_buttons(enum buttons btn, bool state) {
  57. if (state && (btn == BTN_LEFT)) {
  58. // TODO brightness down
  59. return;
  60. } else if (state && (btn == BTN_RIGHT)) {
  61. // TODO brightness up
  62. return;
  63. } else if (state && ((btn == BTN_ENTER) || (btn == BTN_A))) {
  64. // TODO menu_selection
  65. return;
  66. } else if ((!state) || ((btn != BTN_UP) && (btn != BTN_DOWN))) {
  67. return;
  68. }
  69. if (state && (btn == BTN_UP)) {
  70. if (menu_selection < 0) {
  71. menu_selection = menu_length - 1;
  72. } else {
  73. menu_selection -= 1;
  74. }
  75. } else if (state && (btn == BTN_DOWN)) {
  76. if (menu_selection < 0) {
  77. menu_selection = 0;
  78. } else {
  79. menu_selection += 1;
  80. }
  81. }
  82. if (menu_selection < 0) {
  83. menu_selection += menu_length;
  84. }
  85. if (menu_selection >= menu_length) {
  86. menu_selection -= menu_length;
  87. }
  88. if (menu_selection >= 0) {
  89. while (menu_selection < menu_off) {
  90. menu_off -= 1;
  91. }
  92. while (menu_selection >= (menu_off + max_lines)) {
  93. menu_off += 1;
  94. }
  95. }
  96. }
  97. void state_scan_enter(void) {
  98. buttons_callback(state_scan_buttons);
  99. ble_scan(BLE_SCAN_ON);
  100. }
  101. void state_scan_exit(void) {
  102. buttons_callback(NULL);
  103. ble_scan(BLE_SCAN_OFF);
  104. }
  105. void state_scan_run(void) {
  106. static char prev_buff[512] = {0};
  107. char buff[512] = {0};
  108. struct ble_scan_result results[BLE_MAX_SCAN_RESULTS] = {0};
  109. int n = ble_get_scan_results(results, BLE_MAX_SCAN_RESULTS);
  110. int pos = 0, devs = 0;
  111. for (int i = 0; i < n; i++) {
  112. enum known_devices dev = models_filter_name(results[i].name);
  113. if (dev == DEV_UNKNOWN) {
  114. continue;
  115. }
  116. devs++;
  117. if (((devs - 1) < menu_off)
  118. || ((devs - 1 - menu_off) >= max_lines)) {
  119. continue;
  120. }
  121. #if defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  122. if (menu_selection < 0) {
  123. #ifdef MENU_PREFER_VOLCANO
  124. if (dev == DEV_VOLCANO) {
  125. menu_selection = devs - 1;
  126. }
  127. #endif // MENU_PREFER_VOLCANO
  128. #ifdef MENU_PREFER_CRAFTY
  129. if (dev == DEV_CRAFTY) {
  130. menu_selection = devs - 1;
  131. }
  132. #endif // MENU_PREFER_CRAFTY
  133. }
  134. #endif // defined(MENU_PREFER_VOLCANO) || defined(MENU_PREFER_CRAFTY)
  135. if ((devs - 1) == menu_selection) {
  136. pos += snprintf(buff + pos, sizeof(buff) - pos, "> ");
  137. } else {
  138. pos += snprintf(buff + pos, sizeof(buff) - pos, " ");
  139. }
  140. if (dev == DEV_VOLCANO) {
  141. pos += snprintf(buff + pos, sizeof(buff) - pos, "Volcano ");
  142. } else if (dev == DEV_CRAFTY) {
  143. pos += snprintf(buff + pos, sizeof(buff) - pos, "Crafty+ ");
  144. }
  145. char info[32] = "";
  146. if (models_get_serial(results[i].data, results[i].data_len,
  147. info, sizeof(info)) < 0) {
  148. strcpy(info, "-error-");
  149. }
  150. pos += snprintf(buff + pos, sizeof(buff) - pos, "%s\n", info);
  151. }
  152. menu_length = devs;
  153. #if !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  154. if ((menu_selection < 0) && (menu_length > 0)) {
  155. menu_selection = 0;
  156. }
  157. #endif // !defined(MENU_PREFER_VOLCANO) && !defined(MENU_PREFER_CRAFTY)
  158. if (menu_length == 0) {
  159. strncpy(buff, "NONE", sizeof(buff));
  160. }
  161. if (strncmp(buff, prev_buff, sizeof(buff)) != 0) {
  162. strncpy(prev_buff, buff, sizeof(prev_buff));
  163. text_box(buff);
  164. }
  165. }