SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
analog_input.cpp
Go to the documentation of this file.
1#include "analog_input.h"
2
3#include "Arduino.h"
4#include "sensesp.h"
5
6namespace sensesp {
7
8AnalogInput::AnalogInput(uint8_t pin, unsigned int read_delay, String config_path,
9 float output_scale)
11 pin{pin},
12 read_delay{read_delay},
13 output_scale{output_scale} {
14 analog_reader = new AnalogReader(pin);
16}
17
18void AnalogInput::update() { this->emit(output_scale * analog_reader->read()); }
19
21 if (this->analog_reader->configure()) {
22 ReactESP::app->onRepeat(read_delay, [this]() { this->update(); });
23 }
24}
25
26void AnalogInput::get_configuration(JsonObject& root) {
27 root["read_delay"] = read_delay;
28};
29
30static const char SCHEMA[] PROGMEM = R"###({
31 "type": "object",
32 "properties": {
33 "read_delay": { "title": "Read delay", "type": "number", "description": "Number of milliseconds between each analogRead(A0)" }
34 }
35 })###";
36
37String AnalogInput::get_config_schema() { return FPSTR(SCHEMA); }
38
39bool AnalogInput::set_configuration(const JsonObject& config) {
40 String expected[] = {"read_delay"};
41 for (auto str : expected) {
42 if (!config.containsKey(str)) {
43 return false;
44 }
45 }
46 read_delay = config["read_delay"];
47 return true;
48}
49
50} // namespace sensesp
AnalogInput(uint8_t pin=A0, unsigned int read_delay=200, String config_path="", float output_scale=1024.)
void start() override final
virtual float read()=0
virtual bool configure()=0
virtual void load_configuration()
Construct a new transform based on a single function.
Sensor template class for any sensor producing actual values.
Definition sensor.h:45
void emit(T new_value)
const uint8_t PAGE_css_bootstrap[] PROGMEM
ESP32AnalogReader AnalogReader