Add setMode() and setPalette() methods

- automatically start transition
Implement load FX defaults for HTTP API (FXD)
This commit is contained in:
Blaž Kristan 2022-09-29 12:49:12 +02:00
parent 7cd9e8860d
commit f385af595a
6 changed files with 57 additions and 51 deletions

View File

@ -114,8 +114,8 @@ class Animated_Staircase : public Usermod {
// We may need to copy mode and colors from segment 0 to make sure // We may need to copy mode and colors from segment 0 to make sure
// changes are propagated even when the config is changed during a wipe // changes are propagated even when the config is changed during a wipe
// segments->mode = mainsegment.mode; // seg.setMode(mainsegment.mode);
// segments->colors[0] = mainsegment.colors[0]; // seg.setColor(0, mainsegment.colors[0]);
} else { } else {
seg.setOption(SEG_OPTION_ON, false); seg.setOption(SEG_OPTION_ON, false);
} }

View File

@ -531,11 +531,11 @@ public:
for (byte i=0; i<strip.getSegmentsNum(); i++) { for (byte i=0; i<strip.getSegmentsNum(); i++) {
Segment& seg = strip.getSegment(i); Segment& seg = strip.getSegment(i);
if (!seg.isActive()) continue; if (!seg.isActive()) continue;
strip.setMode(i, effectCurrent); seg.setMode(effectCurrent);
} }
} else { } else {
//Segment& seg = strip.getSegment(strip.getMainSegmentId()); Segment& seg = strip.getSegment(strip.getMainSegmentId());
strip.setMode(strip.getMainSegmentId(), effectCurrent); seg.setMode(effectCurrent);
} }
lampUdated(); lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY #ifdef USERMOD_FOUR_LINE_DISPLAY
@ -661,11 +661,11 @@ public:
for (byte i=0; i<strip.getSegmentsNum(); i++) { for (byte i=0; i<strip.getSegmentsNum(); i++) {
Segment& seg = strip.getSegment(i); Segment& seg = strip.getSegment(i);
if (!seg.isActive()) continue; if (!seg.isActive()) continue;
seg.palette = effectPalette; seg.setPalette(effectPalette);
} }
} else { } else {
Segment& seg = strip.getSegment(strip.getMainSegmentId()); Segment& seg = strip.getSegment(strip.getMainSegmentId());
seg.palette = effectPalette; seg.setPalette(effectPalette);
} }
lampUdated(); lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY #ifdef USERMOD_FOUR_LINE_DISPLAY

View File

@ -502,6 +502,8 @@ typedef struct Segment {
void setCCT(uint16_t k); void setCCT(uint16_t k);
void setOpacity(uint8_t o); void setOpacity(uint8_t o);
void setOption(uint8_t n, bool val); void setOption(uint8_t n, bool val);
void setMode(uint8_t fx, bool loadDefaults = false);
void setPalette(uint8_t pal);
uint8_t differs(Segment& b) const; uint8_t differs(Segment& b) const;
void refreshLightCapabilities(void); void refreshLightCapabilities(void);

View File

@ -396,6 +396,48 @@ void Segment::setOption(uint8_t n, bool val) {
else options &= ~(0x01 << n); else options &= ~(0x01 << n);
} }
void Segment::setMode(uint8_t fx, bool loadDefaults) {
// if we have a valid mode & is not reserved
if (fx < strip.getModeCount() && strncmp_P("RSVD", strip.getModeData(fx), 4)) {
if (fx != mode) {
startTransition(strip.getTransition()); // set effect transitions
//markForReset(); // transition will handle this
mode = fx;
// load default values from effect string
if (loadDefaults) {
int16_t sOpt;
sOpt = extractModeDefaults(fx, "sx"); if (sOpt >= 0) speed = sOpt;
sOpt = extractModeDefaults(fx, "ix"); if (sOpt >= 0) intensity = sOpt;
sOpt = extractModeDefaults(fx, "c1"); if (sOpt >= 0) custom1 = sOpt;
sOpt = extractModeDefaults(fx, "c2"); if (sOpt >= 0) custom2 = sOpt;
sOpt = extractModeDefaults(fx, "c3"); if (sOpt >= 0) custom3 = sOpt;
sOpt = extractModeDefaults(fx, "mp12"); if (sOpt >= 0) map1D2D = constrain(sOpt, 0, 7);
sOpt = extractModeDefaults(fx, "ssim"); if (sOpt >= 0) soundSim = constrain(sOpt, 0, 7);
sOpt = extractModeDefaults(fx, "rev"); if (sOpt >= 0) reverse = (bool)sOpt;
sOpt = extractModeDefaults(fx, "mi"); if (sOpt >= 0) mirror = (bool)sOpt; // NOTE: setting this option is a risky business
sOpt = extractModeDefaults(fx, "rY"); if (sOpt >= 0) reverse_y = (bool)sOpt;
sOpt = extractModeDefaults(fx, "mY"); if (sOpt >= 0) mirror_y = (bool)sOpt; // NOTE: setting this option is a risky business
sOpt = extractModeDefaults(fx, "pal");
if (sOpt >= 0 && (size_t)sOpt < strip.getPaletteCount() + strip.customPalettes.size()) {
if (sOpt != palette) {
palette = sOpt;
}
}
}
}
}
}
void Segment::setPalette(uint8_t pal) {
if (pal < strip.getPaletteCount()) {
if (pal != palette) {
if (strip.paletteFade) startTransition(strip.getTransition());
palette = pal;
}
}
}
// 2D matrix // 2D matrix
uint16_t Segment::virtualWidth() const { uint16_t Segment::virtualWidth() const {
uint16_t groupLen = groupLength(); uint16_t groupLen = groupLength();

View File

@ -174,47 +174,16 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
byte fx = seg.mode; byte fx = seg.mode;
if (getVal(elem["fx"], &fx, 0, strip.getModeCount())) { //load effect ('r' random, '~' inc/dec, 0-255 exact value) if (getVal(elem["fx"], &fx, 0, strip.getModeCount())) { //load effect ('r' random, '~' inc/dec, 0-255 exact value)
if (!presetId && currentPlaylist>=0) unloadPlaylist(); if (!presetId && currentPlaylist>=0) unloadPlaylist();
if (fx != seg.mode) { if (fx != seg.mode) seg.setMode(fx, elem[F("fxdef")]);
seg.startTransition(strip.getTransition()); // set effect transitions
//seg.markForReset();
seg.mode = fx;
}
}
// load default values from effect string
if (elem[F("fxdef")])
{
int16_t sOpt;
sOpt = extractModeDefaults(fx, "sx"); if (sOpt >= 0) seg.speed = sOpt;
sOpt = extractModeDefaults(fx, "ix"); if (sOpt >= 0) seg.intensity = sOpt;
sOpt = extractModeDefaults(fx, "c1"); if (sOpt >= 0) seg.custom1 = sOpt;
sOpt = extractModeDefaults(fx, "c2"); if (sOpt >= 0) seg.custom2 = sOpt;
sOpt = extractModeDefaults(fx, "c3"); if (sOpt >= 0) seg.custom3 = sOpt;
sOpt = extractModeDefaults(fx, "mp12"); if (sOpt >= 0) seg.map1D2D = constrain(sOpt, 0, 7);
sOpt = extractModeDefaults(fx, "ssim"); if (sOpt >= 0) seg.soundSim = constrain(sOpt, 0, 7);
sOpt = extractModeDefaults(fx, "rev"); if (sOpt >= 0) seg.reverse = (bool)sOpt;
sOpt = extractModeDefaults(fx, "mi"); if (sOpt >= 0) seg.mirror = (bool)sOpt; // NOTE: setting this option is a risky business
sOpt = extractModeDefaults(fx, "rY"); if (sOpt >= 0) seg.reverse_y = (bool)sOpt;
sOpt = extractModeDefaults(fx, "mY"); if (sOpt >= 0) seg.mirror_y = (bool)sOpt; // NOTE: setting this option is a risky business
sOpt = extractModeDefaults(fx, "pal");
if (sOpt >= 0 && sOpt < strip.getPaletteCount() + strip.customPalettes.size()) {
if (sOpt != seg.palette) {
if (strip.paletteFade && !seg.transitional) seg.startTransition(strip.getTransition());
seg.palette = sOpt;
}
}
} }
//getVal also supports inc/decrementing and random //getVal also supports inc/decrementing and random
getVal(elem["sx"], &seg.speed); getVal(elem["sx"], &seg.speed);
getVal(elem["ix"], &seg.intensity); getVal(elem["ix"], &seg.intensity);
uint8_t pal = seg.palette; uint8_t pal = seg.palette;
if (getVal(elem["pal"], &pal, 1, strip.getPaletteCount())) { if (getVal(elem["pal"], &pal, 1, strip.getPaletteCount())) seg.setPalette(pal);
if (pal != seg.palette) {
if (strip.paletteFade && !seg.transitional) seg.startTransition(strip.getTransition());
seg.palette = pal;
}
}
getVal(elem["c1"], &seg.custom1); getVal(elem["c1"], &seg.custom1);
getVal(elem["c2"], &seg.custom2); getVal(elem["c2"], &seg.custom2);
uint8_t cust3 = seg.custom3; uint8_t cust3 = seg.custom3;

View File

@ -888,17 +888,10 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
for (uint8_t i = 0; i < strip.getSegmentsNum(); i++) { for (uint8_t i = 0; i < strip.getSegmentsNum(); i++) {
Segment& seg = strip.getSegment(i); Segment& seg = strip.getSegment(i);
if (i != selectedSeg && (singleSegment || !seg.isActive() || !seg.isSelected())) continue; // skip non main segments if not applying to all if (i != selectedSeg && (singleSegment || !seg.isActive() || !seg.isSelected())) continue; // skip non main segments if not applying to all
if (fxModeChanged) { if (fxModeChanged) seg.setMode(effectIn, req.indexOf(F("FXD="))>0); // apply defaults if FXD= is specified
seg.startTransition(strip.getTransition());
seg.mode = effectIn;
// TODO: we should load defaults here as well
}
if (speedChanged) seg.speed = speedIn; if (speedChanged) seg.speed = speedIn;
if (intensityChanged) seg.intensity = intensityIn; if (intensityChanged) seg.intensity = intensityIn;
if (paletteChanged) { if (paletteChanged) seg.setPalette(paletteIn);
if (strip.paletteBlend) seg.startTransition(strip.getTransition());
seg.palette = paletteIn;
}
} }
//set advanced overlay //set advanced overlay