SensESP 3.0.1
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 this->emit((Vin_ - Vout) * R2_ / Vout);
13}
14
15bool VoltageDividerR1::to_json(JsonObject& root) {
16 root["Vin"] = Vin_;
17 root["R2"] = R2_;
18 return true;
19}
20
21bool VoltageDividerR1::from_json(const JsonObject& config) {
22 String const expected[] = {"Vin", "R2"};
23 for (auto str : expected) {
24 if (!config[str].is<JsonVariant>()) {
25 ESP_LOGE(
26 __FILENAME__,
27 "Cannot set VoltageDividerR1: configuration: missing json field %s\n",
28 str.c_str());
29 return false;
30 }
31 }
32
33 Vin_ = config["Vin"];
34 R2_ = config["R2"];
35
36 return true;
37}
38
39const String ConfigSchema(const VoltageDividerR1& obj) {
40 return R"({"type":"object","properties":{"Vin":{"title":"Voltage in","type":"number"},"R2":{"title":"Resistance (ohms) of R2","type":"number"}}})";
41}
42
44 const String& config_path)
45 : SymmetricTransform<float>(config_path), R1_{R1}, Vin_{Vin} {
46 load();
47}
48
49void VoltageDividerR2::set(const float& Vout) {
50 this->emit((Vout * R1_) / (Vin_ - Vout));
51}
52
53bool VoltageDividerR2::to_json(JsonObject& root) {
54 root["Vin"] = Vin_;
55 root["R1"] = R1_;
56 return true;
57}
58
59bool VoltageDividerR2::from_json(const JsonObject& config) {
60 String const expected[] = {"Vin", "R1"};
61 for (auto str : expected) {
62 if (!config[str].is<JsonVariant>()) {
63 ESP_LOGE(
64 __FILENAME__,
65 "Cannot set VoltageDividerR2: configuration: missing json field %s\n",
66 str.c_str());
67 return false;
68 }
69 }
70
71 Vin_ = config["Vin"];
72 R1_ = config["R1"];
73
74 return true;
75}
76
77const String ConfigSchema(const VoltageDividerR2& obj) {
78 return R"({"type":"object","properties":{"Vin":{"title":"Voltage in","type":"number"},"R1":{"title":"Resistance (ohms) of R1","type":"number"}}})";
79}
80
81} // namespace sensesp
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
A common type of transform that consumes, transforms, then outputs values of the same data type.
Definition transform.h:94
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)