From b478bedc59d8d3db6dadbea118776d155cf79bc6 Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Sun, 27 Oct 2024 20:03:22 +0200 Subject: [PATCH] Restore Threshold transform --- src/sensesp/transforms/threshold.cpp | 27 ++++++ src/sensesp/transforms/threshold.h | 126 +++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/sensesp/transforms/threshold.cpp create mode 100644 src/sensesp/transforms/threshold.h diff --git a/src/sensesp/transforms/threshold.cpp b/src/sensesp/transforms/threshold.cpp new file mode 100644 index 000000000..28b8a8a5b --- /dev/null +++ b/src/sensesp/transforms/threshold.cpp @@ -0,0 +1,27 @@ +#include "threshold.h" + +namespace sensesp { + +static const char kSchemaTemplate[] PROGMEM = R"({ + "type": "object", + "properties": { + "min": { "title": "Minimum value", "type": "!!TYPE!!", "description" : "Minimum value to be 'in range'" }, + "max": { "title": "Maximum value", "type": "!!TYPE!!", "description" : "Maximum value to be 'in range'" }, + "in_range": { "title": "In range value", "type": "boolean", "description" : "Output value when input value is 'in range'" } + "out_range": { "title": "Out of range value", "type": "boolean", "description" : "Output value when input value is out of range" } + } + })"; + +const String ConfigSchema(const ThresholdTransform& obj) { + String schema = kSchemaTemplate; + schema.replace("!!TYPE!!", "number"); + return schema; +} + +const String ConfigSchema(const ThresholdTransform& obj) { + String schema = kSchemaTemplate; + schema.replace("!!TYPE!!", "integer"); + return schema; +} + +} // namespace sensesp diff --git a/src/sensesp/transforms/threshold.h b/src/sensesp/transforms/threshold.h new file mode 100644 index 000000000..45d135bd9 --- /dev/null +++ b/src/sensesp/transforms/threshold.h @@ -0,0 +1,126 @@ +#include "transform.h" +#ifndef _threshold_h +#define _threshold_h + +namespace sensesp { + +/** + * @brief A Transform base class that translates the value of type C into + * boolean. Base class for classes FloatThreshold and IntThreshold. + * + * @param min_value Minimum value of input for output to be the value of + * in_range. + * + * @param max_value Maximum value of input for output to be the value of + * in_range. + * + * @param in_range Output value if input value is in range. + * + * @param out_range Output value if input value is out of the range. + */ +template +class ThresholdTransform : public Transform { + public: + + ThresholdTransform(C min_value, C max_value, bool in_range, + String config_path = "") + : Transform(config_path), + min_value_{min_value}, + max_value_{max_value}, + in_range_{in_range} { + this->load(); + }; + + virtual void set(const C& new_value) override { + if (input >= min_value_ && input <= max_value_) { + this->output = in_range_; + } else { + this->output = !in_range_; + } + + this->notify(); + } + + bool from_json(const JsonObject& root) override { + String expected[] = {"min", "max", "in_range", "out_range"}; + for (auto str : expected) { + if (!config.containsKey(str)) { + return false; + } + } + min_value_ = root["min"]; + max_value_ = root["max"]; + in_range_ = root["in_range"]; + return true; + } + + bool to_json(JsonObject& root) override { + root["min"] = min_value_; + root["max"] = max_value_; + root["in_range"] = in_range_; + return true; + } + + protected: + C min_value_; + C max_value_; + bool in_range_; +}; + + + +/** + * @brief Translates a float value into a boolean value, which depends on + * whether the float value is "in range" or "out of range". + * + * @param min_value The minimum of the range for the input value to be "in + * range". + * + * @param max_value The maximum of the range for the input value to be "in + * range". + * + * @param in_range The output value if the input value is "in range". Default is + * true. (If the input value is not "in range", the value of output is the + * opposite of in_range.) + * + * @param config_path Path to configure this transform in the Config UI. + */ +class FloatThreshold : public ThresholdTransform { + public: + FloatThreshold(float min_value, float max_value, bool in_range = true, + String config_path = "") + : ThresholdTransform(min_value, max_value, in_range, config_path) { + } +}; + +/** + * @brief Translates an integer value into a boolean value, which depends on + * whether the integer value is "in range" or "out of range". + * + * @param min_value The minimum of the range for the input value to be "in + * range". + * + * @param max_value The maximum of the range for the input value to be "in + * range". + * + * @param in_range The output value if the input value is "in range". Default is + * true. (If the input value is not "in range", the value of output is the + * opposite of in_range.) + * + * @param config_path Path to configure this transform in the Config UI. + */ +class IntThreshold : public ThresholdTransform { + public: + IntThreshold(int min_value, int max_value, bool in_range = true, + String config_path = "") + : ThresholdTransform(min_value, max_value, in_range, config_path) {} +}; + +const String ConfigSchema(const ThresholdTransform& obj); +const String ConfigSchema(const ThresholdTransform& obj); + +typedef ThresholdTransform FloatThreshold; +typedef ThresholdTransform IntThreshold; + +} // namespace sensesp +#endif