浏览代码

optional background fill for text, for splash screen

Thomas Buck 1年前
父节点
当前提交
d7f8847dab
共有 7 个文件被更改,包括 94 次插入12 次删除
  1. 4
    0
      include/image.h
  2. 9
    0
      include/lcd.h
  3. 3
    0
      include/text.h
  4. 1
    0
      src/console.c
  5. 47
    9
      src/image.c
  6. 4
    0
      src/lcd.c
  7. 26
    3
      src/text.c

+ 4
- 0
include/image.h 查看文件

@@ -19,6 +19,10 @@
19 19
 #ifndef __IMAGE_H__
20 20
 #define __IMAGE_H__
21 21
 
22
+#include "pico/stdlib.h"
23
+
24
+void image_draw(char *data, uint width, uint height);
25
+
22 26
 void draw_splash(void);
23 27
 
24 28
 #endif // __IMAGE_H__

+ 9
- 0
include/lcd.h 查看文件

@@ -19,6 +19,14 @@
19 19
 #ifndef __LCD_H__
20 20
 #define __LCD_H__
21 21
 
22
+#include <stdint.h>
23
+
24
+#define RGB_565(r, g, b) ( \
25
+      (((r) >> 3) << 11)   \
26
+    | (((g) >> 2) << 5)    \
27
+    |  ((b) >> 3)          \
28
+)
29
+
22 30
 void lcd_init(void);
23 31
 
24 32
 uint16_t lcd_get_backlight(void);
@@ -26,5 +34,6 @@ void lcd_set_backlight(uint16_t value);
26 34
 
27 35
 void lcd_clear(void);
28 36
 void lcd_write_point(uint16_t x, uint16_t y, uint32_t color);
37
+void lcd_write_rect(uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint32_t color);
29 38
 
30 39
 #endif // __LCD_H__

+ 3
- 0
include/text.h 查看文件

@@ -21,6 +21,8 @@
21 21
 
22 22
 #include "mcufont.h"
23 23
 
24
+#define TEXT_BG_NONE -1
25
+
24 26
 struct text_font {
25 27
     const char *fontname;
26 28
     //int scale; // TODO not supported - requires somewhere to store scaled versions
@@ -37,6 +39,7 @@ struct text_conf {
37 39
     int width;
38 40
     int height;
39 41
     int margin;
42
+    int bg;
40 43
 
41 44
     struct text_font *font;
42 45
 };

+ 1
- 0
src/console.c 查看文件

@@ -145,6 +145,7 @@ static void cnsl_interpret(const char *line) {
145 145
                 .width = 240,
146 146
                 .height = 240 - y_off,
147 147
                 .margin = 5,
148
+                .bg = TEXT_BG_NONE,
148 149
                 .font = &font,
149 150
             };
150 151
             text_draw(&text);

+ 47
- 9
src/image.c 查看文件

@@ -16,10 +16,9 @@
16 16
  * See <http://www.gnu.org/licenses/>.
17 17
  */
18 18
 
19
-#include "pico/stdlib.h"
20
-
21 19
 #include "config.h"
22 20
 #include "lcd.h"
21
+#include "text.h"
23 22
 #include "image.h"
24 23
 
25 24
 #pragma GCC diagnostic push
@@ -27,17 +26,56 @@
27 26
 #include "logo.h"
28 27
 #pragma GCC diagnostic pop
29 28
 
30
-void draw_splash(void) {
31
-    char *data = logo_rgb_data;
32
-    for (uint x = 0; x < logo_width; x++) {
33
-        for (uint y = 0; y < logo_height; y++) {
29
+void image_draw(char *data, uint width, uint height) {
30
+    for (uint x = 0; x < width; x++) {
31
+        for (uint y = 0; y < height; y++) {
34 32
             uint32_t pixel[3];
35 33
             HEADER_PIXEL(data, pixel);
36 34
 
37
-            uint32_t color = (pixel[0] >> 3) << 11;
38
-            color |= (pixel[1] >> 2) << 5;
39
-            color |= pixel[2] >> 3;
35
+            uint32_t color = RGB_565(pixel[0], pixel[1], pixel[2]);
40 36
             lcd_write_point(240 - x - 1, y, color);
41 37
         }
42 38
     }
43 39
 }
40
+
41
+void draw_splash(void) {
42
+    image_draw(logo_rgb_data, logo_width, logo_height);
43
+
44
+    struct text_font font_big = {
45
+        .fontname = "DejaVuSerif32",
46
+    };
47
+    text_prepare_font(&font_big);
48
+
49
+    struct text_font font_small = {
50
+        .fontname = "DejaVuSerif16",
51
+    };
52
+    text_prepare_font(&font_small);
53
+
54
+    struct text_conf text1 = {
55
+        .text = "xythobuz.de",
56
+        .x = 0,
57
+        .y = 0,
58
+        .justify = false,
59
+        .alignment = MF_ALIGN_CENTER,
60
+        .width = 240,
61
+        .height = 240,
62
+        .margin = 2,
63
+        .bg = RGB_565(0x00, 0x00, 0x00),
64
+        .font = &font_big,
65
+    };
66
+    text_draw(&text1);
67
+
68
+    struct text_conf text2 = {
69
+        .text = __DATE__ " " __TIME__,
70
+        .x = 0,
71
+        .y = 195,
72
+        .justify = false,
73
+        .alignment = MF_ALIGN_CENTER,
74
+        .width = 240,
75
+        .height = 240,
76
+        .margin = 2,
77
+        .bg = RGB_565(0x00, 0x00, 0x00),
78
+        .font = &font_small,
79
+    };
80
+    text_draw(&text2);
81
+}

+ 4
- 0
src/lcd.c 查看文件

@@ -269,3 +269,7 @@ void lcd_clear(void) {
269 269
 void lcd_write_point(uint16_t x, uint16_t y, uint32_t color) {
270 270
     st7789_draw_point(&gs_handle, x, y, color);
271 271
 }
272
+
273
+void lcd_write_rect(uint16_t left, uint16_t top, uint16_t right, uint16_t bottom, uint32_t color) {
274
+    st7789_fill_rect(&gs_handle, left, top, right, bottom, color);
275
+}

+ 26
- 3
src/text.c 查看文件

@@ -34,9 +34,7 @@ static void pixel_callback(int16_t x, int16_t y, uint8_t count, uint8_t alpha,
34 34
     if (x < 0 || x + count >= s->options->width) return;
35 35
 
36 36
     while (count--) {
37
-        uint32_t c = (alpha >> 3) << 11;
38
-        c |= (alpha >> 2) << 5;
39
-        c |= alpha >> 3;
37
+        uint32_t c = RGB_565(alpha, alpha, alpha);
40 38
         lcd_write_point(240 - y - 1, x, c);
41 39
         x++;
42 40
     }
@@ -52,6 +50,31 @@ static uint8_t character_callback(int16_t x, int16_t y, mf_char character,
52 50
 static bool line_callback(const char *line, uint16_t count, void *state) {
53 51
     state_t *s = (state_t*)state;
54 52
 
53
+    if (s->options->bg != TEXT_BG_NONE) {
54
+        int16_t width = mf_get_string_width(s->options->font->font, line, count, false) + 2 * s->options->margin;
55
+        int16_t line_height = s->options->font->font->line_height;
56
+
57
+        if (s->options->alignment == MF_ALIGN_LEFT) {
58
+            lcd_write_rect(240 - s->options->y - 1 - line_height,
59
+                           s->options->x,
60
+                           240 - s->options->y - 1,
61
+                           s->options->x + width,
62
+                           s->options->bg);
63
+        } else if (s->options->alignment == MF_ALIGN_CENTER) {
64
+            lcd_write_rect(240 - s->options->y - 1 - line_height,
65
+                           s->options->x + s->options->width / 2 - width / 2,
66
+                           240 - s->options->y - 1,
67
+                           s->options->x + s->options->width / 2 + width / 2,
68
+                           s->options->bg);
69
+        } else if (s->options->alignment == MF_ALIGN_RIGHT) {
70
+            lcd_write_rect(240 - s->options->y - 1 - line_height,
71
+                           s->options->x + s->options->width - width,
72
+                           240 - s->options->y - 1,
73
+                           s->options->x + s->options->width,
74
+                           s->options->bg);
75
+        }
76
+    }
77
+
55 78
     if (s->options->justify) {
56 79
         mf_render_justified(s->options->font->font, s->anchor + s->options->x, s->options->y,
57 80
                             s->options->width - s->options->margin * 2,

正在加载...
取消
保存