SensESP 3.3.0
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
voltagedivider.cpp
Go to the documentation of this file.
1#include "voltagedivider.h"
2
3namespace sensesp {
4
6 const String& config_path)
7 : SymmetricTransform<float>(config_path), R2_{R2}, Vin_{Vin} {
8 load();
9}
10
11void VoltageDividerR1::set(const float& Vout) {
12 if (Vout == 0.0f) return;
13 this->emit((Vin_ - Vout) * R2_ / Vout);
14}
15
16bool VoltageDividerR1::to_json(JsonObject& root) {
17 root["Vin"] = Vin_;
18 root["R2"] = R2_;
19 return true;
20}
21
22bool VoltageDividerR1::from_json(const JsonObject& config) {
23 String const expected[] = {"Vin", "R2"};
24 for (auto str : expected) {
25 if (!config[str].is<JsonVariant>()) {
26 ESP_LOGE(
27 __FILENAME__,
28 "Cannot set VoltageDividerR1: configuration: missing json field %s\n",
29 str.c_str());
30 return false;
31 }
32 }
33
34 Vin_ = config["Vin"];
35 R2_ = config["R2"];
36
37 return true;
38}
39
40const String ConfigSchema(const VoltageDividerR1& obj) {
41 return R"({"type":"object","properties":{"Vin":{"title":"Voltage in","type":"number"},"R2":{"title":"Resistance (ohms) of R2","type":"number"}}})";
42}
43
45 const String& config_path)
46 : SymmetricTransform<float>(config_path), R1_{R1}, Vin_{Vin} {
47 load();
48}
49
50void VoltageDividerR2::set(const float& Vout) {
51 if (Vin_ == Vout) return;
52 this->emit((Vout * R1_) / (Vin_ - Vout));
53}
54
55bool VoltageDividerR2::to_json(JsonObject& root) {
56 root["Vin"] = Vin_;
57 root["R1"] = R1_;
58 return true;
59}
60
61bool VoltageDividerR2::from_json(const JsonObject& config) {
62 String const expected[] = {"Vin", "R1"};
63 for (auto str : expected) {
64 if (!config[str].is<JsonVariant>()) {
65 ESP_LOGE(
66 __FILENAME__,
67 "Cannot set VoltageDividerR2: configuration: missing json field %s\n",
68 str.c_str());
69 return false;
70 }
71 }
72
73 Vin_ = config["Vin"];
74 R1_ = config["R1"];
75
76 return true;
77}
78
79const String ConfigSchema(const VoltageDividerR2& obj) {
80 return R"({"type":"object","properties":{"Vin":{"title":"Voltage in","type":"number"},"R1":{"title":"Resistance (ohms) of R1","type":"number"}}})";
81}
82
83} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
SymmetricTransform(String config_path="")
Definition transform.h:96
void emit(const float &new_value)
Uses the voltage divider formula to calculate (and output) the resistance of R1 in the circuit.
virtual bool from_json(const JsonObject &config) override
virtual void set(const float &Vout) override
virtual bool to_json(JsonObject &root) override
VoltageDividerR1(float R2, float Vin=3.3, const String &config_path="")
Uses the voltage divider formula to calculate (and output) the resistance of R2 in the circuit.
virtual bool from_json(const JsonObject &config) override
virtual bool to_json(JsonObject &root) override
VoltageDividerR2(float R1, float Vin=3.3, const String &config_path="")
virtual void set(const float &Vout) override
const String ConfigSchema(const SmartSwitchController &obj)