SensESP 3.6.1-alpha
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() may replace read_delay_ with the value persisted in the file
18 // system, so arm the timer only after it has run.
19 load();
20
22 event_loop()->onRepeat(read_delay_, [this]() { this->update(); });
23}
24
26 this->emit(output_scale_ * analog_reader_->read());
27}
28
29bool AnalogInput::to_json(JsonObject& root) {
30 root["read_delay"] = read_delay_;
31 return true;
32};
33
34bool AnalogInput::from_json(const JsonObject& config) {
35 String const expected[] = {"read_delay"};
36 for (auto str : expected) {
37 if (!config[str].is<JsonVariant>()) {
38 return false;
39 }
40 }
41 read_delay_ = config["read_delay"];
42 return true;
43}
44
45const String ConfigSchema(AnalogInput& obj) {
46 return R"###({"type":"object","properties":{"read_delay":{"title":"Read delay","type":"number","description":"Number of milliseconds between each reading of the analog input"}} })###";
47}
48
49} // namespace sensesp
Sensor for reading the MCU analog input pins.
reactesp::RepeatEvent * repeat_event_
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:42
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