Browse Source

optional background fill for text, for splash screen

Thomas Buck 1 year ago
parent
commit
d7f8847dab
7 changed files with 94 additions and 12 deletions
  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 View File

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

+ 9
- 0
include/lcd.h View File

19
 #ifndef __LCD_H__
19
 #ifndef __LCD_H__
20
 #define __LCD_H__
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
 void lcd_init(void);
30
 void lcd_init(void);
23
 
31
 
24
 uint16_t lcd_get_backlight(void);
32
 uint16_t lcd_get_backlight(void);
26
 
34
 
27
 void lcd_clear(void);
35
 void lcd_clear(void);
28
 void lcd_write_point(uint16_t x, uint16_t y, uint32_t color);
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
 #endif // __LCD_H__
39
 #endif // __LCD_H__

+ 3
- 0
include/text.h View File

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

+ 1
- 0
src/console.c View File

145
                 .width = 240,
145
                 .width = 240,
146
                 .height = 240 - y_off,
146
                 .height = 240 - y_off,
147
                 .margin = 5,
147
                 .margin = 5,
148
+                .bg = TEXT_BG_NONE,
148
                 .font = &font,
149
                 .font = &font,
149
             };
150
             };
150
             text_draw(&text);
151
             text_draw(&text);

+ 47
- 9
src/image.c View File

16
  * See <http://www.gnu.org/licenses/>.
16
  * See <http://www.gnu.org/licenses/>.
17
  */
17
  */
18
 
18
 
19
-#include "pico/stdlib.h"
20
-
21
 #include "config.h"
19
 #include "config.h"
22
 #include "lcd.h"
20
 #include "lcd.h"
21
+#include "text.h"
23
 #include "image.h"
22
 #include "image.h"
24
 
23
 
25
 #pragma GCC diagnostic push
24
 #pragma GCC diagnostic push
27
 #include "logo.h"
26
 #include "logo.h"
28
 #pragma GCC diagnostic pop
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
             uint32_t pixel[3];
32
             uint32_t pixel[3];
35
             HEADER_PIXEL(data, pixel);
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
             lcd_write_point(240 - x - 1, y, color);
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 View File

269
 void lcd_write_point(uint16_t x, uint16_t y, uint32_t color) {
269
 void lcd_write_point(uint16_t x, uint16_t y, uint32_t color) {
270
     st7789_draw_point(&gs_handle, x, y, color);
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 View File

34
     if (x < 0 || x + count >= s->options->width) return;
34
     if (x < 0 || x + count >= s->options->width) return;
35
 
35
 
36
     while (count--) {
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
         lcd_write_point(240 - y - 1, x, c);
38
         lcd_write_point(240 - y - 1, x, c);
41
         x++;
39
         x++;
42
     }
40
     }
52
 static bool line_callback(const char *line, uint16_t count, void *state) {
50
 static bool line_callback(const char *line, uint16_t count, void *state) {
53
     state_t *s = (state_t*)state;
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
     if (s->options->justify) {
78
     if (s->options->justify) {
56
         mf_render_justified(s->options->font->font, s->anchor + s->options->x, s->options->y,
79
         mf_render_justified(s->options->font->font, s->anchor + s->options->x, s->options->y,
57
                             s->options->width - s->options->margin * 2,
80
                             s->options->width - s->options->margin * 2,

Loading…
Cancel
Save