From c16462a0ce956b5a40bdfeb5db6c227c6e2a7cd7 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Fri, 30 Jun 2023 23:59:29 +0200 Subject: [PATCH] Experimental GIF support --- platformio.ini | 2 + wled00/FX.cpp | 12 +++++ wled00/FX.h | 2 +- wled00/fcn_declare.h | 11 ++++- wled00/image_loader.cpp | 104 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 wled00/image_loader.cpp diff --git a/platformio.ini b/platformio.ini index d3b71d3c..1fd4d1aa 100644 --- a/platformio.ini +++ b/platformio.ini @@ -240,6 +240,8 @@ default_partitions = tools/WLED_ESP32_4MB_1MB_FS.csv lib_deps = https://github.com/lorol/LITTLEFS.git https://github.com/pbolduc/AsyncTCP.git @ 1.2.0 + bitbank2/AnimatedGIF@^1.4.7 + pixelmatix/GifDecoder@^1.1.0 ${env.lib_deps} diff --git a/wled00/FX.cpp b/wled00/FX.cpp index f5fefd5b..2db036e8 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4189,6 +4189,16 @@ uint16_t mode_washing_machine(void) { static const char _data_FX_MODE_WASHING_MACHINE[] PROGMEM = "Washing Machine@!,!;;!"; +/* + Image effect + Draws a .gif image from filesystem on the matrix/strip +*/ +uint16_t mode_image(void) { + renderImageToSegment(SEGMENT); + return FRAMETIME; +} +static const char _data_FX_MODE_IMAGE[] PROGMEM = "Image@;;;12"; + /* Blends random colors across palette Modified, originally by Mark Kriegsman https://gist.github.com/kriegsman/1f7ccbbfa492a73c015e @@ -7734,6 +7744,8 @@ void WS2812FX::setupEffectData() { addEffect(FX_MODE_DANCING_SHADOWS, &mode_dancing_shadows, _data_FX_MODE_DANCING_SHADOWS); addEffect(FX_MODE_WASHING_MACHINE, &mode_washing_machine, _data_FX_MODE_WASHING_MACHINE); + addEffect(FX_MODE_IMAGE, &mode_image, _data_FX_MODE_IMAGE); + addEffect(FX_MODE_BLENDS, &mode_blends, _data_FX_MODE_BLENDS); addEffect(FX_MODE_TV_SIMULATOR, &mode_tv_simulator, _data_FX_MODE_TV_SIMULATOR); addEffect(FX_MODE_DYNAMIC_SMOOTH, &mode_dynamic_smooth, _data_FX_MODE_DYNAMIC_SMOOTH); diff --git a/wled00/FX.h b/wled00/FX.h index 19b1fc4a..045e3fc1 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -242,7 +242,7 @@ #define FX_MODE_CHUNCHUN 111 #define FX_MODE_DANCING_SHADOWS 112 #define FX_MODE_WASHING_MACHINE 113 -// #define FX_MODE_CANDY_CANE 114 // removed in 0.14! +#define FX_MODE_IMAGE 114 // was Candy Cane #define FX_MODE_BLENDS 115 #define FX_MODE_TV_SIMULATOR 116 #define FX_MODE_DYNAMIC_SMOOTH 117 // candidate for removal (check3 in dynamic) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index c67fdbf3..72d589d4 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -5,6 +5,8 @@ * All globally accessible functions are declared here */ +#include "FX.h" + //alexa.cpp #ifndef WLED_DISABLE_ALEXA void onAlexaChange(EspalexaDevice* dev); @@ -103,6 +105,14 @@ void onHueConnect(void* arg, AsyncClient* client); void sendHuePoll(); void onHueData(void* arg, AsyncClient* client, void *data, size_t len); +//image_loader.cpp +bool fileSeekCallback(unsigned long position); +unsigned long filePositionCallback(void); +int fileReadCallback(void); +int fileReadBlockCallback(void * buffer, int numberOfBytes); +int fileSizeCallback(void); +bool renderImageToSegment(Segment &seg); + //improv.cpp enum ImprovRPCType { Command_Wifi = 0x01, @@ -140,7 +150,6 @@ void handleIR(); #include "ESPAsyncWebServer.h" #include "src/dependencies/json/ArduinoJson-v6.h" #include "src/dependencies/json/AsyncJson-v6.h" -#include "FX.h" bool deserializeSegment(JsonObject elem, byte it, byte presetId = 0); bool deserializeState(JsonObject root, byte callMode = CALL_MODE_DIRECT_CHANGE, byte presetId = 0); diff --git a/wled00/image_loader.cpp b/wled00/image_loader.cpp new file mode 100644 index 00000000..b26f6fe1 --- /dev/null +++ b/wled00/image_loader.cpp @@ -0,0 +1,104 @@ +#include "GifDecoder.h" +#include "wled.h" + +File file; +char lastFilename[34] = "/"; +GifDecoder<64, 64, 12, true> decoder; +bool gifDecodeFailed = false; +long lastFrameDisplayTime = 0, currentFrameDelay = 0; + +bool fileSeekCallback(unsigned long position) { + return file.seek(position); +} + +unsigned long filePositionCallback(void) { + return file.position(); +} + +int fileReadCallback(void) { + return file.read(); +} + +int fileReadBlockCallback(void * buffer, int numberOfBytes) { + return file.read((uint8_t*)buffer, numberOfBytes); +} + +int fileSizeCallback(void) { + return file.size(); +} + +bool openGif(const char *filename) { + file = WLED_FS.open(filename, "r"); + + if (!file) return false; + return true; +} + +Segment* activeSeg; +uint16_t gifWidth, gifHeight; +uint16_t fillPixX, fillPixY; + +void screenClearCallback(void) { + activeSeg->fill(0); +} + +void updateScreenCallback(void) {} + +void drawPixelCallback(int16_t x, int16_t y, uint8_t red, uint8_t green, uint8_t blue) { + // simple nearest-neighbor scaling + int16_t outY = y * activeSeg->height() / gifHeight; + int16_t outX = x * activeSeg->width() / gifWidth; + // set multiple pixels if upscaling + for (int16_t i = 0; i < (activeSeg->width()+(gifWidth-1)) / gifWidth; i++) { + for (int16_t j = 0; j < (activeSeg->height()+(gifHeight-1)) / gifHeight; j++) { + activeSeg->setPixelColorXY(outX + i, outY + j, red, green, blue); + } + } +} + +// renders an image (.gif only; .bmp and .fseq to be added soon) from FS to a segment +bool renderImageToSegment(Segment &seg) { + if (!seg.name) return false; + activeSeg = &seg; + + if (strncmp(lastFilename +1, seg.name, 32) != 0) { + Serial.println("segname changed"); + strncpy(lastFilename +1, seg.name, 32); + gifDecodeFailed = false; + if (strcmp(lastFilename + strlen(lastFilename) - 4, ".gif") != 0) { + //DEBUG_PRINTLN(F("Image file not found or not a .gif")); + gifDecodeFailed = true; + return false; + } + if (file) file.close(); + Serial.print("opening gif: "); + Serial.println(openGif(lastFilename)); + if (!file) { gifDecodeFailed = true; return false; } + decoder.setScreenClearCallback(screenClearCallback); + decoder.setUpdateScreenCallback(updateScreenCallback); + decoder.setDrawPixelCallback(drawPixelCallback); + decoder.setFileSeekCallback(fileSeekCallback); + decoder.setFilePositionCallback(filePositionCallback); + decoder.setFileReadCallback(fileReadCallback); + decoder.setFileReadBlockCallback(fileReadBlockCallback); + decoder.setFileSizeCallback(fileSizeCallback); + Serial.println("Starting decoding"); + if(decoder.startDecoding() < 0) { gifDecodeFailed = true; return false; } + Serial.println("Decoding started"); + } + + if (gifDecodeFailed) return false; + if (!file) { gifDecodeFailed = true; return false; } + + if((millis() - lastFrameDisplayTime) > currentFrameDelay) { + decoder.getSize(&gifWidth, &gifHeight); + fillPixX = (seg.width()+(gifWidth-1)) / gifWidth; + fillPixY = (seg.height()+(gifHeight-1)) / gifHeight; + Serial.println("decoding frame"); + int result = decoder.decodeFrame(false); + if (result < 0) { gifDecodeFailed = true; return false; } + currentFrameDelay = decoder.getFrameDelay_ms(); + lastFrameDisplayTime = millis(); + } + return true; +} \ No newline at end of file