SensESP
3.0.1
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 "Arduino.h"
7
#if defined(ESP32)
8
#include "esp_adc_cal.h"
9
#endif
10
11
namespace
sensesp
{
12
16
class
BaseAnalogReader
{
17
private
:
18
int
output_scale_;
19
20
public
:
21
virtual
bool
configure
() = 0;
22
virtual
float
read
() = 0;
23
};
24
25
class
ESP32AnalogReader
:
public
BaseAnalogReader
{
26
protected
:
27
int
pin_
;
28
adc_atten_t
attenuation_
= ADC_ATTEN_DB_12;
29
// This should work with ESP32 and newer variants, ADCs are different
30
adc_bits_width_t
bit_width_
= (adc_bits_width_t)ADC_WIDTH_BIT_DEFAULT;
31
// maximum voltage readout for 3.3V VDDA when attenuation_ is set to 11 dB
32
const
float
kVmax_
= 3300;
33
int8_t
adc_channel_
;
34
esp_adc_cal_characteristics_t
adc_characteristics_
;
35
const
int
kVref_
= 1100;
// voltage reference, in mV
36
37
public
:
38
ESP32AnalogReader
(
int
pin) :
pin_
{pin} {
39
if
(!(32 <= pin && pin <= 39)) {
40
ESP_LOGE(__FILENAME__,
"Only ADC1 is supported at the moment"
);
41
adc_channel_
= -1;
42
return
;
43
}
44
adc_channel_
= digitalPinToAnalogChannel(pin);
45
}
46
47
bool
configure
() {
48
if
(
adc_channel_
== -1) {
49
return
false
;
50
}
51
adc1_config_width(
bit_width_
);
52
adc1_config_channel_atten((adc1_channel_t)
adc_channel_
,
attenuation_
);
53
esp_adc_cal_characterize(ADC_UNIT_1,
attenuation_
,
bit_width_
,
kVref_
,
54
&
adc_characteristics_
);
55
return
true
;
56
}
57
58
float
read
() {
59
uint32_t voltage;
60
esp_adc_cal_get_voltage((adc_channel_t)
adc_channel_
, &
adc_characteristics_
,
61
&voltage);
62
return
voltage /
kVmax_
;
63
}
64
};
65
typedef
ESP32AnalogReader
AnalogReader
;
66
67
}
// namespace sensesp
68
69
#endif
sensesp::BaseAnalogReader
Used by AnalogInput as a hardware abstraction layer.
Definition
analog_reader.h:16
sensesp::BaseAnalogReader::read
virtual float read()=0
sensesp::BaseAnalogReader::configure
virtual bool configure()=0
sensesp::ESP32AnalogReader
Definition
analog_reader.h:25
sensesp::ESP32AnalogReader::pin_
int pin_
Definition
analog_reader.h:27
sensesp::ESP32AnalogReader::configure
bool configure()
Definition
analog_reader.h:47
sensesp::ESP32AnalogReader::read
float read()
Definition
analog_reader.h:58
sensesp::ESP32AnalogReader::bit_width_
adc_bits_width_t bit_width_
Definition
analog_reader.h:30
sensesp::ESP32AnalogReader::ESP32AnalogReader
ESP32AnalogReader(int pin)
Definition
analog_reader.h:38
sensesp::ESP32AnalogReader::kVref_
const int kVref_
Definition
analog_reader.h:35
sensesp::ESP32AnalogReader::adc_characteristics_
esp_adc_cal_characteristics_t adc_characteristics_
Definition
analog_reader.h:34
sensesp::ESP32AnalogReader::kVmax_
const float kVmax_
Definition
analog_reader.h:32
sensesp::ESP32AnalogReader::attenuation_
adc_atten_t attenuation_
Definition
analog_reader.h:28
sensesp::ESP32AnalogReader::adc_channel_
int8_t adc_channel_
Definition
analog_reader.h:33
sensesp
Definition
sensesp.cpp:7
sensesp::AnalogReader
ESP32AnalogReader AnalogReader
Definition
analog_reader.h:65
sensesp.h
src
sensesp
sensors
analog_reader.h
Generated by
1.12.0