libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ESP32ADC Class Reference
Inheritance diagram for LibXR::ESP32ADC:
[legend]
Collaboration diagram for LibXR::ESP32ADC:
[legend]

Public Member Functions

 ESP32ADC (adc_unit_t unit, adc_channel_t channel, adc_atten_t attenuation=ADC_ATTEN_DB_12, adc_bitwidth_t bitwidth=static_cast< adc_bitwidth_t >(SOC_ADC_DIGI_MIN_BITWIDTH), float reference_voltage=3.3f)
 
float Read () override
 读取 ADC
 
- Public Member Functions inherited from LibXR::ADC
 ADC ()=default
 默认构造函数
 

Private Member Functions

float Normalize (int raw) const
 

Private Attributes

adc_unit_t m_unit_
 
adc_channel_t m_channel_
 
adc_atten_t m_attenuation_
 
adc_bitwidth_t m_bitwidth_
 
float m_reference_voltage_
 
uint16_t m_max_raw_
 
adc_oneshot_unit_handle_t m_oneshot_ = nullptr
 

Detailed Description

Definition at line 10 of file esp_adc.hpp.

Constructor & Destructor Documentation

◆ ESP32ADC()

LibXR::ESP32ADC::ESP32ADC ( adc_unit_t unit,
adc_channel_t channel,
adc_atten_t attenuation = ADC_ATTEN_DB_12,
adc_bitwidth_t bitwidth = static_cast<adc_bitwidth_t>(SOC_ADC_DIGI_MIN_BITWIDTH),
float reference_voltage = 3.3f )
inlineexplicit

Definition at line 13 of file esp_adc.hpp.

17 : m_unit_(unit),
18 m_channel_(channel),
19 m_attenuation_(attenuation),
20 m_bitwidth_(bitwidth),
21 m_reference_voltage_(reference_voltage),
22 m_max_raw_((1 << bitwidth) - 1)
23 {
24 adc_oneshot_unit_init_cfg_t unit_cfg = {};
25 unit_cfg.unit_id = m_unit_;
26 unit_cfg.ulp_mode = ADC_ULP_MODE_DISABLE;
27
28 ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_cfg, &m_oneshot_));
29
30 adc_oneshot_chan_cfg_t chan_cfg = {
31 .atten = m_attenuation_,
32 .bitwidth = m_bitwidth_,
33 };
34 ESP_ERROR_CHECK(adc_oneshot_config_channel(m_oneshot_, m_channel_, &chan_cfg));
35 }

◆ ~ESP32ADC()

LibXR::ESP32ADC::~ESP32ADC ( )
inline

Definition at line 37 of file esp_adc.hpp.

38 {
39 if (m_oneshot_)
40 {
41 adc_oneshot_del_unit(m_oneshot_);
42 }
43 }

Member Function Documentation

◆ Normalize()

float LibXR::ESP32ADC::Normalize ( int raw) const
inlineprivate

Definition at line 56 of file esp_adc.hpp.

57 {
58 return (static_cast<float>(raw) / static_cast<float>(m_max_raw_)) *
59 m_reference_voltage_;
60 }

◆ Read()

float LibXR::ESP32ADC::Read ( )
inlineoverridevirtual

读取 ADC

Reads the ADC value

Returns
读取的模拟电压值(范围通常由硬件决定,例如 0-3.3V)
The read analog voltage value (range typically determined by hardware, e.g., 0-3.3V)

该方法为纯虚函数,子类必须实现此方法以提供具体的 ADC 读取功能。 This is a pure virtual function. Subclasses must implement this method to provide specific ADC reading functionality.

Implements LibXR::ADC.

Definition at line 45 of file esp_adc.hpp.

46 {
47 int raw = 0;
48 if (adc_oneshot_read(m_oneshot_, m_channel_, &raw) == ESP_OK)
49 {
50 return Normalize(raw);
51 }
52 return 0.0f;
53 }

Field Documentation

◆ m_attenuation_

adc_atten_t LibXR::ESP32ADC::m_attenuation_
private

Definition at line 64 of file esp_adc.hpp.

◆ m_bitwidth_

adc_bitwidth_t LibXR::ESP32ADC::m_bitwidth_
private

Definition at line 65 of file esp_adc.hpp.

◆ m_channel_

adc_channel_t LibXR::ESP32ADC::m_channel_
private

Definition at line 63 of file esp_adc.hpp.

◆ m_max_raw_

uint16_t LibXR::ESP32ADC::m_max_raw_
private

Definition at line 67 of file esp_adc.hpp.

◆ m_oneshot_

adc_oneshot_unit_handle_t LibXR::ESP32ADC::m_oneshot_ = nullptr
private

Definition at line 68 of file esp_adc.hpp.

◆ m_reference_voltage_

float LibXR::ESP32ADC::m_reference_voltage_
private

Definition at line 66 of file esp_adc.hpp.

◆ m_unit_

adc_unit_t LibXR::ESP32ADC::m_unit_
private

Definition at line 62 of file esp_adc.hpp.


The documentation for this class was generated from the following file: