Skip to content

Commit

Permalink
Small changes incl bugfix on control pointers
Browse files Browse the repository at this point in the history
General
- Variable.valueString(): add c_str

SysModModel
- callVarOnChange: use value and check on pointerArray
- linearToLogarithm: move from Variable to mdl
  • Loading branch information
ewoudwijma committed Sep 17, 2024
1 parent 2be0136 commit 956dbed
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void SysModFiles::setup() {
case onDelete:
if (rowNr != UINT8_MAX && rowNr < fileList.size()) {
const char * fileName = fileList[rowNr].name;
// ppf("fileTbl onDelete %s[%d] = %s %s\n", Variable(var).id(), rowNr, Variable(var).valueString(), fileName);
// ppf("fileTbl onDelete %s[%d] = %s %s\n", Variable(var).id(), rowNr, Variable(var).valueString().c_str(), fileName);
this->removeFiles(fileName, false);

#ifdef STARBASE_USERMOD_LIVE
Expand Down
10 changes: 6 additions & 4 deletions src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class SysModInstances:public SysModule {
//find dash variables and add them to the table
mdl->findVars("dash", true, [tableVar, this](JsonObject var) { //findFun

ppf("dash %s %s found\n", Variable(var).id(), Variable(var).valueString());
ppf("dash %s %s found\n", Variable(var).id(), Variable(var).valueString().c_str());

char columnVarID[32] = "ins";
strcat(columnVarID, var["id"]);
Expand Down Expand Up @@ -561,8 +561,9 @@ class SysModInstances:public SysModule {
for (JsonObject childVar: Variable(mdl->findVar("insTbl")).children())
ui->callVarFun(childVar, UINT8_MAX, onSetValue); //set the value (WIP)); //no rowNr so all rows updated

//tbd: pubsub mechanism
ui->callVarFun("ddpInst", UINT8_MAX, onUI); //rebuild options
ui->callVarFun("artInst", UINT8_MAX, onUI); //rebuild options
// ui->callVarFun("artInst", UINT8_MAX, onUI); //rebuild options
}
}

Expand Down Expand Up @@ -808,9 +809,10 @@ class SysModInstances:public SysModule {

if (!instanceFound) {
ppf("insTbl new instance %s\n", messageIP.toString().c_str());


//tbd: pubsub mechanism
ui->callVarFun("ddpInst", UINT8_MAX, onUI); //rebuild options
ui->callVarFun("artInst", UINT8_MAX, onUI); //rebuild options
// ui->callVarFun("artInst", UINT8_MAX, onUI); //rebuild options

// ui->processOnUI("insTbl");
//run though it sorted to find the right rowNr
Expand Down
27 changes: 18 additions & 9 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,35 @@ bool SysModModel::callVarOnChange(JsonObject var, unsigned8 rowNr, bool init) {

//if var is bound by pointer, set the pointer value before calling onChange
if (!var["p"].isNull()) {
JsonVariant value;
if (rowNr == UINT8_MAX) {
value = var["value"];
} else {
value = var["value"][rowNr];
}

//pointer is an array if set by setValueRowNr, used for controls as each control has a seperate variable
bool isPointerArray = var["p"].is<JsonArray>();
int pointer;
if (var["p"].is<JsonArray>())
if (isPointerArray)
pointer = var["p"][rowNr];
else
pointer = var["p"];

if (pointer != 0) {

if (var["value"].is<JsonArray>()) {
if (var["value"].is<JsonArray>() && !isPointerArray) { //vector if val array but not if control (each var in array stored in seperate variable)
if (rowNr != UINT8_MAX) {
if (var["type"] == "select" || var["type"] == "checkbox" || var["type"] == "range") {
std::vector<uint8_t> *valuePointer = (std::vector<uint8_t> *)pointer;
while (rowNr >= (*valuePointer).size()) (*valuePointer).push_back(UINT8_MAX); //create vector space if needed...
(*valuePointer)[rowNr] = var["value"][rowNr]; //value should be an uint16_t
ppf("%s[%d]:%s (%d - %d - %s)\n", variable.id(), rowNr, variable.valueString().c_str().c_str(), pointer, (*valuePointer).size(), var["p"].as<String>().c_str());
(*valuePointer)[rowNr] = value; //value should be an uint16_t
}
else if (var["type"] == "number") {
std::vector<uint16_t> *valuePointer = (std::vector<uint16_t> *)pointer;
while (rowNr >= (*valuePointer).size()) (*valuePointer).push_back(UINT16_MAX); //create vector space if needed...
(*valuePointer)[rowNr] = var["value"][rowNr]; //value should be an uint16_t
(*valuePointer)[rowNr] = value; //value should be an uint16_t
}
else if (var["type"] == "coord3D") {
std::vector<Coord3D> *valuePointer = (std::vector<Coord3D> *)pointer;
Expand All @@ -277,24 +286,24 @@ bool SysModModel::callVarOnChange(JsonObject var, unsigned8 rowNr, bool init) {
else
print->printJson("dev callVarOnChange type not supported yet", var);

ppf("callVarOnChange set pointer to vector %s[%d]: v:%s p:%d\n", variable.id(), rowNr, variable.valueString(), pointer);
ppf("callVarOnChange set pointer to vector %s[%d]: v:%s p:%d\n", variable.id(), rowNr, variable.valueString().c_str(), pointer);
} else
print->printJson("dev value is array but no rowNr\n", var);
} else {
if (var["type"] == "select" || var["type"] == "checkbox" || var["type"] == "range") {
uint8_t *valuePointer = (uint8_t *)pointer;
*valuePointer = var["value"];
*valuePointer = value;
}
else if (var["type"] == "number") {
uint16_t *valuePointer = (uint16_t *)pointer;
*valuePointer = var["value"];
*valuePointer = value;
}
else if (var["type"] == "coord3D") {
Coord3D *valuePointer = (Coord3D *)pointer;
*valuePointer = var["value"];
*valuePointer = value;
}

ppf("callVarOnChange set pointer %s[%d]: v:%s p:%d\n", variable.id(), rowNr, variable.valueString(), pointer);
ppf("callVarOnChange set pointer %s[%d]: v:%s p:%d\n", variable.id(), rowNr, variable.valueString().c_str(), pointer);
}

// else if (var["type"] == "text") {
Expand Down
36 changes: 18 additions & 18 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,6 @@ class Variable {

//extra methods

unsigned8 linearToLogarithm(unsigned8 value) {
if (value == 0) return 0;

float minp = var["min"].isNull()?var["min"]:0;
float maxp = var["max"].isNull()?var["max"]:255;

// The result should be between 100 an 10000000
float minv = minp?log(minp):0;
float maxv = log(maxp);

// calculate adjustment factor
float scale = (maxv-minv) / (maxp-minp);

return round(exp(minv + scale*((float)value-minp)));
}

void preDetails() {
for (JsonObject varChild: children()) { //for all controls
if (Variable(varChild).order() >= 0) { //post init
Expand Down Expand Up @@ -419,7 +403,7 @@ class SysModModel:public SysModule {
else {
//only print if ! read only
if (!variable.readOnly())
ppf("setValue changed %s %s -> %s\n", variable.id(), var["oldValue"].as<String>().c_str(), variable.valueString());
ppf("setValue changed %s %s -> %s\n", variable.id(), var["oldValue"].as<String>().c_str(), variable.valueString().c_str());
// else
// ppf("setValue changed %s %s\n", Variable(var).id(), var["value"].as<String>().c_str());
web->addResponse(var["id"], "value", var["value"]);
Expand Down Expand Up @@ -509,7 +493,7 @@ class SysModModel:public SysModule {
else if (valueArray.size())
return valueArray[0]; //return the first element
else {
ppf("dev getValue no array or rownr wrong %s %s %d\n", variable.id(), variable.valueString(), rowNr);
ppf("dev getValue no array or rownr wrong %s %s %d\n", variable.id(), variable.valueString().c_str(), rowNr);
return JsonVariant(); // return null
}
}
Expand All @@ -528,6 +512,22 @@ class SysModModel:public SysModule {
//sends dash var change to udp (if init), sets pointer if pointer var and run onChange
bool callVarOnChange(JsonObject var, unsigned8 rowNr = UINT8_MAX, bool init = false);

unsigned8 linearToLogarithm(unsigned8 value, uint8_t minp = 0, uint8_t maxp = UINT8_MAX) {
if (value == 0) return 0;

// float minp = var["min"].isNull()?var["min"]:0;
// float maxp = var["max"].isNull()?var["max"]:255;

// The result should be between 100 an 10000000
float minv = minp?log(minp):0;
float maxv = log(maxp);

// calculate adjustment factor
float scale = (maxv-minv) / (maxp-minp);

return round(exp(minv + scale*((float)value-minp)));
}

private:
bool doShowObsolete = false;
bool cleanUpModelDone = false;
Expand Down
8 changes: 4 additions & 4 deletions src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ class SysModUI: public SysModule {
if (mdl->setValueRowNr == UINT8_MAX)
var["p"] = pointer; //store pointer!
else
var["p"][mdl->setValueRowNr] = pointer; //store pointer in array! Needed??? Check controls!!!!
ppf("initValue pointer to vector stored %s: %s\n", variable.id(), (void *)(var["p"].as<String>().c_str()));
var["p"][mdl->setValueRowNr] = pointer; //store pointer in array!
ppf("initValue pointer stored %s: %s\n", variable.id(), (void *)(var["p"].as<String>().c_str()));
}

if (min) var["min"] = min;
Expand Down Expand Up @@ -266,7 +266,7 @@ class SysModUI: public SysModule {
}

if (onChangeExists)
ppf("initValue onChange init %s[x] <- %s\n", variable.id(), variable.valueString());
ppf("initValue onChange init %s[x] <- %s\n", variable.id(), variable.valueString().c_str());
}
}
return false;
Expand Down Expand Up @@ -335,7 +335,7 @@ class SysModUI: public SysModule {
else {
ppf(" (");
if (funType == onChange) ppf("%s ->", var["oldValue"].as<String>().c_str());
ppf("%s)\n", variable.valueString());
ppf("%s)\n", variable.valueString().c_str());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SysModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void SysModules::setup() {
for (JsonObject childVar: Variable(mdl->findVar("mdlTbl")).children()) {
Variable childVariable = Variable(childVar);
if (!childVar["value"].isNull() && childVariable.valArray().size() != modules.size()) {
ppf("mdlTbl clear (%s %s) %d %d\n", childVariable.id(), childVariable.valueString(), modules.size(), childVariable.valArray().size());
ppf("mdlTbl clear (%s %s) %d %d\n", childVariable.id(), childVariable.valueString().c_str(), modules.size(), childVariable.valArray().size());
childVar.remove("value");
}
}
Expand Down

0 comments on commit 956dbed

Please sign in to comment.