SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
analogvoltage.cpp
Go to the documentation of this file.
1#include "analogvoltage.h"
2
3namespace sensesp {
4
5AnalogVoltage::AnalogVoltage(float max_voltage, float multiplier, float offset,
6 const String& config_path)
7 : FloatTransform(config_path),
8 max_voltage_{max_voltage},
9 multiplier_{multiplier},
10 offset_{offset} {
11 load();
12}
13
14void AnalogVoltage::set(const float& input) {
15 this->emit(((input * (max_voltage_ / MAX_ANALOG_OUTPUT)) * multiplier_) +
16 offset_);
17}
18
19bool AnalogVoltage::to_json(JsonObject& root) {
20 root["max_voltage"] = max_voltage_;
21 root["multiplier"] = multiplier_;
22 root["offset"] = offset_;
23 return true;
24}
25
26bool AnalogVoltage::from_json(const JsonObject& config) {
27 const String expected[] = {"max_voltage", "multiplier", "offset"};
28 for (auto str : expected) {
29 if (!config[str].is<JsonVariant>()) {
30 return false;
31 }
32 }
33 max_voltage_ = config["max_voltage"];
34 multiplier_ = config["multiplier"];
35 offset_ = config["offset"];
36 return true;
37}
38
39const String ConfigSchema(const AnalogVoltage& obj) {
40 return R"###({"type":"object","properties":{"max_voltage":{"title":"Max voltage","type":"number","description":"The maximum voltage allowed into your ESP's Analog Input pin"},"multiplier":{"title":"Multiplier","type":"number","description":"Output will be multiplied by this before sending to SK"},"offset":{"title":"Offset","type":"number","description":"This will be added to output before sending to SK"}}})###";
41}
42
43} // namespace sensesp
#define MAX_ANALOG_OUTPUT
A transform that takes the output of an analog-to-digital converter and outputs the voltage that went...
virtual bool to_json(JsonObject &root) override
virtual void set(const float &input) override
AnalogVoltage(float max_voltage=3.3, float multiplier=1.0, float offset=0.0, const String &config_path="")
virtual bool from_json(const JsonObject &config) 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)