Skip to content

Commit 529edfc

Browse files
DedeHaiblazoncek
andauthored
"unrestricted" number of custom palettes (#4932)
- allow more than 10 custom palettes - move palettes into CPP file - Fix for minimizing cpal.htm (saves 2k of flash) - shortened names in cpal, saves about 400 bytes of Flash after packing - removed async from common.js loading to prevent errors on page loads if the file is not cached - restricted nubmer of user palettes on ESP8266 to 10 - unrestricted number of user palettes on all other platforms (total max palettes: 256) - added a warning when adding more than 10 palettes to let the user decide to risk it - Bugfixes in palette enumeration, fixed AR palette adding - AR palettes are now also added if there are more than 10 custom palettes Co-authored-by: Blaž Kristan <blaz@kristan-sp.si>
1 parent 4b1b0fe commit 529edfc

24 files changed

+397
-477
lines changed

tools/cdata.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async function writeHtmlGzipped(sourceFile, resultFile, page) {
143143
console.info("Minified and compressed " + sourceFile + " from " + originalLength + " to " + result.length + " bytes");
144144
const array = hexdump(result);
145145
let src = singleHeader;
146-
src += `const uint16_t PAGE_${page}_L = ${result.length};\n`;
146+
src += `const uint16_t PAGE_${page}_length = ${result.length};\n`;
147147
src += `const uint8_t PAGE_${page}[] PROGMEM = {\n${array}\n};\n\n`;
148148
console.info("Writing " + resultFile);
149149
fs.writeFileSync(resultFile, src);
@@ -244,9 +244,22 @@ if (isAlreadyBuilt("wled00/data") && process.argv[2] !== '--force' && process.ar
244244

245245
writeHtmlGzipped("wled00/data/index.htm", "wled00/html_ui.h", 'index');
246246
writeHtmlGzipped("wled00/data/pixart/pixart.htm", "wled00/html_pixart.h", 'pixart');
247-
writeHtmlGzipped("wled00/data/cpal/cpal.htm", "wled00/html_cpal.h", 'cpal');
247+
//writeHtmlGzipped("wled00/data/cpal/cpal.htm", "wled00/html_cpal.h", 'cpal');
248248
writeHtmlGzipped("wled00/data/pxmagic/pxmagic.htm", "wled00/html_pxmagic.h", 'pxmagic');
249249

250+
writeChunks(
251+
"wled00/data/cpal",
252+
[
253+
{
254+
file: "cpal.htm",
255+
name: "PAGE_cpal",
256+
method: "gzip",
257+
filter: "html-minify"
258+
}
259+
],
260+
"wled00/html_cpal.h"
261+
);
262+
250263
writeChunks(
251264
"wled00/data",
252265
[

usermods/audioreactive/audio_reactive.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ void AudioReactive::createAudioPalettes(void) {
19811981
if (palettes) return;
19821982
DEBUG_PRINTLN(F("Adding audio palettes."));
19831983
for (int i=0; i<MAX_PALETTES; i++)
1984-
if (customPalettes.size() < 10) {
1984+
if (customPalettes.size() < WLED_MAX_CUSTOM_PALETTES) {
19851985
customPalettes.push_back(CRGBPalette16(CRGB(BLACK)));
19861986
palettes++;
19871987
DEBUG_PRINTLN(palettes);

wled00/FX_2Dfcn.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
Parts of the code adapted from WLED Sound Reactive
99
*/
1010
#include "wled.h"
11-
#include "palettes.h"
1211

1312
// setUpMatrix() - constructs ledmap array from matrix of panels with WxH pixels
1413
// this converts physical (possibly irregular) LED arrangement into well defined

wled00/FX_fcn.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
#include "wled.h"
1313
#include "FXparticleSystem.h" // TODO: better define the required function (mem service) in FX.h?
14-
#include "palettes.h"
1514

1615
/*
1716
Custom per-LED mapping has moved!
@@ -226,8 +225,12 @@ void Segment::resetIfRequired() {
226225
}
227226

228227
CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) {
229-
if (pal < 245 && pal > GRADIENT_PALETTE_COUNT+13) pal = 0;
230-
if (pal > 245 && (customPalettes.size() == 0 || 255U-pal > customPalettes.size()-1)) pal = 0;
228+
// there is one randomy generated palette (1) followed by 4 palettes created from segment colors (2-5)
229+
// those are followed by 7 fastled palettes (6-12) and 59 gradient palettes (13-71)
230+
// then come the custom palettes (255,254,...) growing downwards from 255 (255 being 1st custom palette)
231+
// palette 0 is a varying palette depending on effect and may be replaced by segment's color if so
232+
// instructed in color_from_palette()
233+
if (pal > FIXED_PALETTE_COUNT && pal <= 255-customPalettes.size()) pal = 0; // out of bounds palette
231234
//default palette. Differs depending on effect
232235
if (pal == 0) pal = _default_palette; // _default_palette is set in setMode()
233236
switch (pal) {
@@ -263,13 +266,13 @@ CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) {
263266
}
264267
break;}
265268
default: //progmem palettes
266-
if (pal>245) {
269+
if (pal > 255 - customPalettes.size()) {
267270
targetPalette = customPalettes[255-pal]; // we checked bounds above
268-
} else if (pal < 13) { // palette 6 - 12, fastled palettes
269-
targetPalette = *fastledPalettes[pal-6];
271+
} else if (pal < DYNAMIC_PALETTE_COUNT+FASTLED_PALETTE_COUNT+1) { // palette 6 - 12, fastled palettes
272+
targetPalette = *fastledPalettes[pal-DYNAMIC_PALETTE_COUNT-1];
270273
} else {
271274
byte tcp[72];
272-
memcpy_P(tcp, (byte*)pgm_read_dword(&(gGradientPalettes[pal-13])), 72);
275+
memcpy_P(tcp, (byte*)pgm_read_dword(&(gGradientPalettes[pal-(DYNAMIC_PALETTE_COUNT+FASTLED_PALETTE_COUNT)-1])), 72);
273276
targetPalette.loadDynamicGradientPalette(tcp);
274277
}
275278
break;
@@ -573,8 +576,7 @@ Segment &Segment::setMode(uint8_t fx, bool loadDefaults) {
573576
}
574577

575578
Segment &Segment::setPalette(uint8_t pal) {
576-
if (pal < 245 && pal > GRADIENT_PALETTE_COUNT+13) pal = 0; // built in palettes
577-
if (pal > 245 && (customPalettes.size() == 0 || 255U-pal > customPalettes.size()-1)) pal = 0; // custom palettes
579+
if (pal <= 255-customPalettes.size() && pal > FIXED_PALETTE_COUNT) pal = 0; // not built in palette or custom palette
578580
if (pal != palette) {
579581
//DEBUG_PRINTF_P(PSTR("- Starting palette transition: %d\n"), pal);
580582
startTransition(strip.getTransition(), blendingStyle != BLEND_STYLE_FADE); // start transition prior to change (no need to copy segment)

wled00/colors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void loadCustomPalettes() {
252252
byte tcp[72]; //support gradient palettes with up to 18 entries
253253
CRGBPalette16 targetPalette;
254254
customPalettes.clear(); // start fresh
255-
for (int index = 0; index<10; index++) {
255+
for (int index = 0; index < WLED_MAX_CUSTOM_PALETTES; index++) {
256256
char fileName[32];
257257
sprintf_P(fileName, PSTR("/palette%d.json"), index);
258258

wled00/colors.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ CRGBPalette16 generateHarmonicRandomPalette(const CRGBPalette16 &basepalette);
123123
CRGBPalette16 generateRandomPalette();
124124
void loadCustomPalettes();
125125
extern std::vector<CRGBPalette16> customPalettes;
126-
inline size_t getPaletteCount() { return 13 + GRADIENT_PALETTE_COUNT + customPalettes.size(); }
126+
inline size_t getPaletteCount() { return FIXED_PALETTE_COUNT + customPalettes.size(); }
127127
inline uint32_t colorFromRgbw(byte* rgbw) { return uint32_t((byte(rgbw[3]) << 24) | (byte(rgbw[0]) << 16) | (byte(rgbw[1]) << 8) | (byte(rgbw[2]))); }
128128
void hsv2rgb(const CHSV32& hsv, uint32_t& rgb);
129129
void colorHStoRGB(uint16_t hue, byte sat, byte* rgb);
@@ -141,4 +141,8 @@ void setRandomColor(byte* rgb);
141141

142142
[[gnu::hot, gnu::pure]] uint32_t color_fade(uint32_t c1, uint8_t amount, bool video = false);
143143

144+
// palettes
145+
extern const TProgmemRGBPalette16* const fastledPalettes[];
146+
extern const uint8_t* const gGradientPalettes[];
144147
#endif
148+

wled00/const.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
* Readability defines and their associated numerical values + compile-time constants
77
*/
88

9-
#define GRADIENT_PALETTE_COUNT 59
9+
constexpr size_t FASTLED_PALETTE_COUNT = 7; // = sizeof(fastledPalettes) / sizeof(fastledPalettes[0]);
10+
constexpr size_t GRADIENT_PALETTE_COUNT = 59; // = sizeof(gGradientPalettes) / sizeof(gGradientPalettes[0]);
11+
constexpr size_t DYNAMIC_PALETTE_COUNT = 5; // 1-5 are dynamic palettes (1=random,2=primary,3=primary+secondary,4=primary+secondary+tertiary,5=primary+secondary(+tertiary if not black)
12+
constexpr size_t FIXED_PALETTE_COUNT = DYNAMIC_PALETTE_COUNT + FASTLED_PALETTE_COUNT + GRADIENT_PALETTE_COUNT; // total number of fixed palettes
13+
#ifndef ESP8266
14+
#define WLED_MAX_CUSTOM_PALETTES (255 - FIXED_PALETTE_COUNT) // allow up to 255 total palettes, user is warned about stability issues when adding more than 10
15+
#else
16+
#define WLED_MAX_CUSTOM_PALETTES 10 // ESP8266: limit custom palettes to 10
17+
#endif
1018

1119
// You can define custom product info from build flags.
1220
// This is useful to allow API consumer to identify what type of WLED version

0 commit comments

Comments
 (0)