diff --git a/data/toast1.png b/data/toast1.png new file mode 100644 index 0000000..f614efb Binary files /dev/null and b/data/toast1.png differ diff --git a/include/pngfile.h b/include/pngfile.h new file mode 100644 index 0000000..3fee85b --- /dev/null +++ b/include/pngfile.h @@ -0,0 +1,17 @@ +#ifndef _INC_PNGFILE_H +#define _ING_PNGFILE_H + + +#include +#include + +#include + + +void *png_open(const char *filename, int32_t *size); +void png_close(void *handle); +int32_t png_read(PNGFILE *handle, uint8_t *buffer, int32_t length); +int32_t png_seek(PNGFILE *handle, int32_t position); + + +#endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 43cdd05..a750731 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,6 @@ platform = https://github.com/maxgerhardt/platform-raspberrypi.git board = rpipico framework = arduino - board_build.filesystem_size = 1.4m debug_build_flags = @@ -22,6 +21,7 @@ debug_build_flags = lib_deps = moononournation/GFX Library for Arduino@^1.3.8 olikraus/U8g2@^2.35.7 + bitbank2/PNGdec@^1.0.1 platform_packages = toolchain-gccarmnoneeabi@1.100301.220327 diff --git a/src/main.cpp b/src/main.cpp index 106116e..e17dfd1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,13 +9,22 @@ extern "C" { #include "sin1.h" } +#include +#include + +// png is not working, getting error 1 on working on the file +#include +#include "pngfile.h" +PNG png; + #include "btn.h" -const char name[] = "Your Name"; -uint8_t fgprog = 0; -uint8_t bgprog = 6; +const char name[] = "true"; + +uint8_t fgprog = 1; // default +uint8_t bgprog = 7; // programs @@ -74,6 +83,9 @@ void setup() pinMode(BTN_READ0, INPUT); pinMode(BTN_READ1, INPUT); + + // filesystem + LittleFS.begin(); } int16_t render_drot_printchar(const char *str, uint8_t idx, uint8_t size, uint16_t color) @@ -136,7 +148,8 @@ void render_drot_border(int8_t x_width, uint16_t text_color, uint16_t border_col } } -void render_drot_rotate(int16_t x_width, int16_t rot, int16_t out_x, int16_t out_y) +/* +int16_t render_drot_wobble(int16_t x_width, int16_t rot, uint16_t idx, uint8_t len) { int16_t x, y; int16_t nx, ny; @@ -145,10 +158,22 @@ void render_drot_rotate(int16_t x_width, int16_t rot, int16_t out_x, int16_t out uint16_t *src, *dst; uint16_t src_pxl; + uint8_t r; + r = rot >> 4; + + + // rotation + rx = cos1(rot); + rx = (out_x >> 9) + (out_x >> 11) + (out_x >> 12); + + ry = sin1(rot); + ry = (out_y >> 9) + (out_y >> 11) + (out_y >> 12); + // position int16_t start_x = out_x + 120; int16_t start_y = out_y + 120; + rot += 0x6000; // framebuffer pointers src = drot->getFramebuffer(); @@ -176,29 +201,87 @@ void render_drot_rotate(int16_t x_width, int16_t rot, int16_t out_x, int16_t out } } } +*/ -void text_print_circular(const char *str, uint8_t text_size, int16_t spacing, uint16_t fcolor, uint16_t bcolor, int16_t rot) +int16_t render_drot_rotate(int16_t x_width, int16_t rot, int16_t idx, uint8_t len) +{ + int16_t x, y; + int16_t nx, ny; + int16_t sx, sy; + + int16_t out_x, out_y; + + uint16_t *src, *dst; + uint16_t src_pxl; + + int16_t spacing; + + + // spacing + spacing = rot - LETTER_SPACING; + + // rotation + out_x = cos1(rot); + out_x = (out_x >> 9) + (out_x >> 11) + (out_x >> 12); + + out_y = sin1(rot); + out_y = (out_y >> 9) + (out_y >> 11) + (out_y >> 12); + + // position + int16_t start_x = out_x + 120; + int16_t start_y = out_y + 120; + rot += 0x6000; + + // framebuffer pointers + src = drot->getFramebuffer(); + dst = dout->getFramebuffer(); + + // rotate+copy the buffer + for (y = 0; y < ROT_SIZE; y++) { + for (x = 0; x < x_width; x++) { + src_pxl = src[x + (y * ROT_SIZE)]; + sx = x + 1 - (x_width >> 1); + sy = y - (ROT_SIZE/2) + 1; + if (src_pxl != TRANSPARENT_BG) { + nx = ((sx * cos1(rot)) - (sy * sin1(rot))) >> 15; + ny = ((sx * sin1(rot)) + (sy * cos1(rot))) >> 15; + + nx += start_x; + ny += start_y; + + if ((nx >= 0) && (nx < 240)) { + if ((ny >= 0) && (ny < 240)) { + dst[nx + (ny*240)] = src_pxl; + } + } + } + } + } + + return spacing; +} + +void text_print_standard( + const char *str, + uint8_t text_size, + uint16_t fcolor, + uint16_t bcolor, + int16_t (*render)(int16_t, int16_t, int16_t, uint8_t), + int16_t rot) { uint8_t i; - int16_t x_width; - int16_t x, y; - int16_t out_x, out_y; + int16_t x_width; if (text_size > TEXT_MAX_SIZE) text_size = TEXT_MAX_SIZE; drot->setTextSize(text_size); - for (i = 0; i < strlen(name); i++) { - x = cos1(rot); - x = (x >> 9) + (x >> 11) + (x >> 12); - - y = sin1(rot); - y = (y >> 9) + (y >> 11) + (y >> 12); - + for (i = 0; i < strlen(str); i++) { x_width = render_drot_printchar(str, i, text_size, fcolor); // render character render_drot_border(x_width, fcolor, bcolor); // add border to character - render_drot_rotate(x_width, rot + 0x6000, x, y); - rot -= spacing; + if (render) { + rot = render(x_width, rot, i, strlen(str)); + } } } @@ -308,6 +391,58 @@ void bg_radarsweep(int16_t rot, int16_t offset, uint8_t lines) } } +struct Toasters { + uint16_t x; + uint16_t y; + uint16_t speed; + uint8_t type; + uint8_t frame; +} Toasters; + +static struct Toasters toast[8]; +uint8_t toaster_count; + +int16_t png_x, png_y; + +void png_draw(PNGDRAW *pDraw) +{ + uint16_t usPixels[320]; + uint8_t usMask[320]; + + Serial.printf("Draw pos = 0,%d. size = %d x 1\n", pDraw->y, pDraw->iWidth); + png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_LITTLE_ENDIAN, 0x00000000); + png.getAlphaMask(pDraw, usMask, 1); + dout->draw16bitRGBBitmapWithMask(png_x, png_y + pDraw->y, usPixels, usMask, pDraw->iWidth, 1); +} + +void bg_toasters() +{ + int rc; + + dout->fillScreen(BLACK); + + png_x = 120; + png_y = 120; + + rc = png.open("toast1.png", png_open, png_close, png_read, png_seek, png_draw); + + if (rc == PNG_SUCCESS) { + int16_t pw = png.getWidth(); + int16_t ph = png.getHeight(); + + png_x = (240 - pw) / 2; + png_y = (240 - ph) / 2; + + rc = png.decode(NULL, 0); + + Serial.printf("errcode: %d, Draw offset: (%d, %d); image specs: (%d x %d), %d bpp, pixel type: %d\n", + rc, png_x, png_y, png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType()); + png.close(); + } else { + Serial.println("failed to open png"); + } +} + void texthue_180degree(color_rgb *fc, color_rgb *bc) { uint16_t hue; @@ -382,31 +517,53 @@ void loop() bc.r = bc.g = bc.b = 32; break; } + + case 7: { // toasters + bg_toasters(); + fc.r = fc.g = fc.b = 240; + bc.r = bc.g = bc.b = 32; + break; + } } switch (fgprog) { case 0: { // circular rotation text - trot += 420 - 69; - - text_print_circular(name, TEXT_MAX_SIZE, LETTER_SPACING, - dout->color565(fc.r, fc.g, fc.b), - dout->color565(bc.r, bc.g, bc.b), - trot); - + trot += 100; break; } - case 1: { // wobbly - + case 1: { + trot += 420 - 69; + break; } + case 2: { + trot += 888; + break; + } + case 3: { + trot += 1500; + break; + } + } + text_print_standard(name, TEXT_MAX_SIZE, + dout->color565(fc.r, fc.g, fc.b), + dout->color565(bc.r, bc.g, bc.b), + &render_drot_rotate, trot); + + /* case 2: { // sine loop - + text_print_standard(name, TEXT_MAX_SIZE, + dout->color565(fc.r, fc.g, fc.b), + dout->color565(bc.r, bc.g, bc.b), + &render_drot_wobble, trot); + } } case 3: { // wobbly + sine loop } } + */ dout->flush(); @@ -417,24 +574,24 @@ void loop() switch (btn) { case BTN_STICK_UP: { bgprog++; - bgprog %= 7; + bgprog %= 8; break; } case BTN_STICK_DN: { if (!bgprog) { - bgprog = 6; + bgprog = 7; } else bgprog--; break; } case BTN_STICK_RT: { fgprog++; - fgprog %= 1; + fgprog %= 4; break; } case BTN_STICK_LF: { if (!fgprog) { - fgprog = 0; + fgprog = 3; } else fgprog--; } } diff --git a/src/pngfile.cpp b/src/pngfile.cpp new file mode 100644 index 0000000..11f8ad2 --- /dev/null +++ b/src/pngfile.cpp @@ -0,0 +1,42 @@ +#include "pngfile.h" +#include + + +File fpng; + + +void *png_open(const char *filename, int32_t *size) +{ + fpng = LittleFS.open(filename, "r"); + + if (!fpng || fpng.isDirectory()) { + // error + } else { + // good + } + + return &fpng; +} + +void png_close(void *handle) +{ + if (fpng) fpng.close(); +} + +int32_t png_read(PNGFILE *handle, uint8_t *buffer, int32_t length) +{ + if (!fpng) return 0; + + Serial.println("png: read"); + + return fpng.read(buffer, length); +} + +int32_t png_seek(PNGFILE *handle, int32_t position) +{ + if (!fpng) return 0; + + Serial.printf("png: seek %i\n", position); + + return fpng.seek(position); +}