2019-02-09 16:37:20 +01:00
|
|
|
/*
|
|
|
|
WS2812FX_fcn.cpp contains all utility functions
|
|
|
|
Harm Aldick - 2016
|
|
|
|
www.aldick.org
|
|
|
|
LICENSE
|
|
|
|
The MIT License (MIT)
|
|
|
|
Copyright (c) 2016 Harm Aldick
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
Modified heavily for WLED
|
|
|
|
*/
|
2021-02-24 20:23:32 +01:00
|
|
|
#include "wled.h"
|
2019-10-07 23:38:21 +02:00
|
|
|
#include "FX.h"
|
2019-02-09 16:37:20 +01:00
|
|
|
#include "palettes.h"
|
|
|
|
|
2021-02-13 01:02:14 +01:00
|
|
|
/*
|
|
|
|
Custom per-LED mapping has moved!
|
2020-03-21 00:57:54 +01:00
|
|
|
|
2021-02-13 01:02:14 +01:00
|
|
|
Create a file "ledmap.json" using the edit page.
|
2020-03-21 00:57:54 +01:00
|
|
|
|
2021-02-13 01:02:14 +01:00
|
|
|
this is just an example (30 LEDs). It will first set all even, then all uneven LEDs.
|
|
|
|
{"map":[
|
|
|
|
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
|
|
|
|
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]}
|
2020-03-21 00:57:54 +01:00
|
|
|
|
2021-02-13 01:02:14 +01:00
|
|
|
another example. Switches direction every 5 LEDs.
|
|
|
|
{"map":[
|
|
|
|
0, 1, 2, 3, 4, 9, 8, 7, 6, 5, 10, 11, 12, 13, 14,
|
2021-02-24 20:23:32 +01:00
|
|
|
19, 18, 17, 16, 15, 20, 21, 22, 23, 24, 29, 28, 27, 26, 25]}
|
2021-02-13 01:02:14 +01:00
|
|
|
*/
|
2020-12-13 19:02:12 +01:00
|
|
|
|
2021-04-15 10:55:22 +02:00
|
|
|
//factory defaults LED setup
|
|
|
|
//#define PIXEL_COUNTS 30, 30, 30, 30
|
|
|
|
//#define DATA_PINS 16, 1, 3, 4
|
|
|
|
//#define DEFAULT_LED_TYPE TYPE_WS2812_RGB
|
|
|
|
|
|
|
|
#ifndef PIXEL_COUNTS
|
2021-12-02 13:09:53 +01:00
|
|
|
#define PIXEL_COUNTS DEFAULT_LED_COUNT
|
2021-04-15 10:55:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DATA_PINS
|
|
|
|
#define DATA_PINS LEDPIN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DEFAULT_LED_TYPE
|
|
|
|
#define DEFAULT_LED_TYPE TYPE_WS2812_RGB
|
|
|
|
#endif
|
|
|
|
|
2022-03-16 00:17:49 +01:00
|
|
|
#ifndef DEFAULT_LED_COLOR_ORDER
|
|
|
|
#define DEFAULT_LED_COLOR_ORDER COL_ORDER_GRB //default to GRB
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2021-06-24 02:29:14 +02:00
|
|
|
#if MAX_NUM_SEGMENTS < WLED_MAX_BUSSES
|
|
|
|
#error "Max segments must be at least max number of busses!"
|
|
|
|
#endif
|
|
|
|
|
2022-07-06 13:13:54 +02:00
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ColorTransition class implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
void ColorTransition::startTransition(Segment *seg, uint16_t dur) {
|
|
|
|
// starting a transition has to occur before change
|
|
|
|
if (!seg->isActive()) return;
|
|
|
|
briOld = currentBri(seg->getOption(SEG_OPTION_ON) ? seg->opacity : 0);
|
|
|
|
cctOld = currentBri(seg->cct, true);
|
|
|
|
for (size_t i=0; i<NUM_COLORS; i++) colorOld[i] = currentColor(i, seg->colors[i]);
|
2022-07-10 22:23:25 +02:00
|
|
|
transitionDur = dur;
|
|
|
|
transitionStart = millis();
|
|
|
|
seg->setOption(SEG_OPTION_TRANSITIONAL, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ColorTransition::progress() { //transition progression between 0-65535
|
|
|
|
uint32_t timeNow = millis();
|
|
|
|
if (timeNow - transitionStart > transitionDur) return 0xFFFFU;
|
|
|
|
uint32_t elapsed = timeNow - transitionStart;
|
2022-07-11 11:39:56 +02:00
|
|
|
return min(0xFFFFU, (unsigned int)(elapsed * 0xFFFFU / transitionDur));
|
2022-07-06 13:13:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
uint8_t ColorTransition::currentBri(uint8_t briNew, bool useCct) {
|
|
|
|
uint32_t prog = progress() + 1;
|
2022-07-12 18:10:07 +02:00
|
|
|
if (useCct) return ((briNew * prog) + cctOld * (0x10000 - prog)) >> 16;
|
|
|
|
else return ((briNew * prog) + briOld * (0x10000 - prog)) >> 16;
|
2022-07-10 22:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorTransition::handleTransition(Segment *seg, uint8_t &newBri, uint8_t &newCct, uint32_t *newCol) {
|
2022-07-12 18:10:07 +02:00
|
|
|
newBri = currentBri(newBri);
|
|
|
|
newCct = currentBri(newCct, true);
|
|
|
|
for (size_t i=0; i<NUM_COLORS; i++) newCol[i] = currentColor(i, newCol[i]);
|
|
|
|
unsigned long maxWait = millis() + 22;
|
|
|
|
if (seg->mode == FX_MODE_STATIC && seg->runtime.next_time > maxWait) seg->runtime.next_time = maxWait;
|
|
|
|
if (progress() == 0xFFFFU && seg->getOption(SEG_OPTION_TRANSITIONAL)) {
|
|
|
|
// finish transition
|
|
|
|
briOld = newBri;
|
|
|
|
cctOld = newCct;
|
|
|
|
for (size_t i=0; i<NUM_COLORS; i++) colorOld[i] = newCol[i];
|
|
|
|
seg->setOption(SEG_OPTION_TRANSITIONAL, false);
|
|
|
|
}
|
2022-07-10 22:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Segment class implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool Segment::setColor(uint8_t slot, uint32_t c) { //returns true if changed
|
|
|
|
if (slot >= NUM_COLORS || c == colors[slot]) return false;
|
2022-07-12 18:10:07 +02:00
|
|
|
transition.startTransition(this, strip.getTransition()); // start transition prior to change
|
2022-07-10 22:23:25 +02:00
|
|
|
colors[slot] = c;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Segment::setCCT(uint16_t k) {
|
2022-07-06 13:13:54 +02:00
|
|
|
if (k > 255) { //kelvin value, convert to 0-255
|
|
|
|
if (k < 1900) k = 1900;
|
|
|
|
if (k > 10091) k = 10091;
|
|
|
|
k = (k - 1900) >> 5;
|
|
|
|
}
|
|
|
|
if (cct == k) return;
|
2022-07-12 18:10:07 +02:00
|
|
|
transition.startTransition(this, strip.getTransition()); // start transition prior to change
|
2022-07-06 13:13:54 +02:00
|
|
|
cct = k;
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
void Segment::setOpacity(uint8_t o) {
|
2022-07-06 13:13:54 +02:00
|
|
|
if (opacity == o) return;
|
2022-07-12 18:10:07 +02:00
|
|
|
transition.startTransition(this, strip.getTransition()); // start transition prior to change
|
2022-07-06 13:13:54 +02:00
|
|
|
opacity = o;
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
void Segment::setOption(uint8_t n, bool val) {
|
|
|
|
bool prevOn = getOption(SEG_OPTION_ON);
|
2022-07-12 18:10:07 +02:00
|
|
|
if (n == SEG_OPTION_ON && val != prevOn) transition.startTransition(this, strip.getTransition()); // start transition prior to change
|
2022-07-06 13:13:54 +02:00
|
|
|
if (val) options |= 0x01 << n;
|
|
|
|
else options &= ~(0x01 << n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2D matrix
|
|
|
|
uint16_t Segment::virtualWidth() {
|
|
|
|
uint16_t groupLen = groupLength();
|
|
|
|
uint16_t vWidth = ((getOption(SEG_OPTION_TRANSPOSED) ? height() : width()) + groupLen - 1) / groupLen;
|
|
|
|
if (getOption(SEG_OPTION_MIRROR)) vWidth = (vWidth + 1) /2; // divide by 2 if mirror, leave at least a single LED
|
|
|
|
return vWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t Segment::virtualHeight() {
|
|
|
|
uint16_t groupLen = groupLength();
|
|
|
|
uint16_t vHeight = ((getOption(SEG_OPTION_TRANSPOSED) ? width() : height()) + groupLen - 1) / groupLen;
|
|
|
|
if (getOption(SEG_OPTION_MIRROR_Y)) vHeight = (vHeight + 1) /2; // divide by 2 if mirror, leave at least a single LED
|
|
|
|
return vHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1D strip
|
|
|
|
uint16_t Segment::virtualLength() {
|
2022-07-14 13:22:34 +02:00
|
|
|
#ifndef WLED_DISABLE_2D
|
|
|
|
if (height() > 1) {
|
|
|
|
uint16_t vW = virtualWidth();
|
|
|
|
uint16_t vH = virtualHeight();
|
|
|
|
uint32_t vLen = vW * vH; // use all pixels from segment
|
|
|
|
switch (mapping12) {
|
|
|
|
case M12_VerticalBar: vLen = vW;
|
|
|
|
break;
|
|
|
|
case M12_Circle:
|
|
|
|
case M12_Block: vLen = (vW + vH) / 4; // take half of the average width
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return vLen;
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-06 13:13:54 +02:00
|
|
|
uint16_t groupLen = groupLength();
|
|
|
|
uint16_t vLength = (length() + groupLen - 1) / groupLen;
|
|
|
|
if (getOption(SEG_OPTION_MIRROR)) vLength = (vLength + 1) /2; // divide by 2 if mirror, leave at least a single LED
|
|
|
|
return vLength;
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
|
|
|
|
{
|
2022-07-14 13:22:34 +02:00
|
|
|
#ifndef WLED_DISABLE_2D
|
|
|
|
if (height() > 1) { // if this does not work use strip.isMatrix
|
|
|
|
uint16_t vH = virtualHeight(); // segment height in logical pixels
|
|
|
|
uint16_t vW = virtualWidth();
|
|
|
|
switch (mapping12) {
|
2022-07-11 11:20:30 +02:00
|
|
|
case M12_Pixels:
|
2022-07-14 13:22:34 +02:00
|
|
|
// use all available pixels as a long strip
|
|
|
|
setPixelColorXY(i % vW, i / vW, col);
|
2022-07-11 11:20:30 +02:00
|
|
|
break;
|
|
|
|
case M12_VerticalBar:
|
2022-07-14 13:22:34 +02:00
|
|
|
// expand 1D effect vertically
|
|
|
|
for (int y = 0; y < vH; y++) {
|
2022-07-11 11:20:30 +02:00
|
|
|
setPixelColorXY(i, y * groupLength(), col);
|
|
|
|
}
|
|
|
|
break;
|
2022-07-13 12:41:33 +02:00
|
|
|
case M12_Circle:
|
2022-07-14 13:22:34 +02:00
|
|
|
// expand in circular fashion from center
|
|
|
|
for (int degrees = 0; degrees <= 360; degrees += 180 / (i+1)) { // this may prove too many iterations on larger matrices
|
|
|
|
// may want to try float version as well (with or without antialiasing)
|
2022-07-14 17:22:37 +02:00
|
|
|
int x = (int)roundf(roundf((sin_t(degrees*DEG_TO_RAD) * i + vW / 2.0f) * 10)/10); // sin_t is Aircoookie's implementation that uses less RAM
|
|
|
|
int y = (int)roundf(roundf((cos_t(degrees*DEG_TO_RAD) * i + vH / 2.0f) * 10)/10); // cos_t is Aircoookie's implementation that uses less RAM
|
2022-07-11 11:20:30 +02:00
|
|
|
setPixelColorXY(x, y, col);
|
|
|
|
}
|
|
|
|
break;
|
2022-07-13 12:41:33 +02:00
|
|
|
case M12_Block:
|
2022-07-14 13:22:34 +02:00
|
|
|
// expand in rectangular fashion from center
|
|
|
|
for (int x = vW / 2 - i - 1; x <= vW / 2 + i; x++) {
|
|
|
|
setPixelColorXY(x, vH / 2 - i - 1, col);
|
|
|
|
setPixelColorXY(x, vH / 2 + i , col);
|
2022-07-11 11:20:30 +02:00
|
|
|
}
|
2022-07-14 13:22:34 +02:00
|
|
|
for (int y = vH / 2 - i - 1 + 1; y <= vH / 2 + i - 1; y++) {
|
|
|
|
setPixelColorXY(vW / 2 - i - 1, y, col);
|
|
|
|
setPixelColorXY(vW / 2 + i , y, col);
|
2022-07-11 11:20:30 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-07-10 22:23:25 +02:00
|
|
|
}
|
2022-07-13 12:41:33 +02:00
|
|
|
return;
|
2022-07-10 22:23:25 +02:00
|
|
|
}
|
2022-07-14 13:22:34 +02:00
|
|
|
#endif
|
2022-07-10 22:23:25 +02:00
|
|
|
|
|
|
|
uint16_t len = length();
|
2022-07-12 18:10:07 +02:00
|
|
|
uint8_t _bri_t = strip._bri_t;
|
2022-07-10 22:23:25 +02:00
|
|
|
if (_bri_t < 255) {
|
|
|
|
byte r = scale8(R(col), _bri_t);
|
|
|
|
byte g = scale8(G(col), _bri_t);
|
|
|
|
byte b = scale8(B(col), _bri_t);
|
|
|
|
byte w = scale8(W(col), _bri_t);
|
|
|
|
col = RGBW32(r, g, b, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
// expand pixel (taking into account start, grouping, spacing [and offset])
|
|
|
|
i = i * groupLength();
|
|
|
|
if (getOption(SEG_OPTION_REVERSED)) { // is segment reversed?
|
|
|
|
if (getOption(SEG_OPTION_MIRROR)) { // is segment mirrored?
|
|
|
|
i = (len - 1) / 2 - i; //only need to index half the pixels
|
|
|
|
} else {
|
|
|
|
i = (len - 1) - i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += start; // starting pixel in a group
|
|
|
|
|
|
|
|
// set all the pixels in the group
|
|
|
|
for (int j = 0; j < grouping; j++) {
|
|
|
|
uint16_t indexSet = i + ((getOption(SEG_OPTION_REVERSED)) ? -j : j);
|
|
|
|
if (indexSet >= start && indexSet < stop) {
|
|
|
|
if (getOption(SEG_OPTION_MIRROR)) { //set the corresponding mirrored pixel
|
|
|
|
uint16_t indexMir = stop - indexSet + start - 1;
|
|
|
|
indexMir += offset; // offset/phase
|
|
|
|
if (indexMir >= stop) indexMir -= len; // wrap
|
|
|
|
strip.setPixelColor(indexMir, col);
|
|
|
|
}
|
|
|
|
indexSet += offset; // offset/phase
|
|
|
|
if (indexSet >= stop) indexSet -= len; // wrap
|
|
|
|
strip.setPixelColor(indexSet, col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// anti-aliased normalized version of setPixelColor()
|
|
|
|
void Segment::setPixelColor(float i, uint32_t col, bool aa)
|
|
|
|
{
|
|
|
|
if (i<0.0f || i>1.0f) return; // not normalized
|
|
|
|
|
|
|
|
float fC = i * (virtualLength()-1);
|
|
|
|
if (aa) {
|
|
|
|
uint16_t iL = roundf(fC-0.49f);
|
|
|
|
uint16_t iR = roundf(fC+0.49f);
|
|
|
|
float dL = fC - iL;
|
|
|
|
float dR = iR - fC;
|
|
|
|
uint32_t cIL = getPixelColor(iL);
|
|
|
|
uint32_t cIR = getPixelColor(iR);
|
|
|
|
if (iR!=iL) {
|
|
|
|
// blend L pixel
|
|
|
|
cIL = color_blend(col, cIL, uint8_t(dL*255.0f));
|
|
|
|
setPixelColor(iL, cIL);
|
|
|
|
// blend R pixel
|
|
|
|
cIR = color_blend(col, cIR, uint8_t(dR*255.0f));
|
|
|
|
setPixelColor(iR, cIR);
|
|
|
|
} else {
|
|
|
|
// exact match (x & y land on a pixel)
|
|
|
|
setPixelColor(iL, col);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setPixelColor(uint16_t(roundf(fC)), col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Segment::getPixelColor(uint16_t i)
|
|
|
|
{
|
2022-07-14 13:22:34 +02:00
|
|
|
#ifndef WLED_DISABLE_2D
|
|
|
|
if (height() > 1) { // if this does not work use strip.isMatrix
|
|
|
|
uint16_t vH = virtualHeight(); // segment height in logical pixels
|
|
|
|
uint16_t vW = virtualWidth();
|
|
|
|
switch (mapping12) {
|
2022-07-13 15:40:40 +02:00
|
|
|
case M12_Pixels:
|
2022-07-14 13:22:34 +02:00
|
|
|
return getPixelColorXY(i % vW, i / vW);
|
2022-07-13 15:40:40 +02:00
|
|
|
break;
|
|
|
|
case M12_VerticalBar:
|
|
|
|
// map linear pixel into 2D segment area (even for 1D segments, expanding vertically)
|
|
|
|
return getPixelColorXY(i, 0);
|
|
|
|
break;
|
|
|
|
case M12_Circle:
|
|
|
|
{
|
2022-07-14 13:22:34 +02:00
|
|
|
int x = roundf(roundf((vW / 2) * 10)/10);
|
|
|
|
int y = roundf(roundf((i + vH / 2) * 10)/10);
|
2022-07-13 15:40:40 +02:00
|
|
|
return getPixelColorXY(x,y);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case M12_Block:
|
2022-07-14 13:22:34 +02:00
|
|
|
return getPixelColorXY(vW / 2, vH / 2 - i - 1);
|
2022-07-13 15:40:40 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-07-14 13:22:34 +02:00
|
|
|
#endif
|
2022-07-13 15:40:40 +02:00
|
|
|
|
2022-07-14 17:22:37 +02:00
|
|
|
if (getOption(SEG_OPTION_REVERSED)) i = virtualLength() - i - 1;
|
2022-07-10 22:23:25 +02:00
|
|
|
i *= groupLength();
|
|
|
|
i += start;
|
|
|
|
/* offset/phase */
|
|
|
|
i += offset;
|
|
|
|
if (i >= stop) i -= length();
|
|
|
|
return strip.getPixelColor(i);
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:13:54 +02:00
|
|
|
uint8_t Segment::differs(Segment& b) {
|
|
|
|
uint8_t d = 0;
|
|
|
|
if (start != b.start) d |= SEG_DIFFERS_BOUNDS;
|
|
|
|
if (stop != b.stop) d |= SEG_DIFFERS_BOUNDS;
|
|
|
|
if (offset != b.offset) d |= SEG_DIFFERS_GSO;
|
|
|
|
if (grouping != b.grouping) d |= SEG_DIFFERS_GSO;
|
|
|
|
if (spacing != b.spacing) d |= SEG_DIFFERS_GSO;
|
|
|
|
if (opacity != b.opacity) d |= SEG_DIFFERS_BRI;
|
|
|
|
if (mode != b.mode) d |= SEG_DIFFERS_FX;
|
|
|
|
if (speed != b.speed) d |= SEG_DIFFERS_FX;
|
|
|
|
if (intensity != b.intensity) d |= SEG_DIFFERS_FX;
|
|
|
|
if (palette != b.palette) d |= SEG_DIFFERS_FX;
|
|
|
|
if (custom1 != b.custom1) d |= SEG_DIFFERS_FX;
|
|
|
|
if (custom2 != b.custom2) d |= SEG_DIFFERS_FX;
|
|
|
|
if (custom3 != b.custom3) d |= SEG_DIFFERS_FX;
|
|
|
|
if (startY != b.startY) d |= SEG_DIFFERS_BOUNDS;
|
|
|
|
if (stopY != b.stopY) d |= SEG_DIFFERS_BOUNDS;
|
|
|
|
|
|
|
|
//bit pattern: msb first: [transposed mirrorY reverseY] transitional (tbd) paused needspixelstate mirrored on reverse selected
|
|
|
|
if ((options & 0b11100101110) != (b.options & 0b11100101110)) d |= SEG_DIFFERS_OPT;
|
|
|
|
if ((options & 0x01) != (b.options & 0x01)) d |= SEG_DIFFERS_SEL;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < NUM_COLORS; i++) if (colors[i] != b.colors[i]) d |= SEG_DIFFERS_COL;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Segment::refreshLightCapabilities() {
|
|
|
|
if (!isActive()) {
|
|
|
|
_capabilities = 0; return;
|
|
|
|
}
|
|
|
|
uint8_t capabilities = 0;
|
|
|
|
|
|
|
|
for (uint8_t b = 0; b < busses.getNumBusses(); b++) {
|
|
|
|
Bus *bus = busses.getBus(b);
|
|
|
|
if (bus == nullptr || bus->getLength()==0) break;
|
|
|
|
if (!bus->isOk()) continue;
|
|
|
|
if (bus->getStart() >= stop) continue;
|
|
|
|
if (bus->getStart() + bus->getLength() <= start) continue;
|
|
|
|
|
|
|
|
uint8_t type = bus->getType();
|
|
|
|
if (type != TYPE_ANALOG_1CH && (cctFromRgb || type != TYPE_ANALOG_2CH)) capabilities |= 0x01; // segment supports RGB (full color)
|
|
|
|
if (bus->isRgbw()) capabilities |= 0x02; // segment supports white channel
|
|
|
|
if (!cctFromRgb) {
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_ANALOG_5CH:
|
|
|
|
case TYPE_ANALOG_2CH:
|
|
|
|
capabilities |= 0x04; //segment supports white CCT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (correctWB && type != TYPE_ANALOG_1CH) capabilities |= 0x04; //white balance correction (uses CCT slider)
|
|
|
|
uint8_t aWM = Bus::getAutoWhiteMode()<255 ? Bus::getAutoWhiteMode() : bus->getAWMode();
|
|
|
|
bool whiteSlider = (aWM == RGBW_MODE_DUAL || aWM == RGBW_MODE_MANUAL_ONLY); // white slider allowed
|
|
|
|
if (bus->isRgbw() && (whiteSlider || !(capabilities & 0x01))) capabilities |= 0x08; // allow white channel adjustments (AWM allows or is not RGB)
|
|
|
|
}
|
|
|
|
_capabilities = capabilities;
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
/*
|
|
|
|
* Fills segment with color
|
|
|
|
*/
|
|
|
|
void Segment::fill(uint32_t c) {
|
|
|
|
const uint16_t cols = strip.isMatrix ? virtualWidth() : virtualLength();
|
|
|
|
const uint16_t rows = virtualHeight(); // will be 1 for 1D
|
|
|
|
for(uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) {
|
|
|
|
if (strip.isMatrix) setPixelColorXY(x, y, c);
|
|
|
|
else setPixelColor(x, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blends the specified color with the existing pixel color.
|
|
|
|
void Segment::blendPixelColor(uint16_t n, uint32_t color, uint8_t blend) {
|
|
|
|
setPixelColor(n, color_blend(getPixelColor(n), color, blend));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds the specified color with the existing pixel color perserving color balance.
|
|
|
|
void Segment::addPixelColor(uint16_t n, uint32_t color) {
|
|
|
|
setPixelColor(n, color_add(getPixelColor(n), color));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fade out function, higher rate = quicker fade
|
|
|
|
*/
|
|
|
|
void Segment::fade_out(uint8_t rate) {
|
|
|
|
const uint16_t cols = strip.isMatrix ? virtualWidth() : virtualLength();
|
|
|
|
const uint16_t rows = virtualHeight(); // will be 1 for 1D
|
|
|
|
|
|
|
|
rate = (255-rate) >> 1;
|
|
|
|
float mappedRate = float(rate) +1.1;
|
|
|
|
|
|
|
|
uint32_t color = colors[1]; // SEGCOLOR(1); // target color
|
|
|
|
int w2 = W(color);
|
|
|
|
int r2 = R(color);
|
|
|
|
int g2 = G(color);
|
|
|
|
int b2 = B(color);
|
|
|
|
|
|
|
|
for (uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) {
|
|
|
|
color = strip.isMatrix ? getPixelColorXY(x, y) : getPixelColor(x);
|
|
|
|
int w1 = W(color);
|
|
|
|
int r1 = R(color);
|
|
|
|
int g1 = G(color);
|
|
|
|
int b1 = B(color);
|
|
|
|
|
|
|
|
int wdelta = (w2 - w1) / mappedRate;
|
|
|
|
int rdelta = (r2 - r1) / mappedRate;
|
|
|
|
int gdelta = (g2 - g1) / mappedRate;
|
|
|
|
int bdelta = (b2 - b1) / mappedRate;
|
|
|
|
|
|
|
|
// if fade isn't complete, make sure delta is at least 1 (fixes rounding issues)
|
|
|
|
wdelta += (w2 == w1) ? 0 : (w2 > w1) ? 1 : -1;
|
|
|
|
rdelta += (r2 == r1) ? 0 : (r2 > r1) ? 1 : -1;
|
|
|
|
gdelta += (g2 == g1) ? 0 : (g2 > g1) ? 1 : -1;
|
|
|
|
bdelta += (b2 == b1) ? 0 : (b2 > b1) ? 1 : -1;
|
|
|
|
|
|
|
|
if (strip.isMatrix) setPixelColorXY(x, y, r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
|
|
|
else setPixelColor(x, r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fades all pixels to black using nscale8()
|
|
|
|
void Segment::fadeToBlackBy(uint8_t fadeBy) {
|
|
|
|
const uint16_t cols = strip.isMatrix ? virtualWidth() : virtualLength();
|
|
|
|
const uint16_t rows = virtualHeight(); // will be 1 for 1D
|
|
|
|
|
|
|
|
for (uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) {
|
|
|
|
if (strip.isMatrix) setPixelColorXY(x, y, CRGB(getPixelColorXY(x,y)).nscale8(255-fadeBy));
|
|
|
|
else setPixelColor(x, CRGB(getPixelColor(x)).nscale8(255-fadeBy));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* blurs segment content, source: FastLED colorutils.cpp
|
|
|
|
*/
|
|
|
|
void Segment::blur(uint8_t blur_amount)
|
|
|
|
{
|
|
|
|
if (strip.isMatrix) {
|
|
|
|
// compatibility with 2D
|
|
|
|
const uint16_t cols = virtualWidth();
|
|
|
|
const uint16_t rows = virtualHeight();
|
|
|
|
for (uint16_t i = 0; i < rows; i++) blurRow(i, blur_amount); // blur all rows
|
|
|
|
for (uint16_t k = 0; k < cols; k++) blurCol(k, blur_amount); // blur all columns
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t keep = 255 - blur_amount;
|
|
|
|
uint8_t seep = blur_amount >> 1;
|
|
|
|
CRGB carryover = CRGB::Black;
|
|
|
|
for(uint16_t i = 0; i < virtualLength(); i++)
|
|
|
|
{
|
|
|
|
CRGB cur = CRGB(getPixelColor(i));
|
|
|
|
CRGB part = cur;
|
|
|
|
part.nscale8(seep);
|
|
|
|
cur.nscale8(keep);
|
|
|
|
cur += carryover;
|
|
|
|
if(i > 0) {
|
|
|
|
uint32_t c = getPixelColor(i-1);
|
|
|
|
uint8_t r = R(c);
|
|
|
|
uint8_t g = G(c);
|
|
|
|
uint8_t b = B(c);
|
|
|
|
setPixelColor(i-1, qadd8(r, part.red), qadd8(g, part.green), qadd8(b, part.blue));
|
|
|
|
}
|
|
|
|
setPixelColor(i,cur.red, cur.green, cur.blue);
|
|
|
|
carryover = part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put a value 0 to 255 in to get a color value.
|
|
|
|
* The colours are a transition r -> g -> b -> back to r
|
|
|
|
* Inspired by the Adafruit examples.
|
|
|
|
*/
|
|
|
|
uint32_t Segment::color_wheel(uint8_t pos) { // TODO
|
|
|
|
if (palette) return color_from_palette(pos, false, true, 0);
|
|
|
|
pos = 255 - pos;
|
|
|
|
if(pos < 85) {
|
|
|
|
return ((uint32_t)(255 - pos * 3) << 16) | ((uint32_t)(0) << 8) | (pos * 3);
|
|
|
|
} else if(pos < 170) {
|
|
|
|
pos -= 85;
|
|
|
|
return ((uint32_t)(0) << 16) | ((uint32_t)(pos * 3) << 8) | (255 - pos * 3);
|
|
|
|
} else {
|
|
|
|
pos -= 170;
|
|
|
|
return ((uint32_t)(pos * 3) << 16) | ((uint32_t)(255 - pos * 3) << 8) | (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a new, random wheel index with a minimum distance of 42 from pos.
|
|
|
|
*/
|
|
|
|
uint8_t Segment::get_random_wheel_index(uint8_t pos) {
|
|
|
|
uint8_t r = 0, x = 0, y = 0, d = 0;
|
|
|
|
|
|
|
|
while(d < 42) {
|
|
|
|
r = random8();
|
|
|
|
x = abs(pos - r);
|
|
|
|
y = 255 - x;
|
|
|
|
d = MIN(x, y);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gets a single color from the currently selected palette.
|
|
|
|
* @param i Palette Index (if mapping is true, the full palette will be _virtualSegmentLength long, if false, 255). Will wrap around automatically.
|
|
|
|
* @param mapping if true, LED position in segment is considered for color
|
|
|
|
* @param wrap FastLED palettes will usally wrap back to the start smoothly. Set false to get a hard edge
|
|
|
|
* @param mcol If the default palette 0 is selected, return the standard color 0, 1 or 2 instead. If >2, Party palette is used instead
|
|
|
|
* @param pbri Value to scale the brightness of the returned color by. Default is 255. (no scaling)
|
|
|
|
* @returns Single color from palette
|
|
|
|
*/
|
|
|
|
uint32_t IRAM_ATTR Segment::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri)
|
|
|
|
{
|
|
|
|
if ((palette == 0 && mcol < 3) || strip._no_rgb) {
|
|
|
|
uint32_t color = colors[mcol]; // SEGCOLOR(mcol);
|
|
|
|
if (pbri == 255) return color;
|
|
|
|
return RGBW32(scale8_video(R(color),pbri), scale8_video(G(color),pbri), scale8_video(B(color),pbri), scale8_video(W(color),pbri));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t paletteIndex = i;
|
|
|
|
if (mapping && virtualLength() > 1) paletteIndex = (i*255)/(virtualLength() -1);
|
|
|
|
if (!wrap) paletteIndex = scale8(paletteIndex, 240); //cut off blend at palette "end"
|
|
|
|
CRGB fastled_col;
|
|
|
|
fastled_col = ColorFromPalette(strip.currentPalette, paletteIndex, pbri, (strip.paletteBlend == 3)? NOBLEND:LINEARBLEND);
|
|
|
|
|
|
|
|
return RGBW32(fastled_col.r, fastled_col.g, fastled_col.b, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-07-12 18:10:07 +02:00
|
|
|
// Segmentruntime class implementation
|
2022-07-10 22:23:25 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-07-06 13:13:54 +02:00
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
bool Segmentruntime::allocateData(uint16_t len){
|
2022-07-06 13:13:54 +02:00
|
|
|
if (data && _dataLen == len) return true; //already allocated
|
|
|
|
deallocateData();
|
2022-07-12 18:10:07 +02:00
|
|
|
if (strip.getUsedSegmentData() + len > MAX_SEGMENT_DATA) return false; //not enough memory
|
2022-07-06 13:13:54 +02:00
|
|
|
// if possible use SPI RAM on ESP32
|
|
|
|
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM)
|
|
|
|
if (psramFound())
|
|
|
|
data = (byte*) ps_malloc(len);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
data = (byte*) malloc(len);
|
|
|
|
if (!data) return false; //allocation failed
|
2022-07-12 18:10:07 +02:00
|
|
|
strip.addUsedSegmentData(len);
|
2022-07-06 13:13:54 +02:00
|
|
|
_dataLen = len;
|
|
|
|
memset(data, 0, len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
void Segmentruntime::deallocateData() {
|
2022-07-06 13:13:54 +02:00
|
|
|
free(data);
|
|
|
|
data = nullptr;
|
2022-07-12 18:10:07 +02:00
|
|
|
strip.addUsedSegmentData(-(int16_t)_dataLen);
|
2022-07-06 13:13:54 +02:00
|
|
|
_dataLen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If reset of this segment was request, clears runtime
|
|
|
|
* settings of this segment.
|
|
|
|
* Must not be called while an effect mode function is running
|
|
|
|
* because it could access the data buffer and this method
|
|
|
|
* may free that data buffer.
|
|
|
|
*/
|
2022-07-12 18:10:07 +02:00
|
|
|
void Segmentruntime::resetIfRequired() {
|
2022-07-06 13:13:54 +02:00
|
|
|
if (_requiresReset) {
|
|
|
|
next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0;
|
|
|
|
deallocateData();
|
|
|
|
_requiresReset = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WS2812FX class implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-07-06 13:13:54 +02:00
|
|
|
|
2021-01-18 20:51:32 +01:00
|
|
|
//do not call this method from system context (network callback)
|
2021-02-24 20:23:32 +01:00
|
|
|
void WS2812FX::finalizeInit(void)
|
2019-02-09 16:37:20 +01:00
|
|
|
{
|
2022-02-09 08:43:35 +01:00
|
|
|
//reset segment runtimes
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) {
|
2022-07-12 18:10:07 +02:00
|
|
|
_segments[i].runtime.markForReset();
|
|
|
|
_segments[i].runtime.resetIfRequired();
|
2022-02-09 08:43:35 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 13:28:00 +01:00
|
|
|
_hasWhiteChannel = _isOffRefreshRequired = false;
|
2019-11-29 18:53:01 +01:00
|
|
|
|
2021-03-12 23:56:29 +01:00
|
|
|
//if busses failed to load, add default (fresh install, FS issue, ...)
|
2021-01-30 20:51:36 +01:00
|
|
|
if (busses.getNumBusses() == 0) {
|
2022-03-19 14:21:14 +01:00
|
|
|
DEBUG_PRINTLN(F("No busses, init default"));
|
2021-04-15 22:19:58 +02:00
|
|
|
const uint8_t defDataPins[] = {DATA_PINS};
|
|
|
|
const uint16_t defCounts[] = {PIXEL_COUNTS};
|
2022-04-12 16:08:17 +02:00
|
|
|
const uint8_t defNumBusses = ((sizeof defDataPins) / (sizeof defDataPins[0]));
|
|
|
|
const uint8_t defNumCounts = ((sizeof defCounts) / (sizeof defCounts[0]));
|
2021-04-15 10:55:22 +02:00
|
|
|
uint16_t prevLen = 0;
|
2021-04-16 20:07:54 +02:00
|
|
|
for (uint8_t i = 0; i < defNumBusses && i < WLED_MAX_BUSSES; i++) {
|
2021-04-15 10:55:22 +02:00
|
|
|
uint8_t defPin[] = {defDataPins[i]};
|
|
|
|
uint16_t start = prevLen;
|
2021-10-11 02:19:33 +02:00
|
|
|
uint16_t count = defCounts[(i < defNumCounts) ? i : defNumCounts -1];
|
2021-04-15 10:55:22 +02:00
|
|
|
prevLen += count;
|
2022-03-16 08:52:48 +01:00
|
|
|
BusConfig defCfg = BusConfig(DEFAULT_LED_TYPE, defPin, start, count, DEFAULT_LED_COLOR_ORDER, false, 0, RGBW_MODE_MANUAL_ONLY);
|
2021-04-15 10:55:22 +02:00
|
|
|
busses.add(defCfg);
|
|
|
|
}
|
2021-01-30 20:51:36 +01:00
|
|
|
}
|
2021-01-18 20:51:32 +01:00
|
|
|
|
2021-02-24 20:23:32 +01:00
|
|
|
_length = 0;
|
|
|
|
for (uint8_t i=0; i<busses.getNumBusses(); i++) {
|
|
|
|
Bus *bus = busses.getBus(i);
|
|
|
|
if (bus == nullptr) continue;
|
2021-10-11 02:19:33 +02:00
|
|
|
if (bus->getStart() + bus->getLength() > MAX_LEDS) break;
|
2021-05-22 00:13:49 +02:00
|
|
|
//RGBW mode is enabled if at least one of the strips is RGBW
|
2022-02-04 13:28:00 +01:00
|
|
|
_hasWhiteChannel |= bus->isRgbw();
|
2021-05-22 00:13:49 +02:00
|
|
|
//refresh is required to remain off if at least one of the strips requires the refresh.
|
2022-02-04 13:28:00 +01:00
|
|
|
_isOffRefreshRequired |= bus->isOffRefreshRequired();
|
2021-10-11 02:19:33 +02:00
|
|
|
uint16_t busEnd = bus->getStart() + bus->getLength();
|
|
|
|
if (busEnd > _length) _length = busEnd;
|
2021-06-24 02:29:14 +02:00
|
|
|
#ifdef ESP8266
|
2021-10-11 02:19:33 +02:00
|
|
|
if ((!IS_DIGITAL(bus->getType()) || IS_2PIN(bus->getType()))) continue;
|
2021-01-19 17:22:37 +01:00
|
|
|
uint8_t pins[5];
|
2021-10-11 02:19:33 +02:00
|
|
|
if (!bus->getPins(pins)) continue;
|
|
|
|
BusDigital* bd = static_cast<BusDigital*>(bus);
|
2021-01-19 17:22:37 +01:00
|
|
|
if (pins[0] == 3) bd->reinit();
|
2021-06-24 02:29:14 +02:00
|
|
|
#endif
|
2021-02-24 20:23:32 +01:00
|
|
|
}
|
2021-09-12 01:00:54 +02:00
|
|
|
|
2021-10-11 02:19:33 +02:00
|
|
|
//segments are created in makeAutoSegments();
|
2020-01-02 22:10:59 +01:00
|
|
|
|
2019-02-09 16:37:20 +01:00
|
|
|
setBrightness(_brightness);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WS2812FX::service() {
|
2019-11-26 20:41:15 +01:00
|
|
|
uint32_t nowUp = millis(); // Be aware, millis() rolls over every 49 days
|
|
|
|
now = nowUp + timebase;
|
|
|
|
if (nowUp - _lastShow < MIN_SHOW_DELAY) return;
|
2019-02-11 23:49:04 +01:00
|
|
|
bool doShow = false;
|
2020-05-10 23:58:50 +02:00
|
|
|
|
2019-06-20 14:40:12 +02:00
|
|
|
for(uint8_t i=0; i < MAX_NUM_SEGMENTS; i++)
|
2019-02-11 23:49:04 +01:00
|
|
|
{
|
2022-03-28 22:36:58 +02:00
|
|
|
//if (realtimeMode && useMainSegmentOnly && i == getMainSegmentId()) continue;
|
2022-03-25 16:36:05 +01:00
|
|
|
|
2019-02-11 23:49:04 +01:00
|
|
|
_segment_index = i;
|
2022-07-12 18:10:07 +02:00
|
|
|
Segment &seg = _segments[_segment_index];
|
2020-12-10 02:55:14 +01:00
|
|
|
|
|
|
|
// reset the segment runtime data if needed, called before isActive to ensure deleted
|
|
|
|
// segment's buffers are cleared
|
2022-07-12 18:10:07 +02:00
|
|
|
seg.runtime.resetIfRequired();
|
2020-12-10 02:55:14 +01:00
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
if (!seg.isActive()) continue;
|
2021-01-09 00:35:48 +01:00
|
|
|
|
2022-03-10 20:40:48 +01:00
|
|
|
// last condition ensures all solid segments are updated at the same time
|
2022-07-12 18:10:07 +02:00
|
|
|
if(nowUp > seg.runtime.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC))
|
2019-02-09 16:37:20 +01:00
|
|
|
{
|
2022-07-12 18:10:07 +02:00
|
|
|
if (seg.grouping == 0) seg.grouping = 1; //sanity check
|
2021-01-09 00:35:48 +01:00
|
|
|
doShow = true;
|
|
|
|
uint16_t delay = FRAMETIME;
|
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
if (!seg.getOption(SEG_OPTION_FREEZE)) { //only run effect function if not frozen
|
2022-07-10 22:23:25 +02:00
|
|
|
_virtualSegmentLength = seg.virtualLength();
|
2022-07-12 18:10:07 +02:00
|
|
|
_bri_t = seg.getOption(SEG_OPTION_ON) ? seg.opacity : 0;
|
2022-07-10 22:23:25 +02:00
|
|
|
uint8_t _cct_t = seg.cct;
|
|
|
|
_colors_t[0] = seg.colors[0];
|
|
|
|
_colors_t[1] = seg.colors[1];
|
|
|
|
_colors_t[2] = seg.colors[2];
|
2022-07-12 18:10:07 +02:00
|
|
|
seg.transition.handleTransition(&seg, _bri_t, _cct_t, _colors_t);
|
2022-07-10 22:23:25 +02:00
|
|
|
|
2021-11-27 01:33:48 +01:00
|
|
|
if (!cctFromRgb || correctWB) busses.setSegmentCCT(_cct_t, correctWB);
|
2022-03-05 01:05:26 +01:00
|
|
|
for (uint8_t c = 0; c < NUM_COLORS; c++) {
|
|
|
|
_colors_t[c] = gamma32(_colors_t[c]);
|
|
|
|
}
|
2021-01-09 00:35:48 +01:00
|
|
|
handle_palette();
|
2022-03-08 02:16:33 +01:00
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
delay = (*_mode[seg.mode])();
|
2022-07-12 18:10:07 +02:00
|
|
|
if (seg.mode != FX_MODE_HALLOWEEN_EYES) seg.runtime.call++;
|
2019-06-20 14:40:12 +02:00
|
|
|
}
|
2021-01-09 00:35:48 +01:00
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
seg.runtime.next_time = nowUp + delay;
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 13:13:54 +02:00
|
|
|
_virtualSegmentLength = 0;
|
2021-11-24 11:02:25 +01:00
|
|
|
busses.setSegmentCCT(-1);
|
2019-02-11 23:49:04 +01:00
|
|
|
if(doShow) {
|
2019-03-05 10:59:15 +01:00
|
|
|
yield();
|
2019-02-11 23:49:04 +01:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
_triggered = false;
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
void WS2812FX::setPixelColor(int i, uint32_t col)
|
2019-02-09 16:37:20 +01:00
|
|
|
{
|
2022-07-10 22:23:25 +02:00
|
|
|
if (i >= _length) return;
|
2020-03-22 17:45:09 +01:00
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
// if realtime mode is active and applying to main segment
|
|
|
|
if (realtimeMode && useMainSegmentOnly) {
|
2022-07-12 18:10:07 +02:00
|
|
|
Segment &seg = _segments[_mainSegment];
|
|
|
|
uint16_t len = seg.length(); // length of segment in number of pixels
|
|
|
|
|
|
|
|
if (seg.opacity < 255) {
|
|
|
|
byte r = scale8(R(col), seg.opacity);
|
|
|
|
byte g = scale8(G(col), seg.opacity);
|
|
|
|
byte b = scale8(B(col), seg.opacity);
|
|
|
|
byte w = scale8(W(col), seg.opacity);
|
|
|
|
col = RGBW32(r, g, b, w);
|
|
|
|
}
|
2021-10-23 15:41:35 +02:00
|
|
|
|
2022-03-25 16:36:05 +01:00
|
|
|
// get physical pixel address (taking into account start, grouping, spacing [and offset])
|
2022-07-12 18:10:07 +02:00
|
|
|
i = i * seg.groupLength();
|
|
|
|
if (seg.getOption(SEG_OPTION_REVERSED)) { // is segment reversed?
|
|
|
|
if (seg.getOption(SEG_OPTION_MIRROR)) { // is segment mirrored?
|
2022-03-25 16:36:05 +01:00
|
|
|
i = (len - 1) / 2 - i; //only need to index half the pixels
|
|
|
|
} else {
|
|
|
|
i = (len - 1) - i;
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 18:10:07 +02:00
|
|
|
i += seg.start; // starting pixel in a group
|
2022-03-25 16:36:05 +01:00
|
|
|
|
|
|
|
// set all the pixels in the group
|
2022-07-12 18:10:07 +02:00
|
|
|
for (uint16_t j = 0; j < seg.grouping; j++) {
|
|
|
|
uint16_t indexSet = i + ((seg.getOption(SEG_OPTION_REVERSED)) ? -j : j);
|
|
|
|
if (indexSet >= seg.start && indexSet < seg.stop) {
|
|
|
|
|
|
|
|
if (seg.getOption(SEG_OPTION_MIRROR)) { //set the corresponding mirrored pixel
|
|
|
|
uint16_t indexMir = seg.stop - indexSet + seg.start - 1;
|
|
|
|
indexMir += seg.offset; // offset/phase
|
|
|
|
if (indexMir >= seg.stop) indexMir -= len; // wrap
|
2021-04-06 22:30:23 +02:00
|
|
|
if (indexMir < customMappingSize) indexMir = customMappingTable[indexMir];
|
2022-03-25 16:36:05 +01:00
|
|
|
|
2021-12-02 13:09:53 +01:00
|
|
|
busses.setPixelColor(indexMir, col);
|
2020-08-20 00:04:02 +02:00
|
|
|
}
|
2022-07-12 18:10:07 +02:00
|
|
|
indexSet += seg.offset; // offset/phase
|
|
|
|
if (indexSet >= seg.stop) indexSet -= len; // wrap
|
2021-04-06 07:48:12 +02:00
|
|
|
if (indexSet < customMappingSize) indexSet = customMappingTable[indexSet];
|
2022-03-25 16:36:05 +01:00
|
|
|
|
2021-12-02 13:09:53 +01:00
|
|
|
busses.setPixelColor(indexSet, col);
|
2020-08-07 00:50:19 +02:00
|
|
|
}
|
2020-01-14 10:57:23 +01:00
|
|
|
}
|
2022-03-25 16:36:05 +01:00
|
|
|
} else {
|
2020-03-22 17:45:09 +01:00
|
|
|
if (i < customMappingSize) i = customMappingTable[i];
|
2022-07-10 22:23:25 +02:00
|
|
|
busses.setPixelColor(i, col);
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
uint32_t WS2812FX::getPixelColor(uint16_t i)
|
|
|
|
{
|
|
|
|
if (i >= _length) return 0;
|
|
|
|
//#ifndef WLED_DISABLE_2D
|
|
|
|
//if (isMatrix) return getPixelColorXY(i%matrixWidth, i/matrixWidth); // compatibility w/ non-effect fn
|
|
|
|
//#endif
|
|
|
|
if (i < customMappingSize) i = customMappingTable[i];
|
|
|
|
return busses.getPixelColor(i);
|
|
|
|
}
|
|
|
|
|
2019-02-09 16:37:20 +01:00
|
|
|
|
|
|
|
//DISCLAIMER
|
|
|
|
//The following function attemps to calculate the current LED power usage,
|
|
|
|
//and will limit the brightness to stay below a set amperage threshold.
|
|
|
|
//It is NOT a measurement and NOT guaranteed to stay within the ablMilliampsMax margin.
|
|
|
|
//Stay safe with high amperage and have a reasonable safety margin!
|
|
|
|
//I am NOT to be held liable for burned down garages!
|
|
|
|
|
2019-11-12 19:33:34 +01:00
|
|
|
//fine tune power estimation constants for your setup
|
2019-02-09 16:37:20 +01:00
|
|
|
#define MA_FOR_ESP 100 //how much mA does the ESP use (Wemos D1 about 80mA, ESP32 about 120mA)
|
|
|
|
//you can set it to 0 if the ESP is powered by USB and the LEDs by external
|
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
void WS2812FX::estimateCurrentAndLimitBri() {
|
2019-02-09 16:37:20 +01:00
|
|
|
//power limit calculation
|
|
|
|
//each LED can draw up 195075 "power units" (approx. 53mA)
|
|
|
|
//one PU is the power it takes to have 1 channel 1 step brighter per brightness step
|
|
|
|
//so A=2,R=255,G=0,B=0 would use 510 PU per LED (1mA is about 3700 PU)
|
2020-01-13 18:09:20 +01:00
|
|
|
bool useWackyWS2815PowerModel = false;
|
|
|
|
byte actualMilliampsPerLed = milliampsPerLed;
|
|
|
|
|
|
|
|
if(milliampsPerLed == 255) {
|
|
|
|
useWackyWS2815PowerModel = true;
|
|
|
|
actualMilliampsPerLed = 12; // from testing an actual strip
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
if (ablMilliampsMax < 150 || actualMilliampsPerLed == 0) { //0 mA per LED and too low numbers turn off calculation
|
|
|
|
currentMilliamps = 0;
|
|
|
|
busses.setBrightness(_brightness);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
uint16_t pLen = getLengthPhysical();
|
|
|
|
uint32_t puPerMilliamp = 195075 / actualMilliampsPerLed;
|
|
|
|
uint32_t powerBudget = (ablMilliampsMax - MA_FOR_ESP) * puPerMilliamp; //100mA for ESP power
|
|
|
|
if (powerBudget > puPerMilliamp * pLen) { //each LED uses about 1mA in standby, exclude that from power budget
|
|
|
|
powerBudget -= puPerMilliamp * pLen;
|
|
|
|
} else {
|
|
|
|
powerBudget = 0;
|
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
uint32_t powerSum = 0;
|
|
|
|
|
|
|
|
for (uint8_t b = 0; b < busses.getNumBusses(); b++) {
|
|
|
|
Bus *bus = busses.getBus(b);
|
|
|
|
if (bus->getType() >= TYPE_NET_DDP_RGB) continue; //exclude non-physical network busses
|
|
|
|
uint16_t len = bus->getLength();
|
|
|
|
uint32_t busPowerSum = 0;
|
|
|
|
for (uint16_t i = 0; i < len; i++) { //sum up the usage of each LED
|
|
|
|
uint32_t c = bus->getPixelColor(i);
|
2021-10-26 20:35:45 +02:00
|
|
|
byte r = R(c), g = G(c), b = B(c), w = W(c);
|
2020-01-13 18:09:20 +01:00
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
if(useWackyWS2815PowerModel) { //ignore white component on WS2815 power calculation
|
|
|
|
busPowerSum += (MAX(MAX(r,g),b)) * 3;
|
|
|
|
} else {
|
|
|
|
busPowerSum += (r + g + b + w);
|
2020-01-13 18:09:20 +01:00
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
if (bus->isRgbw()) { //RGBW led total output with white LEDs enabled is still 50mA, so each channel uses less
|
|
|
|
busPowerSum *= 3;
|
|
|
|
busPowerSum = busPowerSum >> 2; //same as /= 4
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
2021-10-11 02:19:53 +02:00
|
|
|
powerSum += busPowerSum;
|
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
uint32_t powerSum0 = powerSum;
|
|
|
|
powerSum *= _brightness;
|
|
|
|
|
|
|
|
if (powerSum > powerBudget) //scale brightness down to stay in current limit
|
|
|
|
{
|
|
|
|
float scale = (float)powerBudget / (float)powerSum;
|
|
|
|
uint16_t scaleI = scale * 255;
|
|
|
|
uint8_t scaleB = (scaleI > 255) ? 255 : scaleI;
|
|
|
|
uint8_t newBri = scale8(_brightness, scaleB);
|
|
|
|
busses.setBrightness(newBri); //to keep brightness uniform, sets virtual busses too
|
|
|
|
currentMilliamps = (powerSum0 * newBri) / puPerMilliamp;
|
2019-02-09 16:37:20 +01:00
|
|
|
} else {
|
2021-10-11 02:19:53 +02:00
|
|
|
currentMilliamps = powerSum / puPerMilliamp;
|
2021-01-21 01:21:16 +01:00
|
|
|
busses.setBrightness(_brightness);
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
2021-10-11 02:19:53 +02:00
|
|
|
currentMilliamps += MA_FOR_ESP; //add power of ESP back to estimate
|
|
|
|
currentMilliamps += pLen; //add standby power back to estimate
|
|
|
|
}
|
|
|
|
|
|
|
|
void WS2812FX::show(void) {
|
|
|
|
|
|
|
|
// avoid race condition, caputre _callback value
|
|
|
|
show_callback callback = _callback;
|
|
|
|
if (callback) callback();
|
|
|
|
|
|
|
|
estimateCurrentAndLimitBri();
|
2019-02-09 16:37:20 +01:00
|
|
|
|
2020-12-10 05:29:53 +01:00
|
|
|
// some buses send asynchronously and this method will return before
|
|
|
|
// all of the data has been sent.
|
|
|
|
// See https://github.com/Makuna/NeoPixelBus/wiki/ESP32-NeoMethods#neoesp32rmt-methods
|
2021-01-21 01:21:16 +01:00
|
|
|
busses.show();
|
2021-02-05 01:33:26 +01:00
|
|
|
unsigned long now = millis();
|
|
|
|
unsigned long diff = now - _lastShow;
|
|
|
|
uint16_t fpsCurr = 200;
|
|
|
|
if (diff > 0) fpsCurr = 1000 / diff;
|
|
|
|
_cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2;
|
|
|
|
_lastShow = now;
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 05:29:53 +01:00
|
|
|
/**
|
|
|
|
* Returns a true value if any of the strips are still being updated.
|
|
|
|
* On some hardware (ESP32), strip updates are done asynchronously.
|
|
|
|
*/
|
|
|
|
bool WS2812FX::isUpdating() {
|
2021-01-21 01:21:16 +01:00
|
|
|
return !busses.canAllShow();
|
2020-12-10 05:29:53 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 01:33:26 +01:00
|
|
|
/**
|
|
|
|
* Returns the refresh rate of the LED strip. Useful for finding out whether a given setup is fast enough.
|
|
|
|
* Only updates on show() or is set to 0 fps if last show is more than 2 secs ago, so accurary varies
|
|
|
|
*/
|
|
|
|
uint16_t WS2812FX::getFps() {
|
|
|
|
if (millis() - _lastShow > 2000) return 0;
|
|
|
|
return _cumulativeFps +1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 01:30:27 +01:00
|
|
|
void WS2812FX::setTargetFps(uint8_t fps) {
|
2021-12-30 01:48:27 +01:00
|
|
|
if (fps > 0 && fps <= 120) _targetFps = fps;
|
2021-12-25 01:30:27 +01:00
|
|
|
_frametime = 1000 / _targetFps;
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:40:12 +02:00
|
|
|
void WS2812FX::setMode(uint8_t segid, uint8_t m) {
|
|
|
|
if (segid >= MAX_NUM_SEGMENTS) return;
|
|
|
|
|
2022-06-26 23:01:22 +02:00
|
|
|
if (m >= getModeCount()) m = getModeCount() - 1;
|
2019-06-20 14:40:12 +02:00
|
|
|
|
|
|
|
if (_segments[segid].mode != m)
|
|
|
|
{
|
2022-07-12 18:10:07 +02:00
|
|
|
_segments[segid].runtime.markForReset();
|
2019-06-20 14:40:12 +02:00
|
|
|
_segments[segid].mode = m;
|
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 22:24:11 +01:00
|
|
|
//applies to all active and selected segments
|
2019-06-20 14:40:12 +02:00
|
|
|
void WS2812FX::setColor(uint8_t slot, uint32_t c) {
|
|
|
|
if (slot >= NUM_COLORS) return;
|
2020-03-14 11:28:42 +01:00
|
|
|
|
2022-02-20 22:24:11 +01:00
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
|
|
|
if (_segments[i].isActive() && _segments[i].isSelected()) {
|
2022-07-10 22:23:25 +02:00
|
|
|
_segments[i].setColor(slot, c);
|
2019-12-02 12:41:35 +01:00
|
|
|
}
|
2020-03-14 11:28:42 +01:00
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:20:07 +01:00
|
|
|
void WS2812FX::setCCT(uint16_t k) {
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
|
|
|
if (_segments[i].isActive() && _segments[i].isSelected()) {
|
2022-07-10 22:23:25 +02:00
|
|
|
_segments[i].setCCT(k);
|
2022-02-23 19:20:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 20:40:48 +01:00
|
|
|
void WS2812FX::setBrightness(uint8_t b, bool direct) {
|
2020-12-14 23:32:57 +01:00
|
|
|
if (gammaCorrectBri) b = gamma8(b);
|
2019-02-09 16:37:20 +01:00
|
|
|
if (_brightness == b) return;
|
2020-12-14 23:32:57 +01:00
|
|
|
_brightness = b;
|
|
|
|
if (_brightness == 0) { //unfreeze all segments on power off
|
2020-08-29 22:26:39 +02:00
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
|
|
|
_segments[i].setOption(SEG_OPTION_FREEZE, false);
|
|
|
|
}
|
|
|
|
}
|
2022-03-10 20:40:48 +01:00
|
|
|
if (direct) {
|
|
|
|
// would be dangerous if applied immediately (could exceed ABL), but will not output until the next show()
|
|
|
|
busses.setBrightness(b);
|
|
|
|
} else {
|
|
|
|
unsigned long t = millis();
|
2022-07-12 18:10:07 +02:00
|
|
|
if (_segments[0].runtime.next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) show(); //apply brightness change immediately if no refresh soon
|
2022-03-10 20:40:48 +01:00
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:20:07 +01:00
|
|
|
uint8_t WS2812FX::getFirstSelectedSegId(void)
|
|
|
|
{
|
2019-06-20 14:40:12 +02:00
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
2022-02-23 19:20:07 +01:00
|
|
|
if (_segments[i].isActive() && _segments[i].isSelected()) return i;
|
2019-06-20 14:40:12 +02:00
|
|
|
}
|
2022-02-23 19:20:07 +01:00
|
|
|
// if none selected, use the main segment
|
|
|
|
return getMainSegmentId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WS2812FX::setMainSegmentId(uint8_t n) {
|
|
|
|
if (n >= MAX_NUM_SEGMENTS) return;
|
|
|
|
//use supplied n if active, or first active
|
2022-02-20 22:24:11 +01:00
|
|
|
if (_segments[n].isActive()) {
|
|
|
|
_mainSegment = n; return;
|
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
2019-06-20 14:40:12 +02:00
|
|
|
{
|
2022-02-20 22:24:11 +01:00
|
|
|
if (_segments[i].isActive()) {
|
|
|
|
_mainSegment = i; return;
|
|
|
|
}
|
2019-06-20 14:40:12 +02:00
|
|
|
}
|
2022-02-20 22:24:11 +01:00
|
|
|
_mainSegment = 0;
|
|
|
|
return;
|
|
|
|
}
|
2019-06-20 14:40:12 +02:00
|
|
|
|
2022-02-20 22:24:11 +01:00
|
|
|
uint8_t WS2812FX::getLastActiveSegmentId(void) {
|
|
|
|
for (uint8_t i = MAX_NUM_SEGMENTS -1; i > 0; i--) {
|
2020-06-22 12:30:31 +02:00
|
|
|
if (_segments[i].isActive()) return i;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-06-20 14:40:12 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 21:22:50 +02:00
|
|
|
uint8_t WS2812FX::getActiveSegmentsNum(void) {
|
|
|
|
uint8_t c = 0;
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
|
|
|
if (_segments[i].isActive()) c++;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:19:53 +02:00
|
|
|
uint16_t WS2812FX::getLengthPhysical(void) {
|
|
|
|
uint16_t len = 0;
|
|
|
|
for (uint8_t b = 0; b < busses.getNumBusses(); b++) {
|
|
|
|
Bus *bus = busses.getBus(b);
|
|
|
|
if (bus->getType() >= TYPE_NET_DDP_RGB) continue; //exclude non-physical network busses
|
|
|
|
len += bus->getLength();
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2022-02-20 22:24:11 +01:00
|
|
|
//used for JSON API info.leds.rgbw. Little practical use, deprecate with info.leds.rgbw.
|
|
|
|
//returns if there is an RGBW bus (supports RGB and White, not only white)
|
|
|
|
//not influenced by auto-white mode, also true if white slider does not affect output white channel
|
|
|
|
bool WS2812FX::hasRGBWBus(void) {
|
|
|
|
for (uint8_t b = 0; b < busses.getNumBusses(); b++) {
|
|
|
|
Bus *bus = busses.getBus(b);
|
|
|
|
if (bus == nullptr || bus->getLength()==0) break;
|
|
|
|
switch (bus->getType()) {
|
|
|
|
case TYPE_SK6812_RGBW:
|
|
|
|
case TYPE_TM1814:
|
|
|
|
case TYPE_ANALOG_4CH:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-28 04:01:58 +01:00
|
|
|
bool WS2812FX::hasCCTBus(void) {
|
|
|
|
if (cctFromRgb && !correctWB) return false;
|
|
|
|
for (uint8_t b = 0; b < busses.getNumBusses(); b++) {
|
|
|
|
Bus *bus = busses.getBus(b);
|
|
|
|
if (bus == nullptr || bus->getLength()==0) break;
|
|
|
|
switch (bus->getType()) {
|
|
|
|
case TYPE_ANALOG_5CH:
|
|
|
|
case TYPE_ANALOG_2CH:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-08 10:50:48 +02:00
|
|
|
void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping, uint8_t spacing, uint16_t offset, uint16_t startY, uint16_t stopY) {
|
2019-03-06 01:20:38 +01:00
|
|
|
if (n >= MAX_NUM_SEGMENTS) return;
|
|
|
|
Segment& seg = _segments[n];
|
2020-01-05 22:30:27 +01:00
|
|
|
|
2020-01-14 10:57:23 +01:00
|
|
|
//return if neither bounds nor grouping have changed
|
2022-03-05 01:05:26 +01:00
|
|
|
bool boundsUnchanged = (seg.start == i1 && seg.stop == i2);
|
2022-05-08 10:50:48 +02:00
|
|
|
if (isMatrix) {
|
|
|
|
boundsUnchanged &= (seg.startY == startY && seg.stopY == stopY);
|
|
|
|
}
|
2022-03-05 01:05:26 +01:00
|
|
|
if (boundsUnchanged
|
2021-12-02 13:09:53 +01:00
|
|
|
&& (!grouping || (seg.grouping == grouping && seg.spacing == spacing))
|
|
|
|
&& (offset == UINT16_MAX || offset == seg.offset)) return;
|
2020-01-02 20:41:15 +01:00
|
|
|
|
2020-01-14 10:57:23 +01:00
|
|
|
if (seg.stop) setRange(seg.start, seg.stop -1, 0); //turn old segment range off
|
|
|
|
if (i2 <= i1) //disable segment
|
|
|
|
{
|
2021-09-18 13:15:19 +02:00
|
|
|
seg.stop = 0;
|
|
|
|
if (seg.name) {
|
|
|
|
delete[] seg.name;
|
|
|
|
seg.name = nullptr;
|
|
|
|
}
|
2022-02-23 19:20:07 +01:00
|
|
|
// if main segment is deleted, set first active as main segment
|
2022-02-20 22:24:11 +01:00
|
|
|
if (n == _mainSegment) setMainSegmentId(0);
|
2020-01-27 00:45:30 +01:00
|
|
|
return;
|
2019-12-05 07:59:05 +01:00
|
|
|
}
|
2022-05-08 10:50:48 +02:00
|
|
|
if (isMatrix) {
|
2022-07-10 22:23:25 +02:00
|
|
|
#ifndef WLED_DISABLE_2D
|
2022-05-08 10:50:48 +02:00
|
|
|
if (i1 < matrixWidth) seg.start = i1;
|
|
|
|
seg.stop = i2 > matrixWidth ? matrixWidth : i2;
|
|
|
|
if (startY < matrixHeight) seg.startY = startY;
|
|
|
|
seg.stopY = stopY > matrixHeight ? matrixHeight : MAX(1,stopY);
|
2022-07-10 22:23:25 +02:00
|
|
|
#endif
|
2022-05-08 10:50:48 +02:00
|
|
|
} else {
|
|
|
|
if (i1 < _length) seg.start = i1;
|
|
|
|
seg.stop = i2 > _length ? _length : i2;
|
|
|
|
seg.startY = 0;
|
|
|
|
seg.stopY = 1;
|
|
|
|
}
|
2020-01-14 10:57:23 +01:00
|
|
|
if (grouping) {
|
|
|
|
seg.grouping = grouping;
|
|
|
|
seg.spacing = spacing;
|
2019-06-20 14:40:12 +02:00
|
|
|
}
|
2021-12-02 13:09:53 +01:00
|
|
|
if (offset < UINT16_MAX) seg.offset = offset;
|
2022-07-12 18:10:07 +02:00
|
|
|
_segments[n].runtime.markForReset();
|
2022-03-05 01:05:26 +01:00
|
|
|
if (!boundsUnchanged) seg.refreshLightCapabilities();
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 11:29:03 +01:00
|
|
|
void WS2812FX::restartRuntime() {
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) {
|
2022-07-12 18:10:07 +02:00
|
|
|
_segments[i].runtime.markForReset();
|
2021-12-20 11:29:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 16:37:20 +01:00
|
|
|
void WS2812FX::resetSegments() {
|
2021-12-21 20:43:49 +01:00
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) if (_segments[i].name) delete[] _segments[i].name;
|
2022-02-20 22:24:11 +01:00
|
|
|
_mainSegment = 0;
|
2019-02-09 16:37:20 +01:00
|
|
|
memset(_segments, 0, sizeof(_segments));
|
2022-07-12 18:10:07 +02:00
|
|
|
//memset(_segmentruntimes, 0, sizeof(_segmentruntimes));
|
2019-02-09 16:37:20 +01:00
|
|
|
_segment_index = 0;
|
2019-03-06 01:20:38 +01:00
|
|
|
_segments[0].mode = DEFAULT_MODE;
|
|
|
|
_segments[0].colors[0] = DEFAULT_COLOR;
|
|
|
|
_segments[0].start = 0;
|
|
|
|
_segments[0].speed = DEFAULT_SPEED;
|
2020-09-07 21:01:10 +02:00
|
|
|
_segments[0].intensity = DEFAULT_INTENSITY;
|
2020-01-14 10:57:23 +01:00
|
|
|
_segments[0].stop = _length;
|
|
|
|
_segments[0].grouping = 1;
|
2020-04-24 23:35:39 +02:00
|
|
|
_segments[0].setOption(SEG_OPTION_SELECTED, 1);
|
|
|
|
_segments[0].setOption(SEG_OPTION_ON, 1);
|
2020-04-23 23:52:33 +02:00
|
|
|
_segments[0].opacity = 255;
|
2021-11-26 20:18:38 +01:00
|
|
|
_segments[0].cct = 127;
|
2022-05-21 13:19:11 +02:00
|
|
|
_segments[0].custom1 = DEFAULT_C1;
|
|
|
|
_segments[0].custom2 = DEFAULT_C2;
|
|
|
|
_segments[0].custom3 = DEFAULT_C3;
|
2020-04-23 23:52:33 +02:00
|
|
|
|
2019-12-13 01:23:07 +01:00
|
|
|
for (uint16_t i = 1; i < MAX_NUM_SEGMENTS; i++)
|
2019-02-09 16:37:20 +01:00
|
|
|
{
|
2022-07-10 22:23:25 +02:00
|
|
|
_segments[i].colors[0] = _segments[i].color_wheel(i*51);
|
2020-01-14 10:57:23 +01:00
|
|
|
_segments[i].grouping = 1;
|
2020-04-24 23:35:39 +02:00
|
|
|
_segments[i].setOption(SEG_OPTION_ON, 1);
|
2020-04-23 23:52:33 +02:00
|
|
|
_segments[i].opacity = 255;
|
2021-11-26 20:18:38 +01:00
|
|
|
_segments[i].cct = 127;
|
2020-09-07 21:01:10 +02:00
|
|
|
_segments[i].speed = DEFAULT_SPEED;
|
|
|
|
_segments[i].intensity = DEFAULT_INTENSITY;
|
2022-05-21 13:19:11 +02:00
|
|
|
_segments[i].custom1 = DEFAULT_C1;
|
|
|
|
_segments[i].custom2 = DEFAULT_C2;
|
|
|
|
_segments[i].custom3 = DEFAULT_C3;
|
2022-07-12 18:10:07 +02:00
|
|
|
_segments[i].runtime.markForReset();
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
2022-07-12 18:10:07 +02:00
|
|
|
_segments[0].runtime.markForReset();
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:09:16 +01:00
|
|
|
void WS2812FX::makeAutoSegments(bool forceReset) {
|
2022-05-08 14:28:02 +02:00
|
|
|
if (isMatrix) {
|
2022-07-10 22:23:25 +02:00
|
|
|
#ifndef WLED_DISABLE_2D
|
2022-05-08 14:28:02 +02:00
|
|
|
// only create 1 2D segment
|
|
|
|
uint8_t mainSeg = getMainSegmentId();
|
|
|
|
if (forceReset) {
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) {
|
|
|
|
setSegment(i, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getActiveSegmentsNum() < 2) {
|
|
|
|
setSegment(mainSeg, 0, matrixWidth, 1, 0, 0, 0, matrixHeight);
|
|
|
|
}
|
2022-07-10 22:23:25 +02:00
|
|
|
#endif
|
2022-05-08 14:28:02 +02:00
|
|
|
} else if (autoSegments) { //make one segment per bus
|
2021-10-30 14:42:17 +02:00
|
|
|
uint16_t segStarts[MAX_NUM_SEGMENTS] = {0};
|
|
|
|
uint16_t segStops [MAX_NUM_SEGMENTS] = {0};
|
2021-10-11 02:19:33 +02:00
|
|
|
uint8_t s = 0;
|
|
|
|
for (uint8_t i = 0; i < busses.getNumBusses(); i++) {
|
|
|
|
Bus* b = busses.getBus(i);
|
|
|
|
|
|
|
|
segStarts[s] = b->getStart();
|
|
|
|
segStops[s] = segStarts[s] + b->getLength();
|
|
|
|
|
|
|
|
//check for overlap with previous segments
|
|
|
|
for (uint8_t j = 0; j < s; j++) {
|
|
|
|
if (segStops[j] > segStarts[s] && segStarts[j] < segStops[s]) {
|
|
|
|
//segments overlap, merge
|
|
|
|
segStarts[j] = min(segStarts[s],segStarts[j]);
|
|
|
|
segStops [j] = max(segStops [s],segStops [j]); segStops[s] = 0;
|
|
|
|
s--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) {
|
2022-07-10 22:23:25 +02:00
|
|
|
_segments[i].setOption(SEG_OPTION_SELECTED, true);
|
2021-10-11 02:19:33 +02:00
|
|
|
setSegment(i, segStarts[i], segStops[i]);
|
2021-09-12 01:00:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-10 16:09:16 +01:00
|
|
|
//expand the main seg to the entire length, but only if there are no other segments, or reset is forced
|
2021-10-11 02:19:33 +02:00
|
|
|
uint8_t mainSeg = getMainSegmentId();
|
2022-02-20 22:24:11 +01:00
|
|
|
|
2022-02-10 16:09:16 +01:00
|
|
|
if (forceReset) {
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) {
|
|
|
|
setSegment(i, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 02:19:33 +02:00
|
|
|
if (getActiveSegmentsNum() < 2) {
|
|
|
|
setSegment(mainSeg, 0, _length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fixInvalidSegments();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WS2812FX::fixInvalidSegments() {
|
|
|
|
//make sure no segment is longer than total (sanity check)
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
|
|
|
if (_segments[i].start >= _length) setSegment(i, 0, 0);
|
|
|
|
if (_segments[i].stop > _length) setSegment(i, _segments[i].start, _length);
|
2022-03-05 01:05:26 +01:00
|
|
|
// this is always called as the last step after finalizeInit(), update covered bus types
|
|
|
|
getSegment(i).refreshLightCapabilities();
|
2021-10-11 02:19:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//true if all segments align with a bus, or if a segment covers the total length
|
|
|
|
bool WS2812FX::checkSegmentAlignment() {
|
|
|
|
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
|
|
|
if (_segments[i].start >= _segments[i].stop) continue; //inactive segment
|
|
|
|
bool aligned = false;
|
|
|
|
for (uint8_t b = 0; b<busses.getNumBusses(); b++) {
|
|
|
|
Bus *bus = busses.getBus(b);
|
|
|
|
if (_segments[i].start == bus->getStart() && _segments[i].stop == bus->getStart() + bus->getLength()) aligned = true;
|
|
|
|
}
|
|
|
|
if (_segments[i].start == 0 && _segments[i].stop == _length) aligned = true;
|
|
|
|
if (!aligned) return false;
|
2021-02-27 12:06:14 +01:00
|
|
|
}
|
2021-10-11 02:19:33 +02:00
|
|
|
return true;
|
2021-02-27 12:06:14 +01:00
|
|
|
}
|
|
|
|
|
2020-08-29 22:26:39 +02:00
|
|
|
//After this function is called, setPixelColor() will use that segment (offsets, grouping, ... will apply)
|
2022-01-31 20:35:11 +01:00
|
|
|
//Note: If called in an interrupt (e.g. JSON API), original segment must be restored,
|
2021-12-03 20:36:37 +01:00
|
|
|
//otherwise it can lead to a crash on ESP32 because _segment_index is modified while in use by the main thread
|
2022-01-31 20:35:11 +01:00
|
|
|
uint8_t WS2812FX::setPixelSegment(uint8_t n)
|
2020-08-29 22:26:39 +02:00
|
|
|
{
|
2022-01-31 20:35:11 +01:00
|
|
|
uint8_t prevSegId = _segment_index;
|
2020-08-29 22:26:39 +02:00
|
|
|
if (n < MAX_NUM_SEGMENTS) {
|
|
|
|
_segment_index = n;
|
2022-07-12 18:10:07 +02:00
|
|
|
_virtualSegmentLength = _segments[_segment_index].virtualLength();
|
2020-08-29 22:26:39 +02:00
|
|
|
}
|
2022-01-31 20:35:11 +01:00
|
|
|
return prevSegId;
|
2020-08-29 22:26:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 16:37:20 +01:00
|
|
|
void WS2812FX::setRange(uint16_t i, uint16_t i2, uint32_t col)
|
|
|
|
{
|
|
|
|
if (i2 >= i)
|
|
|
|
{
|
2020-01-02 22:10:59 +01:00
|
|
|
for (uint16_t x = i; x <= i2; x++) setPixelColor(x, col);
|
2019-02-09 16:37:20 +01:00
|
|
|
} else
|
|
|
|
{
|
2020-01-02 22:10:59 +01:00
|
|
|
for (uint16_t x = i2; x <= i; x++) setPixelColor(x, col);
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WS2812FX::setTransitionMode(bool t)
|
|
|
|
{
|
2022-07-12 18:10:07 +02:00
|
|
|
unsigned long waitMax = millis() + 22; //refresh after 20 ms if transition enabled
|
2020-06-22 12:30:31 +02:00
|
|
|
for (uint16_t i = 0; i < MAX_NUM_SEGMENTS; i++)
|
|
|
|
{
|
2022-07-12 18:10:07 +02:00
|
|
|
if (_segments[i].isActive() && !_segments[i].getOption(SEG_OPTION_TRANSITIONAL)) {
|
|
|
|
if (t) {
|
|
|
|
_segments[i].transition.transitionStart = millis();
|
|
|
|
_segments[i].transition.transitionDur = strip.getTransition();
|
|
|
|
}
|
|
|
|
_segments[i].setOption(SEG_OPTION_TRANSITIONAL, t);
|
|
|
|
if (t && _segments[i].mode == FX_MODE_STATIC && _segments[i].runtime.next_time > waitMax) {
|
|
|
|
_segments[i].runtime.next_time = waitMax;
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 12:30:31 +02:00
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
void WS2812FX::load_gradient_palette(uint8_t index)
|
2019-02-11 23:49:04 +01:00
|
|
|
{
|
2022-07-10 22:23:25 +02:00
|
|
|
byte tcp[72]; //support gradient palettes with up to 18 entries
|
|
|
|
if (index>127) {
|
|
|
|
char fileName[32];
|
|
|
|
strcpy_P(fileName, PSTR("/palette"));
|
|
|
|
if (index>128) sprintf(fileName +7, "%d", index-127);
|
|
|
|
strcat(fileName, ".json");
|
|
|
|
|
|
|
|
if (WLED_FS.exists(fileName)) {
|
|
|
|
StaticJsonDocument<1536> pDoc; // barely enough to fit 72 numbers
|
|
|
|
DEBUG_PRINT(F("Reading palette from "));
|
|
|
|
DEBUG_PRINTLN(fileName);
|
|
|
|
|
|
|
|
if (readObjectFromFile(fileName, nullptr, &pDoc)) {
|
|
|
|
JsonArray pal = pDoc[F("palette")];
|
|
|
|
if (!pal.isNull() && pal.size()>7) { // not an empty palette (at least 2 entries)
|
|
|
|
size_t palSize = MIN(pal.size(), 72);
|
|
|
|
palSize -= palSize % 4; // make sure size is multiple of 4
|
|
|
|
for (int i=0; i<palSize && pal[i].as<int>()<256; i+=4) {
|
|
|
|
tcp[ i ] = (uint8_t) pal[ i ].as<int>(); // index
|
|
|
|
tcp[i+1] = (uint8_t) pal[i+1].as<int>(); // R
|
|
|
|
tcp[i+2] = (uint8_t) pal[i+2].as<int>(); // G
|
|
|
|
tcp[i+3] = (uint8_t) pal[i+3].as<int>(); // B
|
|
|
|
}
|
|
|
|
targetPalette.loadDynamicGradientPalette(tcp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
releaseJSONBufferLock();
|
2019-02-11 23:49:04 +01:00
|
|
|
}
|
2019-02-09 16:37:20 +01:00
|
|
|
} else {
|
2022-07-10 22:23:25 +02:00
|
|
|
byte i = constrain(index, 0, GRADIENT_PALETTE_COUNT -1);
|
|
|
|
memcpy_P(tcp, (byte*)pgm_read_dword(&(gGradientPalettes[i])), 72);
|
|
|
|
targetPalette.loadDynamicGradientPalette(tcp);
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
2020-02-25 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-09 16:37:20 +01:00
|
|
|
/*
|
|
|
|
* FastLED palette modes helper function. Limitation: Due to memory reasons, multiple active segments with FastLED will disable the Palette transitions
|
|
|
|
*/
|
|
|
|
void WS2812FX::handle_palette(void)
|
|
|
|
{
|
|
|
|
bool singleSegmentMode = (_segment_index == _segment_index_palette_last);
|
|
|
|
_segment_index_palette_last = _segment_index;
|
|
|
|
|
2022-07-10 22:23:25 +02:00
|
|
|
byte paletteIndex = _segments[_segment_index].palette;
|
2020-06-06 00:57:34 +02:00
|
|
|
if (paletteIndex == 0) //default palette. Differs depending on effect
|
|
|
|
{
|
2022-07-10 22:23:25 +02:00
|
|
|
// TODO: get default palette ID from _modeData[]
|
|
|
|
switch (_segments[_segment_index].mode)
|
2020-06-06 00:57:34 +02:00
|
|
|
{
|
|
|
|
case FX_MODE_FIRE_2012 : paletteIndex = 35; break; //heat palette
|
|
|
|
case FX_MODE_COLORWAVES : paletteIndex = 26; break; //landscape 33
|
|
|
|
case FX_MODE_FILLNOISE8 : paletteIndex = 9; break; //ocean colors
|
|
|
|
case FX_MODE_NOISE16_1 : paletteIndex = 20; break; //Drywet
|
|
|
|
case FX_MODE_NOISE16_2 : paletteIndex = 43; break; //Blue cyan yellow
|
|
|
|
case FX_MODE_NOISE16_3 : paletteIndex = 35; break; //heat palette
|
|
|
|
case FX_MODE_NOISE16_4 : paletteIndex = 26; break; //landscape 33
|
|
|
|
case FX_MODE_GLITTER : paletteIndex = 11; break; //rainbow colors
|
|
|
|
case FX_MODE_SUNRISE : paletteIndex = 35; break; //heat palette
|
|
|
|
case FX_MODE_FLOW : paletteIndex = 6; break; //party
|
|
|
|
}
|
|
|
|
}
|
2022-07-10 22:23:25 +02:00
|
|
|
if (_segments[_segment_index].mode >= FX_MODE_METEOR && paletteIndex == 0) paletteIndex = 4;
|
2019-02-09 16:37:20 +01:00
|
|
|
|
|
|
|
switch (paletteIndex)
|
|
|
|
{
|
2020-06-06 00:57:34 +02:00
|
|
|
case 0: //default palette. Exceptions for specific effects above
|
|
|
|
targetPalette = PartyColors_p; break;
|
2019-02-09 16:37:20 +01:00
|
|
|
case 1: {//periodically replace palette with a random one. Doesn't work with multiple FastLED segments
|
|
|
|
if (!singleSegmentMode)
|
|
|
|
{
|
|
|
|
targetPalette = PartyColors_p; break; //fallback
|
|
|
|
}
|
2022-07-10 22:23:25 +02:00
|
|
|
if (millis() - _lastPaletteChange > 1000 + ((uint32_t)(255-_segments[_segment_index].intensity))*100)
|
2019-02-09 16:37:20 +01:00
|
|
|
{
|
|
|
|
targetPalette = CRGBPalette16(
|
|
|
|
CHSV(random8(), 255, random8(128, 255)),
|
|
|
|
CHSV(random8(), 255, random8(128, 255)),
|
|
|
|
CHSV(random8(), 192, random8(128, 255)),
|
|
|
|
CHSV(random8(), 255, random8(128, 255)));
|
|
|
|
_lastPaletteChange = millis();
|
|
|
|
} break;}
|
|
|
|
case 2: {//primary color only
|
2022-07-06 13:13:54 +02:00
|
|
|
CRGB prim = CRGB(SEGCOLOR(0));
|
2019-02-09 16:37:20 +01:00
|
|
|
targetPalette = CRGBPalette16(prim); break;}
|
2020-05-03 20:57:53 +02:00
|
|
|
case 3: {//primary + secondary
|
2022-07-06 13:13:54 +02:00
|
|
|
CRGB prim = CRGB(SEGCOLOR(0));
|
|
|
|
CRGB sec = CRGB(SEGCOLOR(1));
|
2020-05-03 20:57:53 +02:00
|
|
|
targetPalette = CRGBPalette16(prim,prim,sec,sec); break;}
|
|
|
|
case 4: {//primary + secondary + tertiary
|
2022-07-06 13:13:54 +02:00
|
|
|
CRGB prim = CRGB(SEGCOLOR(0));
|
|
|
|
CRGB sec = CRGB(SEGCOLOR(1));
|
|
|
|
CRGB ter = CRGB(SEGCOLOR(2));
|
2019-12-06 01:44:45 +01:00
|
|
|
targetPalette = CRGBPalette16(ter,sec,prim); break;}
|
2020-05-03 20:57:53 +02:00
|
|
|
case 5: {//primary + secondary (+tert if not off), more distinct
|
2022-07-06 13:13:54 +02:00
|
|
|
CRGB prim = CRGB(SEGCOLOR(0));
|
|
|
|
CRGB sec = CRGB(SEGCOLOR(1));
|
2020-05-03 20:57:53 +02:00
|
|
|
if (SEGCOLOR(2)) {
|
2022-07-06 13:13:54 +02:00
|
|
|
CRGB ter = CRGB(SEGCOLOR(2));
|
2020-05-03 20:57:53 +02:00
|
|
|
targetPalette = CRGBPalette16(prim,prim,prim,prim,prim,sec,sec,sec,sec,sec,ter,ter,ter,ter,ter,prim);
|
|
|
|
} else {
|
|
|
|
targetPalette = CRGBPalette16(prim,prim,prim,prim,prim,prim,prim,prim,sec,sec,sec,sec,sec,sec,sec,sec);
|
|
|
|
}
|
|
|
|
break;}
|
2019-02-09 16:37:20 +01:00
|
|
|
case 6: //Party colors
|
|
|
|
targetPalette = PartyColors_p; break;
|
|
|
|
case 7: //Cloud colors
|
|
|
|
targetPalette = CloudColors_p; break;
|
|
|
|
case 8: //Lava colors
|
|
|
|
targetPalette = LavaColors_p; break;
|
|
|
|
case 9: //Ocean colors
|
|
|
|
targetPalette = OceanColors_p; break;
|
|
|
|
case 10: //Forest colors
|
|
|
|
targetPalette = ForestColors_p; break;
|
|
|
|
case 11: //Rainbow colors
|
|
|
|
targetPalette = RainbowColors_p; break;
|
|
|
|
case 12: //Rainbow stripe colors
|
|
|
|
targetPalette = RainbowStripeColors_p; break;
|
|
|
|
default: //progmem palettes
|
2020-06-06 00:57:34 +02:00
|
|
|
load_gradient_palette(paletteIndex -13);
|
2019-02-09 16:37:20 +01:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:10:07 +02:00
|
|
|
if (singleSegmentMode && paletteFade && _segments[_segment_index].runtime.call > 0) //only blend if just one segment uses FastLED mode
|
2019-02-09 16:37:20 +01:00
|
|
|
{
|
|
|
|
nblendPaletteTowardPalette(currentPalette, targetPalette, 48);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
currentPalette = targetPalette;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 16:24:26 +02:00
|
|
|
|
2021-11-03 14:52:22 +01:00
|
|
|
//load custom mapping table from JSON file (called from finalizeInit() or deserializeState())
|
2021-04-04 13:54:34 +02:00
|
|
|
void WS2812FX::deserializeMap(uint8_t n) {
|
2022-05-08 10:50:48 +02:00
|
|
|
if (isMatrix) return; // 2D support creates its own ledmap
|
|
|
|
|
2021-09-12 01:15:51 +02:00
|
|
|
char fileName[32];
|
|
|
|
strcpy_P(fileName, PSTR("/ledmap"));
|
|
|
|
if (n) sprintf(fileName +7, "%d", n);
|
|
|
|
strcat(fileName, ".json");
|
2021-04-04 13:54:34 +02:00
|
|
|
bool isFile = WLED_FS.exists(fileName);
|
|
|
|
|
|
|
|
if (!isFile) {
|
|
|
|
// erase custom mapping if selecting nonexistent ledmap.json (n==0)
|
|
|
|
if (!n && customMappingTable != nullptr) {
|
|
|
|
customMappingSize = 0;
|
|
|
|
delete[] customMappingTable;
|
|
|
|
customMappingTable = nullptr;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-02-13 01:02:14 +01:00
|
|
|
|
2021-12-04 01:05:01 +01:00
|
|
|
if (!requestJSONBufferLock(7)) return;
|
2021-11-03 14:52:22 +01:00
|
|
|
|
2021-04-04 13:54:34 +02:00
|
|
|
DEBUG_PRINT(F("Reading LED map from "));
|
|
|
|
DEBUG_PRINTLN(fileName);
|
2021-02-13 01:02:14 +01:00
|
|
|
|
2021-11-03 14:52:22 +01:00
|
|
|
if (!readObjectFromFile(fileName, nullptr, &doc)) {
|
2021-11-12 23:33:10 +01:00
|
|
|
releaseJSONBufferLock();
|
2021-11-03 14:52:22 +01:00
|
|
|
return; //if file does not exist just exit
|
|
|
|
}
|
2021-02-13 01:02:14 +01:00
|
|
|
|
2021-04-04 13:54:34 +02:00
|
|
|
// erase old custom ledmap
|
2021-02-13 01:02:14 +01:00
|
|
|
if (customMappingTable != nullptr) {
|
2021-04-04 13:54:34 +02:00
|
|
|
customMappingSize = 0;
|
2021-02-13 01:02:14 +01:00
|
|
|
delete[] customMappingTable;
|
|
|
|
customMappingTable = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonArray map = doc[F("map")];
|
|
|
|
if (!map.isNull() && map.size()) { // not an empty map
|
|
|
|
customMappingSize = map.size();
|
|
|
|
customMappingTable = new uint16_t[customMappingSize];
|
|
|
|
for (uint16_t i=0; i<customMappingSize; i++) {
|
|
|
|
customMappingTable[i] = (uint16_t) map[i];
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 14:52:22 +01:00
|
|
|
|
2021-11-12 23:33:10 +01:00
|
|
|
releaseJSONBufferLock();
|
2021-02-13 01:02:14 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 11:04:40 +01:00
|
|
|
//gamma 2.8 lookup table used for color correction
|
2022-07-06 13:13:54 +02:00
|
|
|
static byte gammaT[] = {
|
2019-05-22 00:23:09 +02:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
|
|
|
|
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
|
|
|
|
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
|
|
|
|
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
|
|
|
|
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
|
|
|
|
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
|
|
|
|
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
|
|
|
|
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
|
|
|
|
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
|
|
|
|
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
|
|
|
|
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
|
|
|
|
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
|
|
|
|
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
|
|
|
|
|
2020-11-04 11:04:40 +01:00
|
|
|
uint8_t WS2812FX::gamma8_cal(uint8_t b, float gamma) {
|
2022-07-06 13:13:54 +02:00
|
|
|
return (int)(powf((float)b / 255.0f, gamma) * 255.0f + 0.5f);
|
2020-11-04 11:04:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WS2812FX::calcGammaTable(float gamma)
|
|
|
|
{
|
|
|
|
for (uint16_t i = 0; i < 256; i++) {
|
|
|
|
gammaT[i] = gamma8_cal(i, gamma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:23:09 +02:00
|
|
|
uint8_t WS2812FX::gamma8(uint8_t b)
|
|
|
|
{
|
|
|
|
return gammaT[b];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t WS2812FX::gamma32(uint32_t color)
|
|
|
|
{
|
|
|
|
if (!gammaCorrectCol) return color;
|
2021-10-26 20:35:45 +02:00
|
|
|
uint8_t w = W(color);
|
|
|
|
uint8_t r = R(color);
|
|
|
|
uint8_t g = G(color);
|
|
|
|
uint8_t b = B(color);
|
2019-05-22 00:23:09 +02:00
|
|
|
w = gammaT[w];
|
|
|
|
r = gammaT[r];
|
|
|
|
g = gammaT[g];
|
|
|
|
b = gammaT[b];
|
2021-10-26 20:35:45 +02:00
|
|
|
return RGBW32(r, g, b, w);
|
2019-05-22 00:23:09 +02:00
|
|
|
}
|
2019-12-31 11:11:05 +01:00
|
|
|
|
2021-11-24 11:02:25 +01:00
|
|
|
WS2812FX* WS2812FX::instance = nullptr;
|
2021-11-30 22:52:17 +01:00
|
|
|
|
|
|
|
//Bus static member definition, would belong in bus_manager.cpp
|
2021-11-27 16:49:42 +01:00
|
|
|
int16_t Bus::_cct = -1;
|
2021-11-28 01:21:17 +01:00
|
|
|
uint8_t Bus::_cctBlend = 0;
|
2022-03-26 23:20:14 +01:00
|
|
|
uint8_t Bus::_gAWM = 255;
|
2021-12-28 18:09:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
// WLEDSR: extensions
|
|
|
|
// Technical notes
|
|
|
|
// ===============
|
|
|
|
// If an effect name is followed by an @, slider and color control is effective.
|
2022-07-13 12:41:33 +02:00
|
|
|
// See setEffectParameters in index.js for implementation
|
2021-12-28 18:09:52 +01:00
|
|
|
// If not effective then:
|
|
|
|
// - For AC effects (id<128) 2 sliders and 3 colors and the palette will be shown
|
|
|
|
// - For SR effects (id>128) 5 sliders and 3 colors and the palette will be shown
|
|
|
|
// If effective (@)
|
|
|
|
// - a ; seperates slider controls (left) from color controls (middle) and palette control (right)
|
|
|
|
// - if left, middle or right is empty no controls are shown
|
|
|
|
// - a , seperates slider controls (max 5) or color controls (max 3). Palette has only one value
|
|
|
|
// - a ! means that the default is used.
|
|
|
|
// - For sliders: Effect speeds, Effect intensity, Custom 1, Custom 2, Custom 3
|
|
|
|
// - For colors: Fx color, Background color, Custom
|
|
|
|
// - For palette: prompt for color palette OR palette ID if numeric (will hide palette selection)
|
|
|
|
//
|
|
|
|
// Note: If palette is on and no colors are specified 1,2 and 3 is shown in each color circle.
|
|
|
|
// If a color is specified, the 1,2 or 3 is replaced by that specification.
|
|
|
|
// Note: Effects can override default pattern behaviour
|
|
|
|
// - FadeToBlack can override the background setting
|
|
|
|
// - Defining SEGCOL(<i>) can override a specific palette using these values (e.g. Color Gradient)
|
2022-05-24 16:43:21 +02:00
|
|
|
const char JSON_mode_names[] PROGMEM = R"=====(["Mode names have moved"])=====";
|
2022-05-21 18:37:09 +02:00
|
|
|
/*
|
|
|
|
R"=====([
|
2021-12-28 18:09:52 +01:00
|
|
|
"Solid",
|
|
|
|
"Blink@!,;!,!,;!",
|
|
|
|
"Breathe@!,;!,!;!",
|
|
|
|
"Wipe@!,!;!,!,;!",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Wipe Random@!,;1,2,3;!",
|
|
|
|
"Random Colors@!,Fade time;1,2,3;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Sweep@!,!;!,!,;!",
|
2022-01-12 21:35:10 +01:00
|
|
|
"Dynamic@!,!;1,2,3;!",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Colorloop@!,Saturation;1,2,3;!",
|
2022-01-12 21:35:10 +01:00
|
|
|
"Rainbow@!,Size;1,2,3;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Scan@!,# of dots;!,!,;!",
|
|
|
|
"Scan Dual@!,# of dots;!,!,;!",
|
2022-01-12 21:35:10 +01:00
|
|
|
"Fade@!,;!,!,;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Theater@!,Gap size;!,!,;!",
|
2022-01-23 11:13:50 +01:00
|
|
|
"Theater Rainbow@!,Gap size;1,2,3;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Running@!,Wave width;!,!,;!",
|
|
|
|
"Saw@!,Width;!,!,;!",
|
2022-01-12 21:35:10 +01:00
|
|
|
"Twinkle@!,;!,!,;!",
|
|
|
|
"Dissolve@Repeat speed,Dissolve speed;!,!,;!",
|
|
|
|
"Dissolve Rnd@Repeat speed,Dissolve speed;,!,;!",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Sparkle@!,;!,!,;!",
|
|
|
|
"Sparkle Dark@!,!;Bg,Fx,;!",
|
|
|
|
"Sparkle+@!,!;Bg,Fx,;!",
|
2022-01-12 21:35:10 +01:00
|
|
|
"Strobe@!,;!,!,;!",
|
|
|
|
"Strobe Rainbow@!,;,!,;!",
|
|
|
|
"Strobe Mega@!,!;!,!,;!",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Blink Rainbow@Frequency,Blink duration;!,!,;!",
|
|
|
|
"Android@!,Width;!,!,;!",
|
|
|
|
"Chase@!,Width;!,!,!;!",
|
|
|
|
"Chase Random@!,Width;!,,!;!",
|
|
|
|
"Chase Rainbow@!,Width;!,!,;0",
|
|
|
|
"Chase Flash@!,;Bg,Fx,!;!",
|
|
|
|
"Chase Flash Rnd@!,;,Fx,;!",
|
2022-01-12 21:35:10 +01:00
|
|
|
"Rainbow Runner@!,Size;Bg,,;!",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Colorful@!,Saturation;1,2,3;!",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Traffic Light@!,;,!,;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Sweep Random",
|
|
|
|
"Chase 2@!,Width;!,!,;!",
|
2022-01-10 19:00:44 +01:00
|
|
|
"Aurora@!=24,!;1,2,3;!=50",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Stream",
|
|
|
|
"Scanner",
|
|
|
|
"Lighthouse",
|
2022-01-12 15:45:32 +01:00
|
|
|
"Fireworks@Sharpness=96,Frequency=192;!,2,;!=11",
|
|
|
|
"Rain@Fade rate=128,Frequency=128;!,2,;!",
|
|
|
|
"Tetrix@!=224,Width=0;!,!,;!=11",
|
|
|
|
"Fire Flicker@!,!;!,,;!",
|
|
|
|
"Gradient@!,Spread=16;!,!,;!",
|
|
|
|
"Loading@!,Fade=16;!,!,;!",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Police@!,Width;,Bg,;0",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Fairy",
|
|
|
|
"Two Dots@!,Dot size;1,2,Bg;!",
|
|
|
|
"Fairy Twinkle",
|
|
|
|
"Running Dual",
|
2022-05-11 09:53:40 +02:00
|
|
|
"Halloween",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Chase 3@!,Size;1,2,3;0",
|
|
|
|
"Tri Wipe@!,Width;1,2,3;0",
|
|
|
|
"Tri Fade",
|
|
|
|
"Lightning",
|
|
|
|
"ICU",
|
|
|
|
"Multi Comet",
|
|
|
|
"Scanner Dual",
|
|
|
|
"Stream 2",
|
|
|
|
"Oscillate",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Pride 2015@!,;;",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Juggle@!=16,Trail=240;!,!,;!",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Palette@!,;1,2,3;!",
|
|
|
|
"Fire 2012@Spark rate=120,Decay=64;1,2,3;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Colorwaves",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Bpm@!=64,;1,2,3;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Fill Noise",
|
|
|
|
"Noise 1",
|
|
|
|
"Noise 2",
|
|
|
|
"Noise 3",
|
|
|
|
"Noise 4",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Colortwinkles@Fade speed,Spawn speed;1,2,3;!",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Lake@!,;1,2,3;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Meteor@!,Trail length;!,!,;!",
|
|
|
|
"Meteor Smooth@!,Trail length;!,!,;!",
|
|
|
|
"Railway",
|
|
|
|
"Ripple",
|
|
|
|
"Twinklefox",
|
|
|
|
"Twinklecat",
|
2022-05-11 09:53:40 +02:00
|
|
|
"Halloween Eyes@Duration,Eye fade time;Fx,Bg,;!",
|
2021-12-28 18:53:30 +01:00
|
|
|
"Solid Pattern@Fg size,Bg size;Fg,Bg,;!=0",
|
|
|
|
"Solid Pattern Tri@,Size;1,2,3;!=0",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Spots@Spread,Width;!,!,;!",
|
|
|
|
"Spots Fade@Spread,Width;!,!,;!",
|
|
|
|
"Glitter",
|
|
|
|
"Candle@Flicker rate=96,Flicker intensity=224;!,!,;0",
|
|
|
|
"Fireworks Starburst",
|
|
|
|
"Fireworks 1D@Gravity,Firing side;!,!,;!",
|
|
|
|
"Bouncing Balls@Gravity,# of balls;!,!,;!",
|
|
|
|
"Sinelon",
|
|
|
|
"Sinelon Dual",
|
|
|
|
"Sinelon Rainbow",
|
2022-01-23 11:13:50 +01:00
|
|
|
"Popcorn",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Drip@Gravity,# of drips;!,!;!",
|
|
|
|
"Plasma@Phase,;1,2,3;!",
|
|
|
|
"Percent@,% of fill;!,!,;!",
|
|
|
|
"Ripple Rainbow",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Heartbeat@!,!;!,!,;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Pacifica",
|
|
|
|
"Candle Multi@Flicker rate=96,Flicker intensity=224;!,!,;0",
|
|
|
|
"Solid Glitter@,!;!,,;0",
|
|
|
|
"Sunrise@Time [min]=60,;;0",
|
|
|
|
"Phased",
|
|
|
|
"Twinkleup@!,Intensity;!,!,;!",
|
|
|
|
"Noise Pal",
|
|
|
|
"Sine",
|
|
|
|
"Phased Noise",
|
|
|
|
"Flow",
|
|
|
|
"Chunchun@!,Gap size;!,!,;!",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Dancing Shadows@!,# of shadows;!,,;!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"Washing Machine",
|
2022-01-12 21:48:34 +01:00
|
|
|
"Candy Cane@!,Width;;",
|
2022-01-10 18:42:09 +01:00
|
|
|
"Blends@Shift speed,Blend speed;1,2,3,!",
|
2021-12-28 18:09:52 +01:00
|
|
|
"TV Simulator",
|
2022-05-08 10:50:48 +02:00
|
|
|
"Dynamic Smooth",
|
2022-05-10 21:41:20 +02:00
|
|
|
"2D Black Hole@Fade rate,Outer Y freq.,Outer X freq.,Inner X freq.,Inner Y freq.;;",
|
|
|
|
"2D DNA@Scroll speed,Blur;;!",
|
|
|
|
"2D DNA Spiral@Scroll speed,Blur;;!",
|
|
|
|
"2D Drift@Rotation speed,Blur amount;;!",
|
|
|
|
"2D Firenoise@X scale,Y scale;;",
|
|
|
|
"2D Frizzles@X frequency,Y frequency;;!",
|
|
|
|
"2D Hipnotic@X scale,Y scale;;!",
|
2022-05-19 17:01:52 +02:00
|
|
|
"2D Lissajous@X frequency,Fadetime;!,!,!;!",
|
2022-05-10 21:41:20 +02:00
|
|
|
"2D Matrix@Falling speed,Spawning rate,Trail,Custom color;Spawn,Trail;",
|
2022-05-19 11:29:04 +02:00
|
|
|
"2D Akemi@Color speed,Dance;Head palette,Arms & Legs,Eyes & Mouth;Face palette",
|
|
|
|
"2D Colored Bursts@Speed,Number of lines;;!",
|
2022-05-20 14:48:40 +02:00
|
|
|
"2D Game Of Life@!,;!,!;!",
|
2022-05-19 11:29:04 +02:00
|
|
|
"2D Julia@,Max iterations per pixel,X center,Y center,Area size;;!",
|
2022-05-19 17:01:52 +02:00
|
|
|
"2D Metaballs@Speed;!,!,!;!",
|
|
|
|
"2D Noise@Speed,Scale;!,!,!;!",
|
|
|
|
"2D Plasma Ball@Speed;!,!,!;!",
|
|
|
|
"2D Polar Lights@Speed,X scale,Palette;!,!,!;!",
|
2022-05-19 11:29:04 +02:00
|
|
|
"2D Pulser@Speed,Blur;;!",
|
|
|
|
"2D Sindots@Speed,Dot distance;;!",
|
|
|
|
"2D Squared Swirl@,,,,Blur;,,;!",
|
|
|
|
"2D Sun Radiation@Variance,Brightness;;",
|
|
|
|
"2D Tartan@X scale,Y scale;;!",
|
|
|
|
"2D Waverly@Fade rate,Sensitivity;;!",
|
2022-05-19 18:57:23 +02:00
|
|
|
"2D Spaceships@Fade rate,Blur;!,!,!;!",
|
2022-05-20 19:35:22 +02:00
|
|
|
"2D Crazy Bees@Fade rate,Blur;;",
|
2022-05-20 19:44:06 +02:00
|
|
|
"2D Ghost Rider@Fade rate,Blur;!,!,!;!",
|
2022-05-20 19:35:22 +02:00
|
|
|
"2D Blobs@!,# blobs;!,!,!;!"
|
2021-12-28 18:09:52 +01:00
|
|
|
])=====";
|
2022-05-21 18:37:09 +02:00
|
|
|
*/
|
2021-12-28 18:09:52 +01:00
|
|
|
|
|
|
|
const char JSON_palette_names[] PROGMEM = R"=====([
|
|
|
|
"Default","* Random Cycle","* Color 1","* Colors 1&2","* Color Gradient","* Colors Only","Party","Cloud","Lava","Ocean",
|
|
|
|
"Forest","Rainbow","Rainbow Bands","Sunset","Rivendell","Breeze","Red & Blue","Yellowout","Analogous","Splash",
|
|
|
|
"Pastel","Sunset 2","Beech","Vintage","Departure","Landscape","Beach","Sherbet","Hult","Hult 64",
|
|
|
|
"Drywet","Jul","Grintage","Rewhi","Tertiary","Fire","Icefire","Cyane","Light Pink","Autumn",
|
|
|
|
"Magenta","Magred","Yelmag","Yelblu","Orange & Teal","Tiamat","April Night","Orangery","C9","Sakura",
|
|
|
|
"Aurora","Atlantica","C9 2","C9 New","Temperature","Aurora 2","Retro Clown","Candy","Toxy Reaf","Fairy Reaf",
|
|
|
|
"Semi Blue","Pink Candy","Red Reaf","Aqua Flash","Yelblu Hot","Lite Light","Red Flash","Blink Red","Red Shift","Red Tide",
|
|
|
|
"Candy2"
|
|
|
|
])=====";
|