SensESP 3.6.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
analog_reader.h
Go to the documentation of this file.
1#ifndef SENSESP_SENSORS_ANALOG_READER_H_
2#define SENSESP_SENSORS_ANALOG_READER_H_
3
4#include "sensesp.h"
5
6#include <cmath>
7
8#include "Arduino.h"
9
10namespace sensesp {
11
16 public:
17 virtual ~BaseAnalogReader() = default;
18
26 virtual float read() = 0;
27};
28
37 public:
39 static constexpr float kDefaultMaxVoltage = 3300.;
40
41 protected:
42 int pin_;
44
45 public:
53 ESP32AnalogReader(int pin, float max_voltage = kDefaultMaxVoltage)
54 : pin_{pin}, max_voltage_{max_voltage} {
55 if (!(max_voltage > 0.) || !std::isfinite(max_voltage)) {
57 ESP_LOGE(__FILENAME__,
58 "Invalid max_voltage %f mV; using the default %f mV instead",
59 max_voltage, max_voltage_);
60 }
61 }
62
63 float read() override { return analogReadMilliVolts(pin_) / max_voltage_; }
64};
66
67} // namespace sensesp
68
69#endif
Used by AnalogInput as a hardware abstraction layer.
virtual ~BaseAnalogReader()=default
virtual float read()=0
Read the input as a fraction of the full-scale reference voltage.
Calibrated analog reader for the ESP32 family.
static constexpr float kDefaultMaxVoltage
The nominal 3.3 V supply voltage, in millivolts.
ESP32AnalogReader(int pin, float max_voltage=kDefaultMaxVoltage)
float read() override
Read the input as a fraction of the full-scale reference voltage.
ESP32AnalogReader AnalogReader