SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
curveinterpolator.h
Go to the documentation of this file.
1#ifndef SENSESP_TRANSFORMS_CURVEINTERPOLATOR_H_
2#define SENSESP_TRANSFORMS_CURVEINTERPOLATOR_H_
3
5#include "transform.h"
6
7namespace sensesp {
8
40 public:
41 class Sample {
42 public:
43 float input_{};
44 float output_{};
45
47 Sample(float input, float output);
48 Sample(JsonObject& obj);
49
50 friend bool operator<(const Sample& lhs, const Sample& rhs) {
51 return lhs.input_ < rhs.input_;
52 }
53 };
54
55 public:
56 CurveInterpolator(std::set<Sample>* defaults = NULL,
57 const String& config_path = "");
58
59 // Set and retrieve the transformed value
60 void set(const float& input) override;
61
62 // Web UI configuration methods
63 CurveInterpolator* set_input_title(String input_title) {
64 input_title_ = input_title;
65 return this;
66 }
67 CurveInterpolator* set_output_title(String output_title) {
68 output_title_ = output_title;
69 return this;
70 }
71
72 // For reading and writing the configuration of this transformation
73 virtual bool to_json(JsonObject& doc) override;
74 virtual bool from_json(const JsonObject& doc) override;
75
76 // For manually adding sample points
77 void clear_samples();
78
79 void add_sample(const Sample& new_sample);
80
81 const std::set<Sample>& get_samples() const { return samples_; }
82
83 protected:
84 std::set<Sample> samples_{};
85 String input_title_ = "Input";
86 String output_title_ = "Output";
87
88 friend const char* ConfigSchema(const CurveInterpolator& obj);
89};
90
91inline const char* ConfigSchema(const CurveInterpolator& obj) {
92 static const char schema[] = R"({"type":"object","properties":{"samples":{"title":"Sample values","type":"array","format":"table","items":{"type":"object","properties":{"input":{"type":"number","title":"%s"},"output":{"type":"number","title":"%s"}}}}}})";
93 static char buf[sizeof(schema) + 160];
94 snprintf(buf, sizeof(buf), schema, obj.input_title_.c_str(),
95 obj.output_title_.c_str());
96 return buf;
97}
98
99} // namespace sensesp
100
101#endif
friend bool operator<(const Sample &lhs, const Sample &rhs)
Implement a piecewise linear interpolation transform.
CurveInterpolator * set_input_title(String input_title)
void set(const float &input) override
CurveInterpolator * set_output_title(String output_title)
virtual bool to_json(JsonObject &doc) override
friend const char * ConfigSchema(const CurveInterpolator &obj)
void add_sample(const Sample &new_sample)
CurveInterpolator(std::set< Sample > *defaults=NULL, const String &config_path="")
Construct a new CurveInterpolator object.
virtual bool from_json(const JsonObject &doc) override
const std::set< Sample > & get_samples() const
const String ConfigSchema(const SmartSwitchController &obj)