diff --git a/README.md b/README.md index ff653bc..120a8e2 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,4 @@ gfx_draw_string(&canvas, font, 0, 16, "Hello", false); st7567_update_screen(canvas.ctx); ``` -Would be good to add a functionality to draw some bitmaps and some included default fonts. Cozette, Spleen, Terminus, those tiny ones from JP guy \ No newline at end of file +Some included default fonts. Cozette, Spleen, Terminus \ No newline at end of file diff --git a/esp-gfx/gfx.c b/esp-gfx/gfx.c index 667af53..3f3e1ff 100644 --- a/esp-gfx/gfx.c +++ b/esp-gfx/gfx.c @@ -1,6 +1,6 @@ #include "gfx.h" -#include +#include void gfx_draw_char(const gfx_canvas_t *canvas, const GFXfont *f, const bool inverted, const int16_t x, const int16_t y, const char c) { void (*draw_cb)(void* ctx, uint8_t x, uint8_t y) = inverted ? canvas->clear_pixel_callback : canvas->draw_pixel_callback; @@ -90,3 +90,14 @@ void gfx_fill_box(const gfx_canvas_t *canvas, const uint8_t pos_x, const uint8_t for (uint8_t y = pos_y; y < pos_y + size_y; y++) draw_cb(canvas->ctx, x, y); } + +void gfx_draw_bitmap(const gfx_canvas_t *canvas, const uint8_t pos_x, const uint8_t pos_y, const gfx_bitmap_t bmp, const bool inverted) { + void (*draw_cb)(void* ctx, uint8_t x, uint8_t y) = inverted ? canvas->clear_pixel_callback : canvas->draw_pixel_callback; + for (uint16_t i = 0; i < (uint16_t)(bmp.height * bmp.width / 8); i++) { + const uint8_t x = (i * 8) % bmp.width; + const uint8_t y = i / (bmp.width / 8); + for (uint8_t j = 0; j < 8; j++) + if (bmp.data[i] & (1 << j)) + draw_cb(canvas->ctx, pos_x + x + j, pos_y + y); + } +} \ No newline at end of file diff --git a/esp-gfx/gfx.h b/esp-gfx/gfx.h index 2d63f9c..21210e9 100644 --- a/esp-gfx/gfx.h +++ b/esp-gfx/gfx.h @@ -21,6 +21,12 @@ typedef struct { uint8_t yAdvance; } GFXfont; +typedef struct { + uint8_t* data; + uint8_t width; + uint8_t height; +} gfx_bitmap_t; + /** * @brief Canvas struct for the gfx library * Functions take the context and x,y coordinates of a pixel @@ -41,3 +47,4 @@ int16_t gfx_draw_string_centered(const gfx_canvas_t *canvas, const GFXfont *f, i void gfx_draw_line(const gfx_canvas_t *canvas, uint8_t start_x, uint8_t start_y, uint8_t end_x, uint8_t end_y, bool inverted); void gfx_draw_box(const gfx_canvas_t *canvas, uint8_t pos_x, uint8_t pos_y, uint8_t size_x, uint8_t size_y, bool inverted); void gfx_fill_box(const gfx_canvas_t *canvas, uint8_t pos_x, uint8_t pos_y, uint8_t size_x, uint8_t size_y, bool inverted); +void gfx_draw_bitmap(const gfx_canvas_t *canvas, uint8_t pos_x, uint8_t pos_y, gfx_bitmap_t bmp, bool inverted);