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

Public Member Functions

 ESP32DAC (uint8_t channel_id=0, float init_voltage=0.0f, float reference_voltage=3.3f)
 
ErrorCode Write (float voltage) override
 输出 DAC 电压
 
- Public Member Functions inherited from LibXR::DAC
 DAC ()=default
 默认构造函数
 

Private Attributes

uint8_t channel_id_
 
float reference_voltage_
 
bool ready_ = false
 

Detailed Description

Definition at line 19 of file esp_dac.hpp.

Constructor & Destructor Documentation

◆ ESP32DAC()

LibXR::ESP32DAC::ESP32DAC ( uint8_t channel_id = 0,
float init_voltage = 0.0f,
float reference_voltage = 3.3f )
inlineexplicit

Definition at line 22 of file esp_dac.hpp.

24 : channel_id_(channel_id), reference_voltage_(reference_voltage)
25 {
26#if SOC_DAC_SUPPORTED
27 if ((channel_id_ >= SOC_DAC_CHAN_NUM) || (reference_voltage_ <= 0.0f))
28 {
29 return;
30 }
31
32 ready_ = true;
33 dac_ll_power_on(ToChannel(channel_id_));
34 dac_ll_rtc_sync_by_adc(false);
35 (void)gpio_config_as_analog(
36 static_cast<gpio_num_t>(dac_periph_signal.dac_channel_io_num[channel_id_]));
37 (void)Write(init_voltage);
38#else
39 (void)init_voltage;
40#endif
41 }
ErrorCode Write(float voltage) override
输出 DAC 电压
Definition esp_dac.hpp:43

Member Function Documentation

◆ Write()

ErrorCode LibXR::ESP32DAC::Write ( float voltage)
inlineoverridevirtual

输出 DAC 电压

Outputs the DAC voltage

Parameters
voltage需要输出的模拟电压值
voltageThe analog voltage value to be output
Returns
错误码 ErrorCode

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

Implements LibXR::DAC.

Definition at line 43 of file esp_dac.hpp.

44 {
45#if SOC_DAC_SUPPORTED
46 if (!ready_)
47 {
49 }
50
51 if (voltage < 0.0f)
52 {
53 voltage = 0.0f;
54 }
55 else if (voltage > reference_voltage_)
56 {
57 voltage = reference_voltage_;
58 }
59
60 constexpr uint32_t MAX_CODE = (1U << SOC_DAC_RESOLUTION) - 1U;
61 const float scale = static_cast<float>(MAX_CODE) / reference_voltage_;
62 uint32_t code = static_cast<uint32_t>((voltage * scale) + 0.5f);
63 if (code > MAX_CODE)
64 {
65 code = MAX_CODE;
66 }
67
68 dac_ll_update_output_value(ToChannel(channel_id_), static_cast<uint8_t>(code));
69 return ErrorCode::OK;
70#else
71 (void)voltage;
73#endif
74 }
@ NOT_SUPPORT
不支持 | Not supported
@ OK
操作成功 | Operation successful

Field Documentation

◆ channel_id_

uint8_t LibXR::ESP32DAC::channel_id_
private

Definition at line 84 of file esp_dac.hpp.

◆ ready_

bool LibXR::ESP32DAC::ready_ = false
private

Definition at line 86 of file esp_dac.hpp.

◆ reference_voltage_

float LibXR::ESP32DAC::reference_voltage_
private

Definition at line 85 of file esp_dac.hpp.


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