SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
integrator.h
Go to the documentation of this file.
1#ifndef SENSESP_TRANSFORMS_INTEGRATOR_H_
2#define SENSESP_TRANSFORMS_INTEGRATOR_H_
3
5#include "transform.h"
6
7namespace sensesp {
8
18template <class C, class P>
19class Integrator : public Transform<C, P> {
20 public:
28 Integrator(P k = 1, P value = 0, String config_path = "")
29 : Transform<C, P>(config_path), k{k}, value{value} {
30 this->load();
31 this->emit(value);
32 }
33
34 virtual void set(const C& input) override final {
35 value += input * k;
36 this->emit(value);
37 }
38
39 void reset() { value = 0; }
40
41 virtual bool to_json(JsonObject& doc) override final {
42 doc["k"] = k;
43 return true;
44 }
45 virtual bool from_json(const JsonObject& config) override final {
46 if (!config["k"].is<P>()) {
47 return false;
48 }
49 k = config["k"];
50 return true;
51 }
52
53 private:
54 P k;
55 P value = 0;
56};
57
58template <typename T>
59const String ConfigSchema(const Integrator<T, T>& obj) {
60 return R"({"type":"object","properties":{"k":{"title":"Multiplier","type":"number"}}})";
61}
62
65
66} // namespace sensesp
67#endif
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Integrator integrates (accumulates) the incoming values.
Definition integrator.h:19
virtual bool to_json(JsonObject &doc) override final
Definition integrator.h:41
virtual void set(const C &input) override final
Definition integrator.h:34
Integrator(P k=1, P value=0, String config_path="")
Construct a new Integrator T object.
Definition integrator.h:28
virtual bool from_json(const JsonObject &config) override final
Definition integrator.h:45
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
void emit(const P &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
Integrator< float, float > FloatIntegrator
Definition integrator.h:63
Integrator< int, int > Accumulator
Definition integrator.h:64