Skip to content

Commit

Permalink
Presets: reset to None if changes, Controls: default if new effect/proje
Browse files Browse the repository at this point in the history
pio.ini
- newest ESPLive Script

LedModEffects / LedModFixtureGen / SysModNetwork
- switch to pre/postDetails2 (WIP)

SysModModel
- add pre/postDetails2 (WIP)
- triggerEvent: reset preset back to None if var changes
- add findModule
- add resetPresetThreshold

SysModule
- presets: add option None
- preset.onChange: return if preset is None,
- preset/ assignPreset/clearPreset.onChange: decrease resetPresetThreshold if setValue

SysModUI
- processJson: increase resetPresetThreshold if setValue
  • Loading branch information
ewowi committed Dec 13, 2024
1 parent 27af99c commit 1d0ce5c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 19 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ build_flags =
-D STARBASE_USERMOD_LIVE
-D EXTPRINTF=ppf ;redirect Live Script prints to StarBase print
lib_deps =
https://github.com/ewowi/ESPLiveScript.git#f229acc ; v3.1 ;ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates
https://github.com/ewowi/ESPLiveScript.git#c6463f6 ; v3.1 ;ewowi repo adds some proposed PR's and makes sure we don't have unexpected updates

[STARLIGHT_CLOCKLESS_LED_DRIVER]
build_flags =
Expand Down
13 changes: 6 additions & 7 deletions src/App/LedModEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,16 @@ inline uint16_t getRGBWsize(uint16_t nleds){
else
leds->projection = projections[proValue];

ppf("initProjection leds[%d] projection:%s a:%d\n", rowNr, leds->projection->name(), leds->projectionData.bytesAllocated);
ppf("initProjection leds[%d] projection:%s a:%d\n", rowNr, leds->projection?leds->projection->name():"None", leds->projectionData.bytesAllocated);

leds->projectionData.clear(); //delete effectData memory so it can be rebuild

leds->projectionData.read<uint8_t>(); leds->projectionData.begin(); //allocate minimum amount for projectionData (chunk of 32 bytes) to avoid control defaults to be removed

variable.preDetails(); //set all positive var N orders to negative
variable.preDetails2(); //set all positive var N orders to negative
mdl->setValueRowNr = rowNr;
if (leds->projection) leds->projection->setup(*leds, variable); //not if None projection
variable.postDetails(rowNr);
variable.postDetails2(rowNr);

mdl->setValueRowNr = UINT8_MAX;

leds->projectionData.alertIfChanged = true; //find out when it is changing, eg when projections change, in that case controls are lost...solution needed for that...
Expand Down Expand Up @@ -535,14 +535,13 @@ inline uint16_t getRGBWsize(uint16_t nleds){
ppf("initEffect leds[%d] effect:%s a:%d (%d,%d,%d)\n", rowNr, leds.effect->name(), leds.effectData.bytesAllocated, leds.size.x, leds.size.y, leds.size.z);

leds.effectData.clear(); //delete effectData memory so it can be rebuild

leds.effect->loop(leds); leds.effectData.begin(); //do a loop to set effectData right to avoid control defaults to be removed

Variable variable = Variable("layers", "effect");
variable.preDetails();
variable.preDetails2();
mdl->setValueRowNr = rowNr;
leds.effect->setup(leds, variable); //if changed then run setup once (like call==0 in WLED) and set all defaults in effectData
variable.postDetails(rowNr);
variable.postDetails2(rowNr);
mdl->setValueRowNr = UINT8_MAX;

leds.effectData.alertIfChanged = true; //find out when it is changing, eg when projections change, in that case controls are lost...solution needed for that...
Expand Down
2 changes: 1 addition & 1 deletion src/App/LedModFixtureGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ class LedModFixtureGen:public SysModule {
}
}

fixtureVariable.postDetails(UINT8_MAX);
fixtureVariable.postDetails2(UINT8_MAX);
mdl->setValueRowNr = UINT8_MAX;
}

Expand Down
32 changes: 31 additions & 1 deletion src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
children().remove(childVarIt);
}
web->getResponseObject()["details"]["rowNr"] = rowNr;

}
else
print->printJson("dev array but not rowNr", var);
Expand All @@ -139,6 +138,13 @@
ppf("\n");

//post update details
}

void Variable::preDetails2() {
var["n"].to<JsonArray>(); //clean array, old array removed?
}
void Variable::postDetails2(uint8_t rowNr) {
web->getResponseObject()["details"]["rowNr"] = rowNr;
web->getResponseObject()["details"]["var"] = var;
}

Expand Down Expand Up @@ -242,6 +248,16 @@
// ppf("dev pointer of type %s is 0\n", var["type"].as<String>().c_str());
print->printJson("dev pointer is 0", var);
} //pointer

//reset presets if not using presets controls and if updated by UI, except if updated by ui via presets
if (var["id"] != "preset" && var["id"] != "assignPreset" && var["id"] != "clearPreset" && mdl->resetPresetThreshold > 1) {
JsonObject moduleVar = mdl->findModule(pid(), id());
JsonObject presetVar = mdl->findVar(moduleVar["id"], "preset");
if (!presetVar.isNull()) {
ppf("reset Preset %s.%s\n", pid(), id());
Variable(presetVar).setValue(0, rowNr);
}
}
}

bool result = false;
Expand Down Expand Up @@ -797,6 +813,20 @@ JsonObject SysModModel::findVar(const char * pid, const char * id, JsonObject pa
return JsonObject();
}

JsonObject SysModModel::findModule(const char * pid, const char * id) {
// if (model->isNull()) return JsonObject();

for (JsonObject moduleVar : model->as<JsonArray>()) {
bool pididFound = false;
walkThroughModel([&pididFound, pid, id](JsonObject parentVar, JsonObject var) {
pididFound = var["pid"] == pid && var["id"] == id;
return pididFound?var:JsonObject(); //stop if found
}, moduleVar);
if (pididFound) return moduleVar;
}

}

void SysModModel::findVars(const char * property, bool value, FindFun fun, JsonObject parentVar) {
// print ->print("findVar %s %s\n", id, parent.isNull()?"root":"n");

Expand Down
6 changes: 5 additions & 1 deletion src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ class Variable {
//extra methods

void preDetails();

void postDetails(uint8_t rowNr);
void preDetails2();
void postDetails2(uint8_t rowNr);

//checks if var has fun of type eventType implemented by calling it and checking result (for onUI on RO var, also onSetValue is called)
//onChange: sends dash var change to udp (if init), sets pointer if pointer var and run onChange
Expand Down Expand Up @@ -385,6 +386,8 @@ class SysModModel: public SysModule {
std::vector<VarEvent> varEvents;
std::vector<VarEventPS> varEventsPS;

uint8_t resetPresetThreshold = 1; //can be lowered by preset.onchange and highered by processJson, if > 1 (not lowered but highered) then reset is allowed

SysModModel();
void setup() override;
void loop20ms() override;
Expand Down Expand Up @@ -422,6 +425,7 @@ class SysModModel: public SysModule {
//returns the var defined by id (parent to recursively call findVar)
JsonObject walkThroughModel(std::function<JsonObject(JsonObject, JsonObject)> fun, JsonObject parentVar = JsonObject());
JsonObject findVar(const char * pid, const char * id, JsonObject parentVar = JsonObject());
JsonObject findModule(const char * pid, const char * id);
void findVars(const char * id, bool value, FindFun fun, JsonObject parentVar = JsonObject());

uint8_t linearToLogarithm(uint8_t value, uint8_t minp = 0, uint8_t maxp = UINT8_MAX) {
Expand Down
4 changes: 2 additions & 2 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void SysModNetwork::setup() {
return true;
}
case onChange:
variable.preDetails();
variable.preDetails2();
mdl->setValueRowNr = rowNr;

if (variable.value() == 0) {//manual
Expand Down Expand Up @@ -160,7 +160,7 @@ void SysModNetwork::setup() {
}});
}

variable.postDetails(rowNr);
variable.postDetails2(rowNr);
mdl->setValueRowNr = UINT8_MAX;

ethActive = false;
Expand Down
2 changes: 2 additions & 0 deletions src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ void SysModUI::processJson(JsonVariant json) {

if (!var.isNull())
{
mdl->resetPresetThreshold++;
//a button never sets the value
if (var["type"] == "button") { //button always
Variable(var).triggerEvent(onChange, rowNr);
Expand All @@ -177,6 +178,7 @@ void SysModUI::processJson(JsonVariant json) {
// json.remove(key); //key / var["id"] processed we don't need the key in the response
// print->printJson("setValueJV", web->getResponseObject());
}
mdl->resetPresetThreshold--;
}
else
ppf("dev Object %s[%d] not found\n", pidid, rowNr);
Expand Down
28 changes: 22 additions & 6 deletions src/SysModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void SysModule::addPresets(JsonObject parentVar) {

JsonArray modulePresets = mdl->presets->as<JsonObject>()[name];

options.add("None");
for (int i=0; i<modulePresets.size(); i++) {
StarString buf;
if (modulePresets[i].isNull() || modulePresets[i]["name"].isNull()) {
Expand All @@ -47,12 +48,16 @@ void SysModule::addPresets(JsonObject parentVar) {
uint8_t presetValue = variable.var["value"];
ppf("publish preset.onchange %s.%s [%d] %d %s\n", variable.pid(), variable.id(), rowNr, presetValue, variable.valueString().c_str());

if (presetValue == 0) return; else presetValue--;

JsonObject allPresets = mdl->presets->as<JsonObject>();

if (!allPresets.isNull()) {

JsonArray modulePresets = allPresets[name];
if (!modulePresets.isNull()) {
if (!modulePresets.isNull() && presetValue < modulePresets.size()) {

mdl->resetPresetThreshold--;

for (JsonPair pidPair: modulePresets[presetValue].as<JsonObject>()) {
for (JsonPair idPair: pidPair.value().as<JsonObject>()) {
Expand All @@ -70,6 +75,9 @@ void SysModule::addPresets(JsonObject parentVar) {
}
}
}

mdl->resetPresetThreshold++;

}
}

Expand All @@ -82,7 +90,7 @@ void SysModule::addPresets(JsonObject parentVar) {
});

currentVar.subscribe(onChange, [this, &parentVariable](Variable variable, uint8_t rowNr, uint8_t eventType) {
ppf("assignPreset.onUI \n");
ppf("assignPreset.onChange\n");
//save this to the first free slot
//give that a name
//select that one
Expand All @@ -91,6 +99,8 @@ void SysModule::addPresets(JsonObject parentVar) {

uint8_t presetValue = presetVariable.var["value"];

presetValue--; // will be UINT8_MAX if preset None

JsonObject allPresets = mdl->presets->as<JsonObject>();
if (allPresets.isNull()) allPresets = mdl->presets->to<JsonObject>(); //create

Expand All @@ -103,7 +113,7 @@ void SysModule::addPresets(JsonObject parentVar) {
// print->printJson("pv", parentVariable.var); ppf("\n");

uint8_t presetIndex = 0;
if (modulePresets[presetValue].isNull()) { //if slot is 0 use it
if (presetValue < modulePresets.size() && modulePresets[presetValue].isNull()) { //if slot is null use it
presetIndex = presetValue;
} else {
//find the first empty slot, starting from current position, if none, add
Expand Down Expand Up @@ -155,7 +165,9 @@ void SysModule::addPresets(JsonObject parentVar) {

presetVariable.publish(onUI); //reload ui for new list of values

if (presetIndex != presetValue) presetVariable.setValue(presetIndex); //set the new value, if changed
mdl->resetPresetThreshold--;
if (presetIndex != presetValue) presetVariable.setValue(presetIndex+1); //set the new value, if changed, add 1 for None
mdl->resetPresetThreshold++;

});

Expand All @@ -166,14 +178,16 @@ void SysModule::addPresets(JsonObject parentVar) {
});

currentVar.subscribe(onChange, [this](Variable variable, uint8_t rowNr, uint8_t eventType) {
ppf("delete.onUI \n");
ppf("clearPreset.onChange\n");
//free this slot
//remove the name

Variable presetVariable = Variable(name, "preset");

uint8_t presetValue = presetVariable.var["value"];

if (presetValue == 0) return; else presetValue--; //makes no sense to clear None

JsonObject allPresets = mdl->presets->as<JsonObject>();

if (!allPresets.isNull()) {
Expand All @@ -197,7 +211,9 @@ void SysModule::addPresets(JsonObject parentVar) {

presetVariable.publish(onUI); //reload ui for new list of values

if (presetIndex != presetValue) presetVariable.setValue(presetIndex); //set the new value, if changed
mdl->resetPresetThreshold--;
if (presetIndex != presetValue) presetVariable.setValue(presetIndex+1); //set the new value, if changed, add 1 for none
mdl->resetPresetThreshold++;
}
}

Expand Down

0 comments on commit 1d0ce5c

Please sign in to comment.