SensESP
2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
analog_reader.h
Go to the documentation of this file.
1
#ifndef _analog_reader_H_
2
#define _analog_reader_H_
3
4
#include "Arduino.h"
5
#include "
sensesp.h
"
6
#if defined(ESP32)
7
#include "esp_adc_cal.h"
8
#endif
9
10
namespace
sensesp
{
11
15
class
BaseAnalogReader
{
16
private
:
17
int
output_scale_;
18
19
public
:
20
virtual
bool
configure
() = 0;
21
virtual
float
read
() = 0;
22
};
23
24
class
ESP32AnalogReader
:
public
BaseAnalogReader
{
25
private
:
26
int
pin_;
27
adc_atten_t
attenuation_ =
ADC_ATTEN_DB_11
;
28
// This should work with ESP32 and newer variants, ADCs are different
29
adc_bits_width_t
bit_width_ = (
adc_bits_width_t
)
ADC_WIDTH_BIT_DEFAULT
;
30
// maximum voltage readout for 3.3V VDDA when attenuation_ is set to 11 dB
31
const
float
kVmax_ = 3300;
32
int8_t
adc_channel_;
33
esp_adc_cal_characteristics_t
adc_characteristics_;
34
const
int
kVref_ = 1100;
// voltage reference, in mV
35
36
public
:
37
ESP32AnalogReader
(
int
pin) : pin_{pin} {
38
if
(!(32 <= pin && pin <= 39)) {
39
debugE
(
"Only ADC1 is supported at the moment"
);
40
adc_channel_ = -1;
41
return
;
42
}
43
adc_channel_ =
digitalPinToAnalogChannel
(pin);
44
}
45
46
bool
configure
() {
47
if
(adc_channel_ == -1) {
48
return
false
;
49
}
50
adc1_config_width
(bit_width_);
51
adc1_config_channel_atten
((
adc1_channel_t
)adc_channel_, attenuation_);
52
esp_adc_cal_characterize
(
ADC_UNIT_1
, attenuation_, bit_width_, kVref_,
53
&adc_characteristics_);
54
return
true
;
55
}
56
57
float
read
() {
58
uint32_t
voltage
;
59
esp_adc_cal_get_voltage
((
adc_channel_t
)adc_channel_, &adc_characteristics_,
60
&
voltage
);
61
return
voltage
/ kVmax_;
62
}
63
};
64
typedef
ESP32AnalogReader
AnalogReader
;
65
66
}
// namespace sensesp
67
68
#endif
sensesp::BaseAnalogReader
Used by AnalogInput as a hardware abstraction layer.
Definition
analog_reader.h:15
sensesp::BaseAnalogReader::read
virtual float read()=0
sensesp::BaseAnalogReader::configure
virtual bool configure()=0
sensesp::ESP32AnalogReader
Definition
analog_reader.h:24
sensesp::ESP32AnalogReader::configure
bool configure()
Definition
analog_reader.h:46
sensesp::ESP32AnalogReader::read
float read()
Definition
analog_reader.h:57
sensesp::ESP32AnalogReader::ESP32AnalogReader
ESP32AnalogReader(int pin)
Definition
analog_reader.h:37
sensesp::LambdaTransform
Construct a new transform based on a single function.
Definition
lambda_transform.h:71
debugE
#define debugE(fmt,...)
Definition
local_debug.h:50
sensesp
Definition
sensesp.h:22
sensesp::AnalogReader
ESP32AnalogReader AnalogReader
Definition
analog_reader.h:64
sensesp.h
src
sensesp
sensors
analog_reader.h
Generated by
1.9.8