SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
analog_input.cpp
Go to the documentation of this file.
1#include "sensesp.h"
2
3#include "analog_input.h"
4
5#include "Arduino.h"
6#include "sensesp_base_app.h"
7
8namespace sensesp {
9
10AnalogInput::AnalogInput(uint8_t pin, unsigned int read_delay,
11 const String& config_path, float output_scale)
12 : FloatSensor(config_path),
13 pin{pin},
14 read_delay{read_delay},
15 output_scale{output_scale} {
16 analog_reader_ = std::unique_ptr<AnalogReader>(new AnalogReader(pin));
17 load();
18
19 if (this->analog_reader_->configure()) {
20 event_loop()->onRepeat(read_delay, [this]() { this->update(); });
21 }
22}
23
25
26bool AnalogInput::to_json(JsonObject& root) {
27 root["read_delay"] = read_delay;
28 return true;
29};
30
31bool AnalogInput::from_json(const JsonObject& config) {
32 String const expected[] = {"read_delay"};
33 for (auto str : expected) {
34 if (!config[str].is<JsonVariant>()) {
35 return false;
36 }
37 }
38 read_delay = config["read_delay"];
39 return true;
40}
41
42const String ConfigSchema(AnalogInput& obj) {
43 return R"###({"type":"object","properties":{"read_delay":{"title":"Read delay","type":"number","description":"Number of milliseconds between each analogRead(A0)"}} })###";
44}
45
46} // namespace sensesp
Sensor for reading the MCU analog input pins.
std::unique_ptr< BaseAnalogReader > analog_reader_
virtual bool from_json(const JsonObject &config) override
virtual bool to_json(JsonObject &root) override
AnalogInput(uint8_t pin=A0, unsigned int read_delay=200, const String &config_path="", float output_scale=1024.)
unsigned int read_delay
virtual bool load() override
Load and populate the object from a persistent storage.
Definition saveable.cpp:8
Sensor template class for any sensor producing actual values.
Definition sensor.h:40
void emit(const T &new_value)
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
ESP32AnalogReader AnalogReader