SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
change_filter.cpp
Go to the documentation of this file.
1#include "change_filter.h"
2
3namespace sensesp {
4
5static float absf(float val) {
6 if (val < 0) {
7 return -val;
8 }
9 return val;
10}
11
12ChangeFilter::ChangeFilter(float min_delta, float max_delta, int max_skips,
13 const String& config_path)
14 : FloatTransform(config_path),
15 min_delta_{min_delta},
16 max_delta_{max_delta},
17 max_skips_{max_skips},
18 skips_(max_skips_ + 1) {
19 load();
20}
21
22void ChangeFilter::set(const float& new_value) {
23 float delta = absf(new_value - output_);
24 if ((delta >= min_delta_ && delta <= max_delta_) || skips_ > max_skips_) {
25 skips_ = 0;
26 this->emit(new_value);
27 } else {
28 skips_++;
29 }
30}
31
32bool ChangeFilter::to_json(JsonObject& root) {
33 root["min_delta"] = min_delta_;
34 root["max_delta"] = max_delta_;
35 root["max_skips"] = max_skips_;
36 return true;
37}
38
39bool ChangeFilter::from_json(const JsonObject& config) {
40 String const expected[] = {"min_delta", "max_delta", "max_skips"};
41 for (auto str : expected) {
42 if (!config[str].is<JsonVariant>()) {
43 return false;
44 }
45 }
46 min_delta_ = config["min_delta"];
47 max_delta_ = config["max_delta"];
48 max_skips_ = config["max_skips"];
49 skips_ = max_skips_ + 1;
50 return true;
51}
52
53const String ConfigSchema(ChangeFilter& obj) {
54 return R"###({"type":"object","properties":{"min_delta":{"title":"Minimum delta","description":"Minimum difference in change of value before forwarding","type":"number"},"max_delta":{"title":"Maximum delta","description":"Maximum difference in change of value to allow forwarding","type":"number"},"max_skips":{"title":"Max skip count","description":"Maximum number of consecutive filtered values before one is allowed through","type":"number"}}})###";
55}
56
57} // namespace sensesp
A float passthrough transform that will only pass that value through if it is "sufficiently different...
virtual bool from_json(const JsonObject &config) override
virtual void set(const float &new_value) override
ChangeFilter(float min_delta=0.0, float max_delta=9999.0, int max_skips=99, const String &config_path="")
virtual bool to_json(JsonObject &root) override
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
void emit(const float &new_value)
const String ConfigSchema(const SmartSwitchController &obj)