Skip to content

Commit cec67d8

Browse files
committed
Const and 2D box blur
- added 2D blur
1 parent db5e66a commit cec67d8

File tree

4 files changed

+205
-115
lines changed

4 files changed

+205
-115
lines changed

wled00/FX.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,15 +1226,18 @@ uint16_t mode_fireworks() {
12261226
}
12271227
SEGMENT.fade_out(128);
12281228

1229-
bool valid1 = (SEGENV.aux0 < width*height);
1230-
bool valid2 = (SEGENV.aux1 < width*height);
12311229
uint8_t x = SEGENV.aux0%width, y = SEGENV.aux0/width; // 2D coordinates stored in upper and lower byte
1232-
uint32_t sv1 = 0, sv2 = 0;
1233-
if (valid1) sv1 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(x, y) : SEGMENT.getPixelColor(SEGENV.aux0); // get spark color
1234-
if (valid2) sv2 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(x, y) : SEGMENT.getPixelColor(SEGENV.aux1);
1235-
if (!SEGENV.step) SEGMENT.blur(16);
1236-
if (valid1) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, sv1); else SEGMENT.setPixelColor(SEGENV.aux0, sv1); } // restore spark color after blur
1237-
if (valid2) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, sv2); else SEGMENT.setPixelColor(SEGENV.aux1, sv2); } // restore old spark color after blur
1230+
if (!SEGENV.step) {
1231+
// fireworks mode (blur flares)
1232+
bool valid1 = (SEGENV.aux0 < width*height);
1233+
bool valid2 = (SEGENV.aux1 < width*height);
1234+
uint32_t sv1 = 0, sv2 = 0;
1235+
if (valid1) sv1 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(x, y) : SEGMENT.getPixelColor(SEGENV.aux0); // get spark color
1236+
if (valid2) sv2 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(x, y) : SEGMENT.getPixelColor(SEGENV.aux1);
1237+
SEGMENT.blur(16); // used in mode_rain()
1238+
if (valid1) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, sv1); else SEGMENT.setPixelColor(SEGENV.aux0, sv1); } // restore spark color after blur
1239+
if (valid2) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, sv2); else SEGMENT.setPixelColor(SEGENV.aux1, sv2); } // restore old spark color after blur
1240+
}
12381241

12391242
for (int i=0; i<max(1, width/20); i++) {
12401243
if (random8(129 - (SEGMENT.intensity >> 1)) == 0) {
@@ -1261,7 +1264,7 @@ uint16_t mode_rain() {
12611264
SEGENV.step += FRAMETIME;
12621265
if (SEGENV.call && SEGENV.step > SPEED_FORMULA_L) {
12631266
SEGENV.step = 1;
1264-
if (strip.isMatrix) {
1267+
if (SEGMENT.is2D()) {
12651268
//uint32_t ctemp[width];
12661269
//for (int i = 0; i<width; i++) ctemp[i] = SEGMENT.getPixelColorXY(i, height-1);
12671270
SEGMENT.move(6, 1, true); // move all pixels down
@@ -3651,7 +3654,7 @@ uint16_t mode_exploding_fireworks(void)
36513654
else SEGMENT.setPixelColor(int(sparks[i].posX) ? rows - int(sparks[i].pos) - 1 : int(sparks[i].pos), c.red, c.green, c.blue);
36523655
}
36533656
}
3654-
SEGMENT.blur(16);
3657+
if (SEGMENT.check3) SEGMENT.blur(16);
36553658
*dying_gravity *= .8f; // as sparks burn out they fall slower
36563659
} else {
36573660
SEGENV.aux0 = 6 + random8(10); //wait for this many frames
@@ -3666,7 +3669,7 @@ uint16_t mode_exploding_fireworks(void)
36663669
return FRAMETIME;
36673670
}
36683671
#undef MAX_SPARKS
3669-
static const char _data_FX_MODE_EXPLODING_FIREWORKS[] PROGMEM = "Fireworks 1D@Gravity,Firing side;!,!;!;12;pal=11,ix=128";
3672+
static const char _data_FX_MODE_EXPLODING_FIREWORKS[] PROGMEM = "Fireworks 1D@Gravity,Firing side,,,,,,Blur;!,!;!;12;pal=11,ix=128";
36703673

36713674

36723675
/*
@@ -4890,11 +4893,11 @@ uint16_t mode_2DBlackHole(void) { // By: Stepko https://editor.soulma
48904893
// central white dot
48914894
SEGMENT.setPixelColorXY(cols/2, rows/2, WHITE);
48924895
// blur everything a bit
4893-
SEGMENT.blur(cols*rows > 100 ? 16 : 0);
4896+
if (SEGMENT.check3) SEGMENT.blur(16, cols*rows < 100);
48944897

48954898
return FRAMETIME;
48964899
} // mode_2DBlackHole()
4897-
static const char _data_FX_MODE_2DBLACKHOLE[] PROGMEM = "Black Hole@Fade rate,Outer Y freq.,Outer X freq.,Inner X freq.,Inner Y freq.,Solid;!;!;2;pal=11";
4900+
static const char _data_FX_MODE_2DBLACKHOLE[] PROGMEM = "Black Hole@Fade rate,Outer Y freq.,Outer X freq.,Inner X freq.,Inner Y freq.,Solid,,Blur;!;!;2;pal=11";
48984901

48994902

49004903
////////////////////////////
@@ -5636,7 +5639,7 @@ uint16_t mode_2DPulser(void) { // By: ldirko https://edi
56365639
int y = map((sin8(a * 5) + sin8(a * 4) + sin8(a * 2)), 0, 765, rows-1, 0);
56375640
SEGMENT.setPixelColorXY(x, y, ColorFromPalette(SEGPALETTE, map(y, 0, rows-1, 0, 255), 255, LINEARBLEND));
56385641

5639-
SEGMENT.blur(1 + (SEGMENT.intensity>>4));
5642+
SEGMENT.blur(SEGMENT.intensity>>4);
56405643

56415644
return FRAMETIME;
56425645
} // mode_2DPulser()
@@ -6219,7 +6222,7 @@ uint16_t mode_2Ddriftrose(void) {
62196222
uint32_t y = (CY + (cos_t(angle) * (beatsin8(i, 0, L*2)-L))) * 255.f;
62206223
SEGMENT.wu_pixel(x, y, CHSV(i * 10, 255, 255));
62216224
}
6222-
SEGMENT.blur((SEGMENT.intensity>>4)+1);
6225+
SEGMENT.blur(SEGMENT.intensity>>4);
62236226

62246227
return FRAMETIME;
62256228
}
@@ -6475,11 +6478,11 @@ uint16_t mode_2DWaverly(void) {
64756478
SEGMENT.addPixelColorXY((cols - 1) - i, (rows - 1) - j, ColorFromPalette(SEGPALETTE, map(j, 0, thisMax, 250, 0), 255, LINEARBLEND));
64766479
}
64776480
}
6478-
SEGMENT.blur(cols*rows > 100 ? 16 : 0);
6481+
if (SEGMENT.check3) SEGMENT.blur(16, cols*rows < 100);
64796482

64806483
return FRAMETIME;
64816484
} // mode_2DWaverly()
6482-
static const char _data_FX_MODE_2DWAVERLY[] PROGMEM = "Waverly@Amplification,Sensitivity;;!;2v;ix=64,si=0"; // Beatsin
6485+
static const char _data_FX_MODE_2DWAVERLY[] PROGMEM = "Waverly@Amplification,Sensitivity,,,,,Blur;;!;2v;ix=64,si=0"; // Beatsin
64836486

64846487
#endif // WLED_DISABLE_2D
64856488

wled00/FX.h

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ typedef struct Segment {
636636
inline void addPixelColorXY(int x, int y, byte r, byte g, byte b, byte w = 0, bool fast = false) { addPixelColorXY(x, y, RGBW32(r,g,b,w), fast); }
637637
inline void addPixelColorXY(int x, int y, CRGB c, bool fast = false) { addPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0), fast); }
638638
inline void fadePixelColorXY(uint16_t x, uint16_t y, uint8_t fade) { setPixelColorXY(x, y, color_fade(getPixelColorXY(x,y), fade, true)); }
639-
void box_blur(uint16_t i, bool vertical, fract8 blur_amount); // 1D box blur (with weight)
639+
void box_blur(unsigned r = 1U, bool smear = false); // 2D box blur
640+
void blur2D(uint8_t blur_amount, bool smear = false);
640641
void blurRow(uint32_t row, fract8 blur_amount, bool smear = false);
641642
void blurCol(uint32_t col, fract8 blur_amount, bool smear = false);
642643
void moveX(int8_t delta, bool wrap = false);
@@ -666,14 +667,15 @@ typedef struct Segment {
666667
inline void setPixelColorXY(float x, float y, byte r, byte g, byte b, byte w = 0, bool aa = true) { setPixelColor(x, RGBW32(r,g,b,w), aa); }
667668
inline void setPixelColorXY(float x, float y, CRGB c, bool aa = true) { setPixelColor(x, RGBW32(c.r,c.g,c.b,0), aa); }
668669
#endif
669-
inline uint32_t getPixelColorXY(uint16_t x, uint16_t y) { return getPixelColor(x); }
670+
inline uint32_t getPixelColorXY(int x, int y) { return getPixelColor(x); }
670671
inline void blendPixelColorXY(uint16_t x, uint16_t y, uint32_t c, uint8_t blend) { blendPixelColor(x, c, blend); }
671672
inline void blendPixelColorXY(uint16_t x, uint16_t y, CRGB c, uint8_t blend) { blendPixelColor(x, RGBW32(c.r,c.g,c.b,0), blend); }
672673
inline void addPixelColorXY(int x, int y, uint32_t color, bool fast = false) { addPixelColor(x, color, fast); }
673674
inline void addPixelColorXY(int x, int y, byte r, byte g, byte b, byte w = 0, bool fast = false) { addPixelColor(x, RGBW32(r,g,b,w), fast); }
674675
inline void addPixelColorXY(int x, int y, CRGB c, bool fast = false) { addPixelColor(x, RGBW32(c.r,c.g,c.b,0), fast); }
675676
inline void fadePixelColorXY(uint16_t x, uint16_t y, uint8_t fade) { fadePixelColor(x, fade); }
676-
inline void box_blur(uint16_t i, bool vertical, fract8 blur_amount) {}
677+
inline void box_blur(unsigned i, bool vertical, fract8 blur_amount) {}
678+
inline void blur2D(uint8_t blur_amount, bool smear = false) {}
677679
inline void blurRow(uint32_t row, fract8 blur_amount, bool smear = false) {}
678680
inline void blurCol(uint32_t col, fract8 blur_amount, bool smear = false) {}
679681
inline void moveX(int8_t delta, bool wrap = false) {}
@@ -800,56 +802,55 @@ class WS2812FX { // 96 bytes
800802
bool
801803
paletteFade,
802804
checkSegmentAlignment(void),
803-
hasRGBWBus(void),
804-
hasCCTBus(void),
805-
// return true if the strip is being sent pixel updates
806-
isUpdating(void),
805+
hasRGBWBus(void) const,
806+
hasCCTBus(void) const,
807+
isUpdating(void) const, // return true if the strip is being sent pixel updates
807808
deserializeMap(uint8_t n=0);
808809

809-
inline bool isServicing(void) { return _isServicing; } // returns true if strip.service() is executing
810-
inline bool hasWhiteChannel(void) { return _hasWhiteChannel; } // returns true if strip contains separate white chanel
811-
inline bool isOffRefreshRequired(void) { return _isOffRefreshRequired; } // returns true if strip requires regular updates (i.e. TM1814 chipset)
812-
inline bool isSuspended(void) { return _suspend; } // returns true if strip.service() execution is suspended
813-
inline bool needsUpdate(void) { return _triggered; } // returns true if strip received a trigger() request
810+
inline bool isServicing(void) const { return _isServicing; } // returns true if strip.service() is executing
811+
inline bool hasWhiteChannel(void) const { return _hasWhiteChannel; } // returns true if strip contains separate white chanel
812+
inline bool isOffRefreshRequired(void) const { return _isOffRefreshRequired; } // returns true if strip requires regular updates (i.e. TM1814 chipset)
813+
inline bool isSuspended(void) const { return _suspend; } // returns true if strip.service() execution is suspended
814+
inline bool needsUpdate(void) const { return _triggered; } // returns true if strip received a trigger() request
814815

815816
uint8_t
816817
paletteBlend,
817818
cctBlending,
818-
getActiveSegmentsNum(void),
819-
getFirstSelectedSegId(void),
820-
getLastActiveSegmentId(void),
821-
getActiveSegsLightCapabilities(bool selectedOnly = false);
822-
823-
inline uint8_t getBrightness(void) { return _brightness; } // returns current strip brightness
824-
inline uint8_t getMaxSegments(void) { return MAX_NUM_SEGMENTS; } // returns maximum number of supported segments (fixed value)
825-
inline uint8_t getSegmentsNum(void) { return _segments.size(); } // returns currently present segments
826-
inline uint8_t getCurrSegmentId(void) { return _segment_index; } // returns current segment index (only valid while strip.isServicing())
827-
inline uint8_t getMainSegmentId(void) { return _mainSegment; } // returns main segment index
828-
inline uint8_t getPaletteCount() { return 13 + GRADIENT_PALETTE_COUNT + customPalettes.size(); }
829-
inline uint8_t getTargetFps() { return _targetFps; } // returns rough FPS value for las 2s interval
830-
inline uint8_t getModeCount() { return _modeCount; } // returns number of registered modes/effects
819+
getActiveSegmentsNum(void) const,
820+
getFirstSelectedSegId(void) const,
821+
getLastActiveSegmentId(void) const,
822+
getActiveSegsLightCapabilities(bool selectedOnly = false) const;
823+
824+
inline uint8_t getBrightness(void) const { return _brightness; } // returns current strip brightness
825+
inline uint8_t getMaxSegments(void) const { return MAX_NUM_SEGMENTS; } // returns maximum number of supported segments (fixed value)
826+
inline uint8_t getSegmentsNum(void) const { return _segments.size(); } // returns currently present segments
827+
inline uint8_t getCurrSegmentId(void) const { return _segment_index; } // returns current segment index (only valid while strip.isServicing())
828+
inline uint8_t getMainSegmentId(void) const { return _mainSegment; } // returns main segment index
829+
inline uint8_t getPaletteCount() const { return 13 + GRADIENT_PALETTE_COUNT + customPalettes.size(); }
830+
inline uint8_t getTargetFps() const { return _targetFps; } // returns rough FPS value for las 2s interval
831+
inline uint8_t getModeCount() const { return _modeCount; } // returns number of registered modes/effects
831832

832833
uint16_t
833-
getLengthPhysical(void),
834-
getLengthTotal(void), // will include virtual/nonexistent pixels in matrix
835-
getFps(),
836-
getMappedPixelIndex(uint16_t index);
834+
getLengthPhysical(void) const,
835+
getLengthTotal(void) const, // will include virtual/nonexistent pixels in matrix
836+
getFps() const,
837+
getMappedPixelIndex(uint16_t index) const;
837838

838-
inline uint16_t getFrameTime(void) { return _frametime; } // returns amount of time a frame should take (in ms)
839-
inline uint16_t getMinShowDelay(void) { return MIN_SHOW_DELAY; } // returns minimum amount of time strip.service() can be delayed (constant)
840-
inline uint16_t getLength(void) { return _length; } // returns actual amount of LEDs on a strip (2D matrix may have less LEDs than W*H)
841-
inline uint16_t getTransition(void) { return _transitionDur; } // returns currently set transition time (in ms)
839+
inline uint16_t getFrameTime(void) const { return _frametime; } // returns amount of time a frame should take (in ms)
840+
inline uint16_t getMinShowDelay(void) const { return MIN_SHOW_DELAY; } // returns minimum amount of time strip.service() can be delayed (constant)
841+
inline uint16_t getLength(void) const { return _length; } // returns actual amount of LEDs on a strip (2D matrix may have less LEDs than W*H)
842+
inline uint16_t getTransition(void) const { return _transitionDur; } // returns currently set transition time (in ms)
842843

843844
uint32_t
844845
now,
845846
timebase,
846-
getPixelColor(uint16_t);
847+
getPixelColor(uint16_t) const;
847848

848-
inline uint32_t getLastShow(void) { return _lastShow; } // returns millis() timestamp of last strip.show() call
849-
inline uint32_t segColor(uint8_t i) { return _colors_t[i]; } // returns currently valid color (for slot i) AKA SEGCOLOR(); may be blended between two colors while in transition
849+
inline uint32_t getLastShow(void) const { return _lastShow; } // returns millis() timestamp of last strip.show() call
850+
inline uint32_t segColor(uint8_t i) const { return _colors_t[i]; } // returns currently valid color (for slot i) AKA SEGCOLOR(); may be blended between two colors while in transition
850851

851852
const char *
852-
getModeData(uint8_t id = 0) { return (id && id<_modeCount) ? _modeData[id] : PSTR("Solid"); }
853+
getModeData(uint8_t id = 0) const { return (id && id<_modeCount) ? _modeData[id] : PSTR("Solid"); }
853854

854855
const char **
855856
getModeDataSrc(void) { return &(_modeData[0]); } // vectors use arrays for underlying data
@@ -900,7 +901,7 @@ class WS2812FX { // 96 bytes
900901
inline void setPixelColorXY(int x, int y, byte r, byte g, byte b, byte w = 0) { setPixelColorXY(x, y, RGBW32(r,g,b,w)); }
901902
inline void setPixelColorXY(int x, int y, CRGB c) { setPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0)); }
902903

903-
inline uint32_t getPixelColorXY(uint16_t x, uint16_t y) { return getPixelColor(isMatrix ? y * Segment::maxWidth + x : x);}
904+
inline uint32_t getPixelColorXY(int x, int y) const { return getPixelColor(isMatrix ? y * Segment::maxWidth + x : x); }
904905

905906
// end 2D support
906907

0 commit comments

Comments
 (0)