SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
curveinterpolator.cpp
Go to the documentation of this file.
1#include "curveinterpolator.h"
2
3#include "sensesp.h"
4
5namespace sensesp {
6
8
10 : input{input}, output{output} {}
11
13 : input{obj["input"]}, output{obj["output"]} {}
14
26 // Load default values if no configuration present...
27 if (defaults != NULL) {
28 samples_.clear();
30 }
31
33}
34
36 float x0 = 0.0;
37 float y0 = 0.0;
38
39 std::set<Sample>::iterator it = samples_.begin();
40
41 while (it != samples_.end()) {
42 auto& sample = *it;
43
44 if (input > sample.input) {
45 x0 = sample.input;
47 } else {
48 break;
49 }
50
51 it++;
52 }
53
54 if (it != samples_.end()) {
55 // We found our range. "input" is between
56 // minInput and it->input. CurveInterpolator the result
57 Sample max = *it;
58 float x1 = max.input;
59 float y1 = max.output;
60
61 output = (y0 * (x1 - input) + y1 * (input - x0)) / (x1 - x0);
62 } else {
63 // Hit the end of the table with no match.
64 output = 9999.9;
65 // debugW("Could not find sample interval for input %0f", input);
66 }
67
68 notify();
69}
70
72 JsonArray json_samples = root.createNestedArray("samples");
73 for (auto& sample : samples_) {
74 auto entry = json_samples.createNestedObject();
75 if( entry.isNull() ) {
76 debugE("No memory for sample");
77 }
78 entry["input"] = sample.input;
79 entry["output"] = sample.output;
80 }
81}
82
83static const char SCHEMA[] PROGMEM = R"({
84 "type": "object",
85 "properties": {
86 "samples": { "title": "Sample values",
87 "type": "array",
88 "format": "table",
89 "items": { "type": "object",
90 "properties": {
91 "input": { "type": "number",
92 "title": "%s" },
93 "output": { "type": "number",
94 "title": "%s" }
95 }}}
96 }
97 })";
98
100 char buf[sizeof(SCHEMA) + 160];
101 snprintf_P(buf, sizeof(buf), SCHEMA, input_title_.c_str(),
102 output_title_.c_str());
103 return String(buf);
104}
105
107 String expected[] = {"samples"};
108 for (auto str : expected) {
109 if (!config.containsKey(str)) {
110 debugE(
111 "Can not set CurveInterpolator configuration: missing json field "
112 "%s\n",
113 str.c_str());
114 return false;
115 }
116 }
117
118 JsonArray arr = config["samples"];
119 if (arr.size() > 0) {
120 samples_.clear();
121 for (auto jentry : arr) {
122 auto json_object = jentry.as<JsonObject>();
124 samples_.insert(sample);
125 }
126 }
127
128 return true;
129}
130
132
137
138} // namespace sensesp
virtual void load_configuration()
virtual String get_config_schema() override
virtual bool set_configuration(const JsonObject &config) override
virtual void get_configuration(JsonObject &doc) override
void set_input(float input, uint8_t input_channel=0) override
void add_sample(const Sample &new_sample)
CurveInterpolator(std::set< Sample > *defaults=NULL, String config_path="")
Construct a new CurveInterpolator object.
Construct a new transform based on a single function.
const uint8_t PAGE_css_bootstrap[] PROGMEM
#define max(a, b)
#define debugE(fmt,...)
Definition local_debug.h:50