Skip to content

Commit a9c211d

Browse files
committed
Tetris (falling bricks) effect & Colortwinkles low brightness fix.
1 parent 94941a7 commit a9c211d

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

wled00/FX.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ uint16_t WS2812FX::mode_colortwinkle()
19911991
if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed
19921992

19931993
CRGB fastled_col, prev;
1994-
fract8 fadeUpAmount = 8 + (SEGMENT.speed/4), fadeDownAmount = 5 + (SEGMENT.speed/7);
1994+
fract8 fadeUpAmount = _brightness>28 ? 8 + (SEGMENT.speed>>2) : 68-_brightness, fadeDownAmount = _brightness>28 ? 8 + (SEGMENT.speed>>3) : 68-_brightness;
19951995
for (uint16_t i = 0; i < SEGLEN; i++) {
19961996
fastled_col = col_to_crgb(getPixelColor(i));
19971997
prev = fastled_col;
@@ -3144,6 +3144,59 @@ uint16_t WS2812FX::mode_drip(void)
31443144
}
31453145

31463146

3147+
/*
3148+
* Tetris or Stacking (falling bricks) Effect
3149+
* by Blaz Kristan (https://github.com/blazoncek, https://blaz.at/home)
3150+
*/
3151+
typedef struct Tetris {
3152+
float pos;
3153+
float speed;
3154+
uint32_t col;
3155+
} tetris;
3156+
3157+
uint16_t WS2812FX::mode_tetris(void) {
3158+
3159+
uint16_t dataSize = sizeof(tetris);
3160+
if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed
3161+
Tetris* drop = reinterpret_cast<Tetris*>(SEGENV.data);
3162+
3163+
// initialize dropping on first call or segment full
3164+
if (SEGENV.call == 0 || SEGENV.aux1 >= SEGLEN) {
3165+
SEGENV.aux1 = 0; // reset brick stack size
3166+
SEGENV.step = 0;
3167+
fill(SEGCOLOR(1));
3168+
return 250; // short wait
3169+
}
3170+
3171+
if (SEGENV.step == 0) { //init
3172+
drop->speed = 0.0238 * (SEGMENT.speed ? (SEGMENT.speed>>4)+1 : random8(3,20)); // set speed
3173+
drop->pos = SEGLEN-1; // start at end of segment
3174+
drop->col = color_from_palette(random8(0,15)<<4,false,false,0); // limit color choices so there is enough HUE gap
3175+
SEGENV.step = 1; // drop state (0 init, 1 forming, 2 falling)
3176+
SEGENV.aux0 = (SEGMENT.intensity ? (SEGMENT.intensity>>5)+1 : random8(1,5)) * (1+(SEGLEN>>6)); // size of brick
3177+
}
3178+
3179+
if (SEGENV.step == 1) { // forming
3180+
if (random8()>>6) { // random drop
3181+
SEGENV.step = 2; // fall
3182+
}
3183+
}
3184+
3185+
if (SEGENV.step > 1) { // falling
3186+
if (drop->pos > SEGENV.aux1) { // fall until top of stack
3187+
drop->pos -= drop->speed; // may add gravity as: speed += gravity
3188+
if (int(drop->pos) < SEGENV.aux1) drop->pos = SEGENV.aux1;
3189+
for (uint16_t i=int(drop->pos); i<SEGLEN; i++) setPixelColor(i,i<int(drop->pos)+SEGENV.aux0 ? drop->col : SEGCOLOR(1));
3190+
} else { // we hit bottom
3191+
SEGENV.step = 0; // go back to init
3192+
SEGENV.aux1 += SEGENV.aux0; // increase the stack size
3193+
if (SEGENV.aux1 >= SEGLEN) return 1000; // wait for a second
3194+
}
3195+
}
3196+
return FRAMETIME;
3197+
}
3198+
3199+
31473200
/*
31483201
/ Plasma Effect
31493202
/ adapted from https://github.com/atuline/FastLED-Demos/blob/master/plasma/plasma.ino

wled00/FX.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
#define IS_REVERSE ((SEGMENT.options & REVERSE ) == REVERSE )
119119
#define IS_SELECTED ((SEGMENT.options & SELECTED ) == SELECTED )
120120

121-
#define MODE_COUNT 118
121+
#define MODE_COUNT 119
122122

123123
#define FX_MODE_STATIC 0
124124
#define FX_MODE_BLINK 1
@@ -238,6 +238,7 @@
238238
#define FX_MODE_BLENDS 115
239239
#define FX_MODE_TV_SIMULATOR 116
240240
#define FX_MODE_DYNAMIC_SMOOTH 117
241+
#define FX_MODE_TETRIS 118
241242

242243

243244
class WS2812FX {
@@ -577,6 +578,7 @@ class WS2812FX {
577578
_mode[FX_MODE_BLENDS] = &WS2812FX::mode_blends;
578579
_mode[FX_MODE_TV_SIMULATOR] = &WS2812FX::mode_tv_simulator;
579580
_mode[FX_MODE_DYNAMIC_SMOOTH] = &WS2812FX::mode_dynamic_smooth;
581+
_mode[FX_MODE_TETRIS] = &WS2812FX::mode_tetris;
580582

581583
_brightness = DEFAULT_BRIGHTNESS;
582584
currentPalette = CRGBPalette16(CRGB::Black);
@@ -791,7 +793,8 @@ class WS2812FX {
791793
mode_candy_cane(void),
792794
mode_blends(void),
793795
mode_tv_simulator(void),
794-
mode_dynamic_smooth(void);
796+
mode_dynamic_smooth(void),
797+
mode_tetris(void);
795798

796799
private:
797800
NeoPixelWrapper *bus;
@@ -891,7 +894,7 @@ const char JSON_mode_names[] PROGMEM = R"=====([
891894
"Twinklefox","Twinklecat","Halloween Eyes","Solid Pattern","Solid Pattern Tri","Spots","Spots Fade","Glitter","Candle","Fireworks Starburst",
892895
"Fireworks 1D","Bouncing Balls","Sinelon","Sinelon Dual","Sinelon Rainbow","Popcorn","Drip","Plasma","Percent","Ripple Rainbow",
893896
"Heartbeat","Pacifica","Candle Multi", "Solid Glitter","Sunrise","Phased","Twinkleup","Noise Pal", "Sine","Phased Noise",
894-
"Flow","Chunchun","Dancing Shadows","Washing Machine","Candy Cane","Blends","TV Simulator","Dynamic Smooth"
897+
"Flow","Chunchun","Dancing Shadows","Washing Machine","Candy Cane","Blends","TV Simulator","Dynamic Smooth","Tetris"
895898
])=====";
896899

897900

0 commit comments

Comments
 (0)