diff --git a/docs/firmware.md b/docs/firmware.md index 8db55cd..ad734f0 100644 --- a/docs/firmware.md +++ b/docs/firmware.md @@ -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. diff --git a/flix/cli.ino b/flix/cli.ino index c6273c1..662de44 100644 --- a/flix/cli.ino +++ b/flix/cli.ino @@ -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(); diff --git a/flix/parameters.ino b/flix/parameters.ino index c478bbb..5232ec6 100644 --- a/flix/parameters.ino +++ b/flix/parameters.ino @@ -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 ¶meter : 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 ¶meter : 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 ¶meter : 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 ¶meter : 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 ¶meter : 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); } } } diff --git a/flix/util.h b/flix/util.h index 3672c98..e4ddd48 100644 --- a/flix/util.h +++ b/flix/util.h @@ -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; }; };