Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 65 additions & 19 deletions wled00/FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,37 +660,83 @@ static const char _data_FX_MODE_SAW[] PROGMEM = "Saw@!,Width;!,!;!";
/*
* Blink several LEDs in random colors on, reset, repeat.
* Inspired by www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
* Cycle mode checkbox is check2
*/
uint16_t mode_twinkle(void) {
SEGMENT.fade_out(224);

uint32_t cycleTime = 20 + (255 - SEGMENT.speed)*5;
uint32_t cycleTime = 20 + (255 - SEGMENT.speed) * 5;
uint32_t it = strip.now / cycleTime;
if (it != SEGENV.step)
{
unsigned maxOn = map(SEGMENT.intensity, 0, 255, 1, SEGLEN); // make sure at least one LED is on
if (SEGENV.aux0 >= maxOn)

// Standard twinkle
if ( ! SEGMENT.check2) {
SEGMENT.fade_out(224);

if (it != SEGENV.step)
{
SEGENV.aux0 = 0;
SEGENV.aux1 = hw_random(); //new seed for our PRNG
unsigned maxOn = map(SEGMENT.intensity, 0, 255, 1, SEGLEN); // make sure at least one LED is on
if (SEGENV.aux0 >= maxOn)
{
SEGENV.aux0 = 0;
SEGENV.aux1 = hw_random(); //new seed for our PRNG
}
SEGENV.aux0++;
SEGENV.step = it;
}
SEGENV.aux0++;
SEGENV.step = it;
}

uint16_t PRNG16 = SEGENV.aux1;
uint16_t PRNG16 = SEGENV.aux1;

for (unsigned i = 0; i < SEGENV.aux0; i++)
{
PRNG16 = (uint16_t)(PRNG16 * 2053) + 13849; // next 'random' number
uint32_t p = (uint32_t)SEGLEN * (uint32_t)PRNG16;
unsigned j = p >> 16;
SEGMENT.setPixelColor(j, SEGMENT.color_from_palette(j, true, PALETTE_SOLID_WRAP, 0));
}

return FRAMETIME;
}

for (unsigned i = 0; i < SEGENV.aux0; i++)
// Cycle mode: randomly switch all LEDs to new color, then back to old color
if (SEGLEN <= 1) return mode_static();

unsigned dataSize = (SEGLEN + 7) >> 3;
if ( ! SEGENV.allocateData(dataSize)) return mode_static();

if (SEGENV.call == 0 || SEGENV.aux1 > 1)
{
PRNG16 = (uint16_t)(PRNG16 * 2053) + 13849; // next 'random' number
uint32_t p = (uint32_t)SEGLEN * (uint32_t)PRNG16;
unsigned j = p >> 16;
SEGMENT.setPixelColor(j, SEGMENT.color_from_palette(j, true, PALETTE_SOLID_WRAP, 0));
SEGENV.aux0 = 0;
SEGENV.aux1 = 0;
memset(SEGENV.data, 0, dataSize);
SEGMENT.fill(SEGCOLOR(1));
}

if (it != SEGENV.step)
{
SEGENV.step = it;
if (SEGENV.aux0 >= SEGLEN)
{
SEGENV.aux1 = ! SEGENV.aux1;
SEGENV.aux0 = 0;
memset(SEGENV.data, 0, dataSize);
}
else
{
for (unsigned attempts = 0; attempts < SEGLEN * 2; attempts++) {
unsigned j = hw_random16(SEGLEN);
unsigned byteIndex = j >> 3, bitNum = j & 0x07;
if ( ! bitRead(SEGENV.data[byteIndex], bitNum))
{
bitWrite(SEGENV.data[byteIndex], bitNum, true);
SEGENV.aux0++;
SEGMENT.setPixelColor(j, SEGENV.aux1 ? SEGCOLOR(1) : SEGMENT.color_from_palette(j, true, PALETTE_SOLID_WRAP, 0));
break;
}
}
}
}

return FRAMETIME;
}
static const char _data_FX_MODE_TWINKLE[] PROGMEM = "Twinkle@!,!;!,!;!;;m12=0"; //pixels
static const char _data_FX_MODE_TWINKLE[] PROGMEM = "Twinkle@!,!,,,,,Cycle mode;!,!;!;;m12=0"; //pixels


/*
Expand Down