Utilize Value class in parameters for

Now bool parameters also may be used.
This commit is contained in:
Oleg Kalachev
2026-08-14 06:11:43 +03:00
parent 2d1efac05f
commit 6098293776
4 changed files with 30 additions and 29 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ Parameters subsystem (`parameters.ino`) uses standard [Preferences.h](https://do
To add a new parameter:
1. Define a global variable for the parameter, two types are supported: `float` and `int`.
1. Define a global variable for the parameter, three types are supported: `float`, `int`, and `bool`.
2. Add an entry to the `parameters` array, with the parameter name, a pointer to the variable, and optionally a callback function to call when the parameter is changed.
3. Everything else will be handled automatically.
+1 -1
View File
@@ -105,7 +105,7 @@ void doCommand(String str, bool echo = false) {
if (success) {
print("%s = %g\n", arg0.c_str(), getParameter(arg0.c_str()));
} else {
print("Parameter not found: %s\n", arg0.c_str());
print("Cannot set parameter: %s\n", arg0.c_str());
}
} else if (command == "preset") {
resetParameters();
+16 -21
View File
@@ -20,15 +20,11 @@ Preferences storage;
struct Parameter {
const char *name; // max length is 15
bool integer;
union { float *f; int *i; }; // pointer to the variable
float inital; // default value
Value value; // pointer to the variable
float initial; // default value
float cache; // what's stored in flash
void (*callback)(); // called after parameter change
Parameter(const char *name, float *variable, void (*callback)() = nullptr) : name(name), integer(false), f(variable), callback(callback) {};
Parameter(const char *name, int *variable, void (*callback)() = nullptr) : name(name), integer(true), i(variable), callback(callback) {};
float getValue() const { return integer ? *i : *f; };
void setValue(const float value) { if (integer) *i = value; else *f = value; };
Parameter(const char *name, Value value, void (*callback)() = nullptr) : name(name), value(value), callback(callback) {};
};
Parameter parameters[] = {
@@ -168,11 +164,11 @@ void setupParameters() {
storage.begin("flix");
// Read parameters from storage
for (auto &parameter : parameters) {
parameter.inital = parameter.getValue();
parameter.initial = parameter.value.get();
if (storage.isKey(parameter.name)) {
parameter.setValue(storage.getFloat(parameter.name));
parameter.value.set(storage.getFloat(parameter.name));
}
parameter.cache = parameter.getValue();
parameter.cache = parameter.value.get();
}
}
@@ -187,13 +183,13 @@ const char *getParameterName(int index) {
float getParameter(int index) {
if (index < 0 || index >= parametersCount()) return NAN;
return parameters[index].getValue();
return parameters[index].value.get();
}
float getParameter(const char *name) {
for (auto &parameter : parameters) {
if (strcasecmp(parameter.name, name) == 0) {
return parameter.getValue();
return parameter.value.get();
}
}
return NAN;
@@ -202,10 +198,9 @@ float getParameter(const char *name) {
bool setParameter(const char *name, const float value) {
for (auto &parameter : parameters) {
if (strcasecmp(parameter.name, name) == 0) {
if (parameter.integer && !isfinite(value)) return false; // can't set integer to NaN or Inf
parameter.setValue(value);
bool success = parameter.value.set(value);
if (parameter.callback) parameter.callback();
return true;
return success;
}
}
return false;
@@ -217,10 +212,10 @@ void syncParameters() {
if (motorsActive()) return; // don't use flash while flying, it may cause a delay
for (auto &parameter : parameters) {
if (floatEquals(parameter.getValue(), parameter.cache)) continue; // no change
if (floatEquals(parameter.value.get(), parameter.cache)) continue; // no change
storage.putFloat(parameter.name, parameter.getValue());
parameter.cache = parameter.getValue(); // update cache
storage.putFloat(parameter.name, parameter.value.get());
parameter.cache = parameter.value.get(); // update cache
}
}
@@ -229,10 +224,10 @@ void printParameters(const char *filter) {
for (auto &parameter : parameters) {
if (strncasecmp(parameter.name, filter, strlen(filter))) continue;
if (floatEquals(parameter.getValue(), parameter.inital)) { // parameter changed
print("%-15s %-13g\n", parameter.name, parameter.getValue());
if (floatEquals(parameter.value.get(), parameter.initial)) { // parameter changed
print("%-15s %-13g\n", parameter.name, parameter.value.get());
} else {
print("%-15s %-13g [%g]\n", parameter.name, parameter.getValue(), parameter.inital);
print("%-15s %-13g [%g]\n", parameter.name, parameter.value.get(), parameter.initial);
}
}
}
+12 -6
View File
@@ -99,13 +99,19 @@ struct Value {
}
};
void set(float value) const {
switch (type) {
case FLOAT: *_float = value; break;
case INT: *_int = value; break;
case BOOL: *_bool = (value != 0); break;
default: break;
bool set(float value) const {
if (type == FLOAT) {
*_float = value;
return true;
} else if (type == INT) {
if (!isfinite(value)) return false;
*_int = value;
return true;
} else if (type == BOOL) {
*_bool = (value != 0);
return true;
}
return false;
};
};