From ab4119a1a9bbe14efb9da70d2c8619ffbc63aa82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 28 Dec 2022 21:32:15 +0100 Subject: [PATCH] introduce bitmap struct --- main.c | 99 ++++++++++++++++++++++++++--------------------- ufb/fb_text.c | 8 ++-- ufb/framebuffer.c | 4 +- ufb/framebuffer.h | 27 +++++++++++-- 4 files changed, 84 insertions(+), 54 deletions(-) diff --git a/main.c b/main.c index 3a98a31..e3b4ae5 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,54 @@ const uint LED_PIN = 25; bi_decl(bi_program_description("OLED demo")) bi_decl(bi_1pin_with_name(LED_PIN, "Blink LED")) +static const uint8_t zir_data[] = { + // levo + 0b10000000, + 0b01000000, + 0b00100000, + 0b00010000, + 0b00001000, + 0b00000100, + 0b00000010, + 0b00011001, + 0b00011111, + 0b11111000, + 0b00011111, + 0b00001000, + // + 0b10000000, + 0b01000000, + 0b00100000, + 0b00010000, + 0b00001000, + 0b00000100, + 0b00000010, + 0b00000001, + 0b10000000, + 0b11111111, + 0b00100000, + 0b00010000, + // + 0b00110000, + 0b10001000, + 0b11111000, + 0b00011000, + 0b11111000, + 0b10011000, + 0b00011000, + 0b10011000, + 0b11111000, + 0b00011111, + 0b11111000, + 0b10000000, +}; + +static const fb_bitmap_t zir = { + .width = 12, + .height = 24, + .data = zir_data +}; + int main() { stdio_init_all(); @@ -25,55 +73,16 @@ int main() { oled_init(); -#define ZIR_W 12 -#define ZIR_H 24 - const uint8_t zirafa[(ZIR_H/8) * ZIR_W] = { - // levo - 0b10000000, - 0b01000000, - 0b00100000, - 0b00010000, - 0b00001000, - 0b00000100, - 0b00000010, - 0b00011001, - 0b00011111, - 0b11111000, - 0b00011111, - 0b00001000, - // - 0b10000000, - 0b01000000, - 0b00100000, - 0b00010000, - 0b00001000, - 0b00000100, - 0b00000010, - 0b00000001, - 0b10000000, - 0b11111111, - 0b00100000, - 0b00010000, - // - 0b00110000, - 0b10001000, - 0b11111000, - 0b00011000, - 0b11111000, - 0b10011000, - 0b00011000, - 0b10011000, - 0b11111000, - 0b00011111, - 0b11111000, - 0b10000000, - }; - fb_clear(); - fb_bitmap(5, 5, ZIR_W, ZIR_H, zirafa, 1); + + fb_bitmap(5, 5, &zir); + fb_bitmap(20, 5, &zir); + fb_text(40, 35, "MEOW", FONT_DOUBLE, 1); fb_text(50, 20, "meow", FONT_DOUBLE, 1); + fb_frame(0, 0, 128, 64, 2, 1); + fb_blit(); while (1) { diff --git a/ufb/fb_text.c b/ufb/fb_text.c index 8b5da7b..e686120 100644 --- a/ufb/fb_text.c +++ b/ufb/fb_text.c @@ -44,18 +44,18 @@ void fb_text_P_or_RAM(fbpos_t x, fbpos_t y, const char *text, uint8_t flags, fbc // no double, using normal format #if IS_AVR - fb_bitmap_P(x, y, symw, symh, data, color); + fb_bitmap_ex_P(x, y, symw, symh, data, color); #else - fb_bitmap(x, y, symw, symh, data, color); + fb_bitmap_ex(x, y, symw, symh, data, color); #endif if (flags & FONT_BOLD) { x++; #if IS_AVR - fb_bitmap_P(x, y, symw, symh, data, color); + fb_bitmap_ex_P(x, y, symw, symh, data, color); #else - fb_bitmap(x, y, symw, symh, data, color); + fb_bitmap_ex(x, y, symw, symh, data, color); #endif x += symw+2; diff --git a/ufb/framebuffer.c b/ufb/framebuffer.c index 0b938bd..33b37bd 100644 --- a/ufb/framebuffer.c +++ b/ufb/framebuffer.c @@ -230,9 +230,9 @@ void fb_circle(fbpos_t x0, fbpos_t y0, fbpos_t radius, uint8_t thickness, fbcolo } #if IS_AVR -void fb_bitmap_P(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color) +void fb_bitmap_ex_P(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color) #else -void fb_bitmap(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color) +void fb_bitmap_ex(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color) #endif { if (x >= FBW || y >= FBH) { return; } diff --git a/ufb/framebuffer.h b/ufb/framebuffer.h index c0c7c35..26752f4 100644 --- a/ufb/framebuffer.h +++ b/ufb/framebuffer.h @@ -13,10 +13,20 @@ #define FBSET 0xFF #define FBCLEAR 0x00 +/// Width/height type typedef uint16_t fbsize_t; +/// Position type (X/Y) typedef uint8_t fbpos_t; +/// Color type typedef uint8_t fbcolor_t; +/// Bitmap struct +typedef struct { + const uint8_t *data; + fbsize_t width; + fbsize_t height; +} fb_bitmap_t; + #define FB_LEN ((FBH / 8) * FBW) /// Framebuffer backing array. @@ -69,11 +79,22 @@ void fb_rect(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, fbcolor_t color); /// Draw a frame (unfilled rect) void fb_frame(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, fbpos_t thickness, fbcolor_t color); -#if IS_AVR /// Draw a bitmap from progmem -void fb_bitmap_P(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color); +#if IS_AVR + void fb_bitmap_ex_P(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color); +#else + void fb_bitmap_ex(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color); +#endif + +/// Draw a bitmap using the bitmap struct +#if IS_AVR + static inline void fb_bitmap_P(fbpos_t x, fbpos_t y, const fb_bitmap_t *bitmap) { + fb_bitmap_ex_P(x, y, bitmap->width, bitmap->height, bitmap->data, 1); + } #else -void fb_bitmap(fbpos_t x, fbpos_t y, fbpos_t w, fbpos_t h, const uint8_t *map, fbcolor_t color); + static inline void fb_bitmap(fbpos_t x, fbpos_t y, const fb_bitmap_t *bitmap) { + fb_bitmap_ex(x, y, bitmap->width, bitmap->height, bitmap->data, 1); + } #endif /// Draw a circle