diff --git a/usermods/audioreactive/audio_reactive.h b/usermods/audioreactive/audio_reactive.h index 63fdd733..eb1f5d2b 100644 --- a/usermods/audioreactive/audio_reactive.h +++ b/usermods/audioreactive/audio_reactive.h @@ -617,7 +617,7 @@ class AudioReactive : public Usermod { bool enabled = false; bool initDone = false; bool addPalettes = false; - CRGBPalette16 *palette[MAX_PALETTES]; + int8_t palettes = 0; // variables for UDP sound sync WiFiUDP fftUdp; // UDP object for sound sync (from WiFi UDP, not Async UDP!) @@ -1632,8 +1632,18 @@ class AudioReactive : public Usermod { inputLevel = min(255,max(0,usermod[FPSTR(_inputLvl)].as())); } } + if (root.containsKey(F("rmcpal")) && root[F("rmcpal")].as()) { + // handle removal of custom palettes from JSON call so we don't break things + removeAudioPalettes(); + } } + void onStateChange(uint8_t callMode) { + if (initDone && enabled && addPalettes && palettes==0 && strip.customPalettes.size()<10) { + // if palettes were removed during JSON call re-add them + createAudioPalettes(); + } + } /* * addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object. @@ -1846,20 +1856,24 @@ class AudioReactive : public Usermod { }; void AudioReactive::removeAudioPalettes(void) { - for (int i=MAX_PALETTES-1; i>=0; i--) { - if (palette[i]) strip.customPalettes.pop_back(); - palette[i] = nullptr; + DEBUG_PRINTLN(F("Removing audio palettes.")); + while (palettes) { + strip.customPalettes.pop_back(); + DEBUG_PRINTLN(palettes); + palettes--; } + DEBUG_PRINT(F("Total # of palettes: ")); DEBUG_PRINTLN(strip.customPalettes.size()); } void AudioReactive::createAudioPalettes(void) { + DEBUG_PRINT(F("Total # of palettes: ")); DEBUG_PRINTLN(strip.customPalettes.size()); + DEBUG_PRINTLN(F("Adding audio palettes.")); for (int i=0; i=MAX_PALETTES || !palette[pal]) return; // palette does not exist + if (pal>=palettes) return; // palette does not exist uint8_t tcp[16]; // Needs to be 4 times however many colors are being used. // 3 colors = 12, 4 colors = 16, etc. @@ -1920,7 +1934,7 @@ void AudioReactive::fillAudioPalette(int pal) { tcp[14] = rgb.g; tcp[15] = rgb.b; - palette[pal]->loadDynamicGradientPalette(tcp); + strip.customPalettes[strip.customPalettes.size()-1-palettes+pal].loadDynamicGradientPalette(tcp); } // strings to reduce flash memory usage (used more than twice) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 6b9d144b..0017112e 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1752,7 +1752,7 @@ void WS2812FX::loadCustomPalettes() { if (readObjectFromFile(fileName, nullptr, &pDoc)) { JsonArray pal = pDoc[F("palette")]; - if (!pal.isNull() && pal.size()>4) { // not an empty palette (at least 2 entries) + if (!pal.isNull() && pal.size()>3) { // not an empty palette (at least 2 entries) if (pal[0].is() && pal[1].is()) { // we have an array of index & hex strings size_t palSize = MIN(pal.size(), 36); @@ -1761,7 +1761,7 @@ void WS2812FX::loadCustomPalettes() { uint8_t rgbw[] = {0,0,0,0}; tcp[ j ] = (uint8_t) pal[ i ].as(); // index colorFromHexString(rgbw, pal[i+1].as()); // will catch non-string entires - for (size_t c=0; c<3; c++) tcp[j+1+c] = rgbw[c]; // only use RGB component + for (size_t c=0; c<3; c++) tcp[j+1+c] = gamma8(rgbw[c]); // only use RGB component DEBUG_PRINTF("%d(%d) : %d %d %d\n", i, int(tcp[j]), int(tcp[j+1]), int(tcp[j+2]), int(tcp[j+3])); } } else { @@ -1769,13 +1769,15 @@ void WS2812FX::loadCustomPalettes() { palSize -= palSize % 4; // make sure size is multiple of 4 for (size_t i=0; i()<256; i+=4) { tcp[ i ] = (uint8_t) pal[ i ].as(); // index - tcp[i+1] = (uint8_t) pal[i+1].as(); // R - tcp[i+2] = (uint8_t) pal[i+2].as(); // G - tcp[i+3] = (uint8_t) pal[i+3].as(); // B + tcp[i+1] = gamma8((uint8_t) pal[i+1].as()); // R + tcp[i+2] = gamma8((uint8_t) pal[i+2].as()); // G + tcp[i+3] = gamma8((uint8_t) pal[i+3].as()); // B DEBUG_PRINTF("%d(%d) : %d %d %d\n", i, int(tcp[i]), int(tcp[i+1]), int(tcp[i+2]), int(tcp[i+3])); } } customPalettes.push_back(targetPalette.loadDynamicGradientPalette(tcp)); + } else { + DEBUG_PRINTLN(F("Wrong palette format.")); } } } else {