From 89543e927a0ba17d538e13ff13134c72d5adb5a1 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 4 Apr 2021 13:54:34 +0200 Subject: [PATCH] Added multiple ledmaps, selectable via JSON API. --- wled00/FX.h | 6 +++--- wled00/FX_fcn.cpp | 27 +++++++++++++++++++++------ wled00/json.cpp | 5 +++++ wled00/wled.h | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 3aa9180b..c8c5532b 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -603,7 +603,8 @@ class WS2812FX { setPixelColor(uint16_t n, uint32_t c), setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0), show(void), - setPixelSegment(uint8_t n); + setPixelSegment(uint8_t n), + deserializeMap(uint8_t n=0); bool isRgbw = false, @@ -835,8 +836,7 @@ class WS2812FX { void blendPixelColor(uint16_t n, uint32_t color, uint8_t blend), - startTransition(uint8_t oldBri, uint32_t oldCol, uint16_t dur, uint8_t segn, uint8_t slot), - deserializeMap(void); + startTransition(uint8_t oldBri, uint32_t oldCol, uint16_t dur, uint8_t segn, uint8_t slot); uint16_t* customMappingTable = nullptr; uint16_t customMappingSize = 0; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 73bb3caf..bb0ac665 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -995,18 +995,33 @@ bool WS2812FX::segmentsAreIdentical(Segment* a, Segment* b) //load custom mapping table from JSON file -void WS2812FX::deserializeMap(void) { - if (!WLED_FS.exists("/ledmap.json")) return; +void WS2812FX::deserializeMap(uint8_t n) { + String fileName = String("/ledmap"); + if (n) fileName += String(n); + fileName += String(".json"); + bool isFile = WLED_FS.exists(fileName); + + if (!isFile) { + // erase custom mapping if selecting nonexistent ledmap.json (n==0) + if (!n && customMappingTable != nullptr) { + customMappingSize = 0; + delete[] customMappingTable; + customMappingTable = nullptr; + } + return; + } + DynamicJsonDocument doc(JSON_BUFFER_SIZE); // full sized buffer for larger maps + DEBUG_PRINT(F("Reading LED map from ")); + DEBUG_PRINTLN(fileName); - DEBUG_PRINTLN(F("Reading LED map from /ledmap.json...")); - - if (!readObjectFromFile("/ledmap.json", nullptr, &doc)) return; //if file does not exist just exit + if (!readObjectFromFile(fileName.c_str(), nullptr, &doc)) return; //if file does not exist just exit + // erase old custom ledmap if (customMappingTable != nullptr) { + customMappingSize = 0; delete[] customMappingTable; customMappingTable = nullptr; - customMappingSize = 0; } JsonArray map = doc[F("map")]; diff --git a/wled00/json.cpp b/wled00/json.cpp index 13cc4293..9719033e 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -269,6 +269,11 @@ bool deserializeState(JsonObject root) usermods.readFromJsonState(root); + int8_t ledmap = root[F("ledmap")] | -1; + if (ledmap >= 0) { + strip.deserializeMap(ledmap); + } + int ps = root[F("psave")] | -1; if (ps > 0) { savePreset(ps, true, nullptr, root); diff --git a/wled00/wled.h b/wled00/wled.h index 1fd114bc..8027cb31 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2104031 +#define VERSION 2104041 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG