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.

image.c 3.9KB

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