HTTP API: Set segments directly in set.cpp

This commit is contained in:
cschwinne 2022-02-21 20:48:11 +01:00
parent 1f5971f15a
commit 5f871bc01f

View File

@ -631,7 +631,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
selseg.setOption(SEG_OPTION_SELECTED, t); selseg.setOption(SEG_OPTION_SELECTED, t);
} }
//temporary values, do not write direcly to global values if only setting a single segment // temporary values, write directly to segments, globals are updated by setValuesFromMainSeg()
uint32_t col0 = selseg.colors[0]; uint32_t col0 = selseg.colors[0];
uint32_t col1 = selseg.colors[1]; uint32_t col1 = selseg.colors[1];
byte colIn[4] = {R(col0), G(col0), B(col0), W(col0)}; byte colIn[4] = {R(col0), G(col0), B(col0), W(col0)};
@ -708,15 +708,15 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
bool col0Changed = false, col1Changed = false; bool col0Changed = false, col1Changed = false;
//set colors //set colors
updateVal(&req, "&R=", &colIn[0]); col0Changed |= updateVal(&req, "&R=", &colIn[0]);
updateVal(&req, "&G=", &colIn[1]); col0Changed |= updateVal(&req, "&G=", &colIn[1]);
updateVal(&req, "&B=", &colIn[2]); col0Changed |= updateVal(&req, "&B=", &colIn[2]);
updateVal(&req, "&W=", &colIn[3]); col0Changed |= updateVal(&req, "&W=", &colIn[3]);
updateVal(&req, "R2=", &colInSec[0]); col1Changed |= updateVal(&req, "R2=", &colInSec[0]);
updateVal(&req, "G2=", &colInSec[1]); col1Changed |= updateVal(&req, "G2=", &colInSec[1]);
updateVal(&req, "B2=", &colInSec[2]); col1Changed |= updateVal(&req, "B2=", &colInSec[2]);
updateVal(&req, "W2=", &colInSec[3]); col1Changed |= updateVal(&req, "W2=", &colInSec[3]);
#ifdef WLED_ENABLE_LOXONE #ifdef WLED_ENABLE_LOXONE
//lox parser //lox parser
@ -726,6 +726,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
if (parseLx(lxValue, colIn)) { if (parseLx(lxValue, colIn)) {
bri = 255; bri = 255;
nightlightActive = false; //always disable nightlight when toggling nightlightActive = false; //always disable nightlight when toggling
col0Changed = true;
} }
} }
pos = req.indexOf(F("LY=")); // Lox secondary color pos = req.indexOf(F("LY=")); // Lox secondary color
@ -734,6 +735,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
if(parseLx(lxValue, colInSec)) { if(parseLx(lxValue, colInSec)) {
bri = 255; bri = 255;
nightlightActive = false; //always disable nightlight when toggling nightlightActive = false; //always disable nightlight when toggling
col1Changed = true;
} }
} }
#endif #endif
@ -749,6 +751,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
} }
byte sec = req.indexOf(F("H2")); byte sec = req.indexOf(F("H2"));
colorHStoRGB(temphue, tempsat, (sec>0) ? colInSec : colIn); colorHStoRGB(temphue, tempsat, (sec>0) ? colInSec : colIn);
col0Changed |= (!sec); col1Changed |= sec;
} }
//set white spectrum (kelvin) //set white spectrum (kelvin)
@ -756,6 +759,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
if (pos > 0) { if (pos > 0) {
byte sec = req.indexOf(F("K2")); byte sec = req.indexOf(F("K2"));
colorKtoRGB(getNumVal(&req, pos), (sec>0) ? colInSec : colIn); colorKtoRGB(getNumVal(&req, pos), (sec>0) ? colInSec : colIn);
col0Changed |= (!sec); col1Changed |= sec;
} }
//set color from HEX or 32bit DEC //set color from HEX or 32bit DEC
@ -763,10 +767,12 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
pos = req.indexOf(F("CL=")); pos = req.indexOf(F("CL="));
if (pos > 0) { if (pos > 0) {
colorFromDecOrHexString(colIn, (char*)req.substring(pos + 3).c_str()); colorFromDecOrHexString(colIn, (char*)req.substring(pos + 3).c_str());
col0Changed = true;
} }
pos = req.indexOf(F("C2=")); pos = req.indexOf(F("C2="));
if (pos > 0) { if (pos > 0) {
colorFromDecOrHexString(colInSec, (char*)req.substring(pos + 3).c_str()); colorFromDecOrHexString(colInSec, (char*)req.substring(pos + 3).c_str());
col1Changed = true;
} }
pos = req.indexOf(F("C3=")); pos = req.indexOf(F("C3="));
if (pos > 0) { if (pos > 0) {
@ -782,6 +788,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
if (pos > 0) { if (pos > 0) {
byte sec = getNumVal(&req, pos); byte sec = getNumVal(&req, pos);
setRandomColor(sec? colInSec : colIn); setRandomColor(sec? colInSec : colIn);
col0Changed |= (!sec); col1Changed |= sec;
} }
//swap 2nd & 1st //swap 2nd & 1st
@ -793,39 +800,43 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
colIn[i] = colInSec[i]; colIn[i] = colInSec[i];
colInSec[i] = temp; colInSec[i] = temp;
} }
col0Changed = col1Changed = true;
} }
//apply colors to selected segment, and main color array if applicable // apply colors to selected segment, and all selected segments if applicable
for (byte i=0; i<4; i++) if (colIn[i]!=col[i]) col0Changed = colorChanged = true; if (col0Changed) {
if (singleSegment) { colorChanged = true;
if (col0Changed) selseg.setColor(0, RGBW32(colIn[0], colIn[1], colIn[2], colIn[3]), selectedSeg); uint32_t colIn0 = RGBW32(colIn[0], colIn[1], colIn[2], colIn[3]);
} else { selseg.setColor(0, colIn0, selectedSeg);
for (byte i=0; i<4; i++) col[i] = colIn[i]; if (!singleSegment) strip.setColor(0, colIn0);
} }
for (byte i=0; i<4; i++) if (colInSec[i]!=colSec[i]) col1Changed = colorChanged = true; if (col1Changed) {
if (singleSegment) { colorChanged = true;
if (col1Changed) selseg.setColor(1, RGBW32(colInSec[0], colInSec[1], colInSec[2], colInSec[3]), selectedSeg); uint32_t colIn1 = RGBW32(colInSec[0], colInSec[1], colInSec[2], colInSec[3]);
} else { selseg.setColor(1, colIn1, selectedSeg);
for (byte i=0; i<4; i++) colSec[i] = colInSec[i]; if (!singleSegment) strip.setColor(1, colIn1);
} }
//set effect parameters bool fxModeChanged = false, speedChanged = false, intensityChanged = false, paletteChanged = false;
if (updateVal(&req, "FX=", &effectIn, 0, strip.getModeCount()-1) && request != nullptr) unloadPlaylist(); //unload playlist if changing FX using web request // set effect parameters
updateVal(&req, "SX=", &speedIn); if (updateVal(&req, "FX=", &effectIn, 0, strip.getModeCount()-1)) {
updateVal(&req, "IX=", &intensityIn); if (request != nullptr) unloadPlaylist(); // unload playlist if changing FX using web request
updateVal(&req, "FP=", &paletteIn, 0, strip.getPaletteCount()-1); fxModeChanged = true;
if (effectIn != effectCurrent || speedIn != effectSpeed || intensityIn != effectIntensity || paletteIn != effectPalette) effectChanged = true; }
if (singleSegment) { speedChanged = updateVal(&req, "SX=", &speedIn);
strip.setMode(selectedSeg, effectIn); intensityChanged = updateVal(&req, "IX=", &intensityIn);
selseg.speed = speedIn; paletteChanged = updateVal(&req, "FP=", &paletteIn, 0, strip.getPaletteCount()-1);
selseg.intensity = intensityIn;
selseg.palette = paletteIn; effectChanged = (fxModeChanged || speedChanged || intensityChanged || paletteChanged);
} else {
effectCurrent = effectIn; for (uint8_t i = 0; i < strip.getMaxSegments(); i++) {
effectSpeed = speedIn; WS2812FX::Segment& seg = strip.getSegment(i);
effectIntensity = intensityIn; if (i != selectedSeg && (singleSegment || !seg.isActive() || !seg.isSelected())) continue;
effectPalette = paletteIn; if (fxModeChanged) strip.setMode(i, effectIn);
if (speedChanged) seg.speed = speedIn;
if (intensityChanged) seg.intensity = intensityIn;
if (paletteChanged) seg.palette = paletteIn;
} }
//set advanced overlay //set advanced overlay
@ -954,10 +965,10 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
if (pos > 0) { if (pos > 0) {
userVar1 = getNumVal(&req, pos); userVar1 = getNumVal(&req, pos);
} }
//you can add more if you need // you can add more if you need
if (!singleSegment) applyValuesToSelectedSegs(); // global col[], effectCurrent, ... are updated in stateChanged()
if (!apply) return true; //when called by JSON API, do not call colorUpdated() here if (!apply) return true; // when called by JSON API, do not call colorUpdated() here
//internal call, does not send XML response //internal call, does not send XML response
pos = req.indexOf(F("IN")); pos = req.indexOf(F("IN"));