S&B Volcano vaporizer remote control with Pi Pico W
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

image.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * image.c
  3. *
  4. * Copyright (c) 2022 - 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 "lcd.h"
  22. #include "text.h"
  23. #include "lipo.h"
  24. #include "util.h"
  25. #include "wifi.h"
  26. #include "image.h"
  27. #pragma GCC diagnostic push
  28. #pragma GCC diagnostic ignored "-Wtrigraphs"
  29. #include "logo.h"
  30. #pragma GCC diagnostic pop
  31. #define BATT_INTERVAL_MS 777
  32. void image_draw(const char *data, uint width, uint height) {
  33. for (uint y = 0; y < height; y++) {
  34. for (uint x = 0; x < width; x++) {
  35. uint32_t pixel[3];
  36. HEADER_PIXEL(data, pixel);
  37. uint32_t color = RGB_565(pixel[0], pixel[1], pixel[2]);
  38. lcd_write_point(x, y, color);
  39. }
  40. }
  41. }
  42. void draw_splash(void) {
  43. image_draw(logo_rgb_data, logo_width, logo_height);
  44. struct text_font font_big = {
  45. .fontname = "DejaVuSerif32",
  46. };
  47. text_prepare_font(&font_big);
  48. struct text_font font_small = {
  49. .fontname = "DejaVuSerif16",
  50. };
  51. text_prepare_font(&font_small);
  52. struct text_conf text1 = {
  53. .text = "xythobuz.de",
  54. .x = 0,
  55. .y = 0,
  56. .justify = false,
  57. .alignment = MF_ALIGN_CENTER,
  58. .width = 240,
  59. .height = 240,
  60. .margin = 2,
  61. .fg = RGB_565(0xFF, 0xFF, 0xFF),
  62. .bg = RGB_565(0x00, 0x00, 0x00),
  63. .font = &font_big,
  64. };
  65. text_draw(&text1);
  66. struct text_conf text2 = {
  67. .text = __DATE__ " @ " __TIME__,
  68. .x = 0,
  69. .y = 195,
  70. .justify = false,
  71. .alignment = MF_ALIGN_CENTER,
  72. .width = 240,
  73. .height = 240,
  74. .margin = 2,
  75. .fg = RGB_565(0xFF, 0xFF, 0xFF),
  76. .bg = RGB_565(0x00, 0x00, 0x00),
  77. .font = &font_small,
  78. };
  79. text_draw(&text2);
  80. }
  81. void draw_battery_indicator(void) {
  82. static const float batt_warn_limit = 0.0f;
  83. static char prev_s[30] = {0};
  84. static uint32_t prev_c = 0;
  85. char s[30] = {0};
  86. float v = lipo_voltage();
  87. uint32_t c = RGB_565(0xFF, 0x00, 0x00);
  88. if (lipo_charging()) {
  89. // "Batt: 99.9% (4.20V)"
  90. snprintf(s, sizeof(s), "Batt: Charging ");
  91. c = RGB_565(0xFF, 0xFF, 0x00);
  92. } else {
  93. float percentage = lipo_percentage(v);
  94. snprintf(s, sizeof(s), "Batt: %02.1f%% (%.2fV)", percentage, v);
  95. if (percentage > batt_warn_limit) {
  96. float hue = map(percentage, batt_warn_limit, 100, 0.0, 0.333);
  97. c = from_hsv(hue, 1.0, 1.0);
  98. }
  99. }
  100. // only re-draw battery indicator when it has changed
  101. if ((strcmp(s, prev_s) == 0) && (prev_c == c)) {
  102. return;
  103. }
  104. strcpy(prev_s, s);
  105. prev_c = c;
  106. static struct text_font font = {
  107. .fontname = "fixed_10x20",
  108. .font = NULL,
  109. };
  110. if (font.font == NULL) {
  111. text_prepare_font(&font);
  112. }
  113. struct text_conf text = {
  114. .text = s,
  115. .x = 0,
  116. .y = 219,
  117. .justify = false,
  118. .alignment = MF_ALIGN_CENTER,
  119. .width = 240,
  120. .height = 240,
  121. .margin = 2,
  122. .fg = c,
  123. .bg = LCD_BLACK,
  124. .font = &font,
  125. };
  126. text_draw(&text);
  127. }
  128. void draw_wifi_indicator(void) {
  129. static char prev_s[30] = {0};
  130. char s[30] = {0};
  131. snprintf(s, sizeof(s), "WiFi: %17s", wifi_state());
  132. if (strcmp(s, prev_s) == 0) {
  133. return;
  134. }
  135. strcpy(prev_s, s);
  136. static struct text_font font = {
  137. .fontname = "fixed_10x20",
  138. .font = NULL,
  139. };
  140. if (font.font == NULL) {
  141. text_prepare_font(&font);
  142. }
  143. struct text_conf text = {
  144. .text = s,
  145. .x = 0,
  146. .y = 219 - 50,
  147. .justify = false,
  148. .alignment = MF_ALIGN_CENTER,
  149. .width = 240,
  150. .height = 240,
  151. .margin = 2,
  152. .fg = LCD_WHITE,
  153. .bg = LCD_BLACK,
  154. .font = &font,
  155. };
  156. text_draw(&text);
  157. }
  158. void battery_run(void) {
  159. static uint32_t last_run = 0;
  160. uint32_t now = to_ms_since_boot(get_absolute_time());
  161. if ((now >= (last_run + BATT_INTERVAL_MS)) || (last_run == 0)) {
  162. last_run = now;
  163. draw_battery_indicator();
  164. draw_wifi_indicator();
  165. }
  166. }