Skip to content

Commit e82f38e

Browse files
committed
Tuning
1 parent d2401a2 commit e82f38e

File tree

7 files changed

+73
-90
lines changed

7 files changed

+73
-90
lines changed

wled00/FX.h

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,16 @@ typedef struct Segment {
563563

564564
// transition functions
565565
void startTransition(uint16_t dur); // transition has to start before actual segment values change
566-
void stopTransition(void); // ends transition mode by destroying transition structure
567-
void handleTransition(void);
566+
void stopTransition(void); // ends transition mode by destroying transition structure (does nothing if not in transition)
567+
inline void handleTransition(void) { if (progress() == 0xFFFFU) stopTransition(); }
568568
#ifndef WLED_DISABLE_MODE_BLEND
569569
void swapSegenv(tmpsegd_t &tmpSegD); // copies segment data into specifed buffer, if buffer is not a transition buffer, segment data is overwritten from transition buffer
570570
void restoreSegenv(tmpsegd_t &tmpSegD); // restores segment data from buffer, if buffer is not transition buffer, changed values are copied to transition buffer
571571
#endif
572-
uint16_t progress(void); // transition progression between 0-65535
573-
uint8_t currentBri(bool useCct = false); // current segment brightness/CCT (blended while in transition)
574-
uint8_t currentMode(void); // currently active effect/mode (while in transition)
575-
uint32_t currentColor(uint8_t slot); // currently active segment color (blended while in transition)
572+
uint16_t progress(void) const; // transition progression between 0-65535
573+
uint8_t currentBri(bool useCct = false) const; // current segment brightness/CCT (blended while in transition)
574+
uint8_t currentMode(void) const; // currently active effect/mode (while in transition)
575+
uint32_t currentColor(uint8_t slot) const; // currently active segment color (blended while in transition)
576576
CRGBPalette16 &loadPalette(CRGBPalette16 &tgt, uint8_t pal);
577577
void setCurrentPalette(void);
578578

@@ -587,7 +587,7 @@ typedef struct Segment {
587587
inline void setPixelColor(float i, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0, bool aa = true) { setPixelColor(i, RGBW32(r,g,b,w), aa); }
588588
inline void setPixelColor(float i, CRGB c, bool aa = true) { setPixelColor(i, RGBW32(c.r,c.g,c.b,0), aa); }
589589
#endif
590-
uint32_t getPixelColor(int i);
590+
uint32_t getPixelColor(int i) const;
591591
// 1D support functions (some implement 2D as well)
592592
void blur(uint8_t, bool smear = false);
593593
void fill(uint32_t c);
@@ -599,8 +599,8 @@ typedef struct Segment {
599599
inline void addPixelColor(int n, byte r, byte g, byte b, byte w = 0, bool fast = false) { addPixelColor(n, RGBW32(r,g,b,w), fast); }
600600
inline void addPixelColor(int n, CRGB c, bool fast = false) { addPixelColor(n, RGBW32(c.r,c.g,c.b,0), fast); }
601601
inline void fadePixelColor(uint16_t n, uint8_t fade) { setPixelColor(n, color_fade(getPixelColor(n), fade, true)); }
602-
uint32_t color_from_palette(uint16_t, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri = 255);
603-
uint32_t color_wheel(uint8_t pos);
602+
uint32_t color_from_palette(uint16_t, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri = 255) const;
603+
uint32_t color_wheel(uint8_t pos) const;
604604

605605
// 2D matrix
606606
uint16_t virtualWidth(void) const; // segment width in virtual pixels (accounts for groupping and spacing)
@@ -618,7 +618,7 @@ typedef struct Segment {
618618
inline void setPixelColorXY(float x, float y, byte r, byte g, byte b, byte w = 0, bool aa = true) { setPixelColorXY(x, y, RGBW32(r,g,b,w), aa); }
619619
inline void setPixelColorXY(float x, float y, CRGB c, bool aa = true) { setPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0), aa); }
620620
#endif
621-
uint32_t getPixelColorXY(int x, int y);
621+
uint32_t getPixelColorXY(int x, int y) const;
622622
// 2D support functions
623623
inline void blendPixelColorXY(uint16_t x, uint16_t y, uint32_t color, uint8_t blend) { setPixelColorXY(x, y, color_blend(getPixelColorXY(x,y), color, blend)); }
624624
inline void blendPixelColorXY(uint16_t x, uint16_t y, CRGB c, uint8_t blend) { blendPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0), blend); }
@@ -729,15 +729,7 @@ class WS2812FX { // 96 bytes
729729
customMappingSize(0),
730730
_lastShow(0),
731731
_segment_index(0),
732-
_mainSegment(0),
733-
_queuedChangesSegId(255),
734-
_qStart(0),
735-
_qStop(0),
736-
_qStartY(0),
737-
_qStopY(0),
738-
_qGrouping(0),
739-
_qSpacing(0),
740-
_qOffset(0)
732+
_mainSegment(0)
741733
{
742734
WS2812FX::instance = this;
743735
_mode.reserve(_modeCount); // allocate memory to prevent initial fragmentation (does not increase size())
@@ -945,14 +937,6 @@ class WS2812FX { // 96 bytes
945937

946938
uint8_t _segment_index;
947939
uint8_t _mainSegment;
948-
uint8_t _queuedChangesSegId;
949-
uint16_t _qStart, _qStop, _qStartY, _qStopY;
950-
uint8_t _qGrouping, _qSpacing;
951-
uint16_t _qOffset;
952-
/*
953-
void
954-
setUpSegmentFromQueuedChanges(void);
955-
*/
956940
};
957941

958942
extern const char JSON_mode_names[];

wled00/FX_2Dfcn.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void IRAM_ATTR Segment::setPixelColorXY(int x, int y, uint32_t col)
180180

181181
if (reverse ) x = virtualWidth() - x - 1;
182182
if (reverse_y) y = virtualHeight() - y - 1;
183-
if (transpose) { unsigned t = x; x = y; y = t; } // swap X & Y if segment transposed
183+
if (transpose) { std::swap(x,y); } // swap X & Y if segment transposed
184184

185185
x *= groupLength(); // expand to physical pixels
186186
y *= groupLength(); // expand to physical pixels
@@ -261,12 +261,12 @@ void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa)
261261
#endif
262262

263263
// returns RGBW values of pixel
264-
uint32_t IRAM_ATTR Segment::getPixelColorXY(int x, int y) {
264+
uint32_t IRAM_ATTR Segment::getPixelColorXY(int x, int y) const {
265265
if (!isActive()) return 0; // not active
266266
if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return 0; // if pixel would fall out of virtual segment just exit
267267
if (reverse ) x = virtualWidth() - x - 1;
268268
if (reverse_y) y = virtualHeight() - y - 1;
269-
if (transpose) { unsigned t = x; x = y; y = t; } // swap X & Y if segment transposed
269+
if (transpose) { std::swap(x,y); } // swap X & Y if segment transposed
270270
x *= groupLength(); // expand to physical pixels
271271
y *= groupLength(); // expand to physical pixels
272272
if (x >= width() || y >= height()) return 0;

wled00/FX_fcn.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,8 @@ void Segment::stopTransition() {
327327
}
328328
}
329329

330-
void Segment::handleTransition() {
331-
unsigned _progress = progress();
332-
if (_progress == 0xFFFFU) stopTransition();
333-
}
334-
335330
// transition progression between 0-65535
336-
uint16_t IRAM_ATTR Segment::progress() {
331+
uint16_t IRAM_ATTR Segment::progress() const {
337332
if (isInTransition()) {
338333
unsigned diff = millis() - _t->_start;
339334
if (_t->_dur > 0 && diff < _t->_dur) return diff * 0xFFFFU / _t->_dur;
@@ -412,7 +407,7 @@ void Segment::restoreSegenv(tmpsegd_t &tmpSeg) {
412407
}
413408
#endif
414409

415-
uint8_t IRAM_ATTR Segment::currentBri(bool useCct) {
410+
uint8_t IRAM_ATTR Segment::currentBri(bool useCct) const {
416411
unsigned prog = progress();
417412
if (prog < 0xFFFFU) {
418413
unsigned curBri = (useCct ? cct : (on ? opacity : 0)) * prog;
@@ -422,15 +417,15 @@ uint8_t IRAM_ATTR Segment::currentBri(bool useCct) {
422417
return (useCct ? cct : (on ? opacity : 0));
423418
}
424419

425-
uint8_t IRAM_ATTR Segment::currentMode() {
420+
uint8_t IRAM_ATTR Segment::currentMode() const {
426421
#ifndef WLED_DISABLE_MODE_BLEND
427422
unsigned prog = progress();
428423
if (modeBlending && prog < 0xFFFFU) return _t->_modeT;
429424
#endif
430425
return mode;
431426
}
432427

433-
uint32_t IRAM_ATTR Segment::currentColor(uint8_t slot) {
428+
uint32_t IRAM_ATTR Segment::currentColor(uint8_t slot) const {
434429
if (slot >= NUM_COLORS) slot = 0;
435430
#ifndef WLED_DISABLE_MODE_BLEND
436431
return isInTransition() ? color_blend(_t->_segT._colorT[slot], colors[slot], progress(), true) : colors[slot];
@@ -685,9 +680,11 @@ uint16_t IRAM_ATTR Segment::virtualLength() const {
685680
vLen = vH;
686681
break;
687682
case M12_pCorner:
688-
case M12_pArc:
689683
vLen = max(vW,vH); // get the longest dimension
690684
break;
685+
case M12_pArc:
686+
vLen = sqrt16(vH*vH + vW*vW); // use diagonal
687+
break;
691688
case M12_sPinwheel:
692689
vLen = getPinwheelLength(vW, vH);
693690
break;
@@ -730,12 +727,14 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
730727
if (i==0)
731728
setPixelColorXY(0, 0, col);
732729
else {
733-
float step = HALF_PI / (2.85f*i);
734-
for (float rad = 0.0f; rad <= HALF_PI+step/2; rad += step) {
735-
// may want to try float version as well (with or without antialiasing)
736-
int x = roundf(sin_t(rad) * i);
737-
int y = roundf(cos_t(rad) * i);
730+
float r = i;
731+
float step = HALF_PI / (2.8284f * r + 4); // we only need (PI/4)/(r/sqrt(2)+1) steps
732+
for (float rad = 0.0f; rad <= (HALF_PI/2)+step/2; rad += step) {
733+
int x = roundf(sin_t(rad) * r);
734+
int y = roundf(cos_t(rad) * r);
735+
// exploit symmetry
738736
setPixelColorXY(x, y, col);
737+
setPixelColorXY(y, x, col);
739738
}
740739
// Bresenham’s Algorithm (may not fill every pixel)
741740
//int d = 3 - (2*i);
@@ -893,7 +892,7 @@ void Segment::setPixelColor(float i, uint32_t col, bool aa)
893892
}
894893
#endif
895894

896-
uint32_t IRAM_ATTR Segment::getPixelColor(int i)
895+
uint32_t IRAM_ATTR Segment::getPixelColor(int i) const
897896
{
898897
if (!isActive()) return 0; // not active
899898
#ifndef WLED_DISABLE_2D
@@ -1059,7 +1058,7 @@ void Segment::fade_out(uint8_t rate) {
10591058
const int rows = virtualHeight(); // will be 1 for 1D
10601059

10611060
rate = (255-rate) >> 1;
1062-
float mappedRate = float(rate) +1.1f;
1061+
float mappedRate = 1.0f / (float(rate) + 1.1f);
10631062

10641063
uint32_t color = colors[1]; // SEGCOLOR(1); // target color
10651064
int w2 = W(color);
@@ -1069,15 +1068,16 @@ void Segment::fade_out(uint8_t rate) {
10691068

10701069
for (int y = 0; y < rows; y++) for (int x = 0; x < cols; x++) {
10711070
color = is2D() ? getPixelColorXY(x, y) : getPixelColor(x);
1071+
if (color == colors[1]) continue; // already at target color
10721072
int w1 = W(color);
10731073
int r1 = R(color);
10741074
int g1 = G(color);
10751075
int b1 = B(color);
10761076

1077-
int wdelta = (w2 - w1) / mappedRate;
1078-
int rdelta = (r2 - r1) / mappedRate;
1079-
int gdelta = (g2 - g1) / mappedRate;
1080-
int bdelta = (b2 - b1) / mappedRate;
1077+
int wdelta = (w2 - w1) * mappedRate;
1078+
int rdelta = (r2 - r1) * mappedRate;
1079+
int gdelta = (g2 - g1) * mappedRate;
1080+
int bdelta = (b2 - b1) * mappedRate;
10811081

10821082
// if fade isn't complete, make sure delta is at least 1 (fixes rounding issues)
10831083
wdelta += (w2 == w1) ? 0 : (w2 > w1) ? 1 : -1;
@@ -1149,7 +1149,7 @@ void Segment::blur(uint8_t blur_amount, bool smear) {
11491149
* The colours are a transition r -> g -> b -> back to r
11501150
* Inspired by the Adafruit examples.
11511151
*/
1152-
uint32_t Segment::color_wheel(uint8_t pos) {
1152+
uint32_t Segment::color_wheel(uint8_t pos) const {
11531153
if (palette) return color_from_palette(pos, false, true, 0); // perhaps "strip.paletteBlend < 2" should be better instead of "true"
11541154
uint8_t w = W(currentColor(0));
11551155
pos = 255 - pos;
@@ -1173,7 +1173,7 @@ uint32_t Segment::color_wheel(uint8_t pos) {
11731173
* @param pbri Value to scale the brightness of the returned color by. Default is 255. (no scaling)
11741174
* @returns Single color from palette
11751175
*/
1176-
uint32_t Segment::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri) {
1176+
uint32_t Segment::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri) const {
11771177
uint32_t color = gamma32(currentColor(mcol));
11781178

11791179
// default palette or no RGB support on segment

wled00/colors.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* color blend function
99
*/
1010
uint32_t color_blend(uint32_t color1, uint32_t color2, uint16_t blend, bool b16) {
11-
if(blend == 0) return color1;
11+
if (blend == 0) return color1;
1212
unsigned blendmax = b16 ? 0xFFFF : 0xFF;
13-
if(blend == blendmax) return color2;
14-
uint8_t shift = b16 ? 16 : 8;
13+
if (blend == blendmax) return color2;
14+
unsigned shift = b16 ? 16 : 8;
1515

1616
uint32_t w1 = W(color1);
1717
uint32_t r1 = R(color1);
@@ -73,22 +73,19 @@ uint32_t color_fade(uint32_t c1, uint8_t amount, bool video)
7373
uint32_t g = G(c1);
7474
uint32_t b = B(c1);
7575
uint32_t w = W(c1);
76-
if (video) {
77-
uint32_t scale = amount; // 32bit for faster calculation
78-
scaledcolor = (((r * scale) >> 8) << 16) + ((r && scale) ? 1 : 0);
79-
scaledcolor |= (((g * scale) >> 8) << 8) + ((g && scale) ? 1 : 0);
80-
scaledcolor |= ((b * scale) >> 8) + ((b && scale) ? 1 : 0);
76+
uint32_t scale = amount + !video; // 32bit for faster calculation
77+
if (video) {
78+
scaledcolor = (((r * scale) >> 8) << 16) + ((r && scale) ? 1 : 0);
79+
scaledcolor |= (((g * scale) >> 8) << 8) + ((g && scale) ? 1 : 0);
80+
scaledcolor |= ((b * scale) >> 8) + ((b && scale) ? 1 : 0);
8181
scaledcolor |= (((w * scale) >> 8) << 24) + ((w && scale) ? 1 : 0);
82-
return scaledcolor;
83-
}
84-
else {
85-
uint32_t scale = 1 + amount;
86-
scaledcolor = ((r * scale) >> 8) << 16;
82+
} else {
83+
scaledcolor = ((r * scale) >> 8) << 16;
8784
scaledcolor |= ((g * scale) >> 8) << 8;
88-
scaledcolor |= (b * scale) >> 8;
85+
scaledcolor |= (b * scale) >> 8;
8986
scaledcolor |= ((w * scale) >> 8) << 24;
90-
return scaledcolor;
9187
}
88+
return scaledcolor;
9289
}
9390

9491
void setRandomColor(byte* rgb)
@@ -140,25 +137,25 @@ CRGBPalette16 generateHarmonicRandomPalette(CRGBPalette16 &basepalette)
140137
case 1: // triadic
141138
harmonics[0] = basehue + 113 + random8(15);
142139
harmonics[1] = basehue + 233 + random8(15);
143-
harmonics[2] = basehue -7 + random8(15);
140+
harmonics[2] = basehue - 7 + random8(15);
144141
break;
145142

146143
case 2: // split-complementary
147144
harmonics[0] = basehue + 145 + random8(10);
148145
harmonics[1] = basehue + 205 + random8(10);
149-
harmonics[2] = basehue - 5 + random8(10);
146+
harmonics[2] = basehue - 5 + random8(10);
150147
break;
151148

152149
case 3: // square
153-
harmonics[0] = basehue + 85 + random8(10);
150+
harmonics[0] = basehue + 85 + random8(10);
154151
harmonics[1] = basehue + 175 + random8(10);
155152
harmonics[2] = basehue + 265 + random8(10);
156153
break;
157154

158155
case 4: // tetradic
159-
harmonics[0] = basehue + 80 + random8(20);
156+
harmonics[0] = basehue + 80 + random8(20);
160157
harmonics[1] = basehue + 170 + random8(20);
161-
harmonics[2] = basehue + random8(30)-15;
158+
harmonics[2] = basehue - 15 + random8(30);
162159
break;
163160
}
164161

@@ -384,13 +381,13 @@ bool colorFromHexString(byte* rgb, const char* in) {
384381
return true;
385382
}
386383

387-
float minf (float v, float w)
384+
static inline float minf(float v, float w)
388385
{
389386
if (w > v) return v;
390387
return w;
391388
}
392389

393-
float maxf (float v, float w)
390+
static inline float maxf(float v, float w)
394391
{
395392
if (w > v) return w;
396393
return v;

0 commit comments

Comments
 (0)