libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ESP32PWM Class Reference

ESP32 PWM 驱动实现 / ESP32 PWM driver implementation. More...

#include <esp_pwm.hpp>

Inheritance diagram for LibXR::ESP32PWM:
[legend]
Collaboration diagram for LibXR::ESP32PWM:
[legend]

Public Member Functions

 ESP32PWM (int gpio_num, ledc_channel_t channel, ledc_timer_t timer=LEDC_TIMER_0, ledc_timer_bit_t resolution=static_cast< ledc_timer_bit_t >((static_cast< uint8_t >(LEDC_TIMER_BIT_MAX) - 1)))
 构造 PWM 通道对象 / Construct PWM channel object
 
ErrorCode SetDutyCycle (float value) override
 Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。
 
ErrorCode SetConfig (Configuration config) override
 Configures the PWM settings. 配置 PWM 参数。
 
ErrorCode Enable () override
 Enables the PWM output. 启用 PWM 输出。
 
ErrorCode Disable () override
 Disables the PWM output. 禁用 PWM 输出。
 
- Public Member Functions inherited from LibXR::PWM

Private Attributes

int gpio_num_
 
ledc_channel_t channel_
 
ledc_timer_t timer_
 
ledc_timer_bit_t resolution_
 
uint32_t max_duty_
 

Detailed Description

ESP32 PWM 驱动实现 / ESP32 PWM driver implementation.

Definition at line 13 of file esp_pwm.hpp.

Constructor & Destructor Documentation

◆ ESP32PWM()

LibXR::ESP32PWM::ESP32PWM ( int gpio_num,
ledc_channel_t channel,
ledc_timer_t timer = LEDC_TIMER_0,
ledc_timer_bit_t resolution = static_cast<ledc_timer_bit_t>(               (static_cast<uint8_t>(LEDC_TIMER_BIT_MAX) - 1)) )
inline

构造 PWM 通道对象 / Construct PWM channel object

Parameters
gpio_numGPIO 编号 / GPIO number
channelLEDC 通道 / LEDC channel
timerLEDC 定时器 / LEDC timer
resolution占空比分辨率 / Duty resolution

Definition at line 24 of file esp_pwm.hpp.

27 : gpio_num_(gpio_num),
28 channel_(channel),
29 timer_(timer),
30 resolution_(resolution),
31 max_duty_((1 << resolution) - 1)
32 {
33 ledc_channel_config_t channel_conf = {};
34 channel_conf.gpio_num = gpio_num_;
35 channel_conf.speed_mode = static_cast<ledc_mode_t>(0);
36 channel_conf.channel = channel_;
37 channel_conf.intr_type = LEDC_INTR_DISABLE;
38 channel_conf.timer_sel = timer_;
39 channel_conf.duty = 0;
40 channel_conf.hpoint = 0;
41
42 auto err = ledc_channel_config(&channel_conf);
43 if (err != ESP_OK)
44 {
45 ASSERT(false);
46 };
47 }

Member Function Documentation

◆ Disable()

ErrorCode LibXR::ESP32PWM::Disable ( )
inlineoverridevirtual

Disables the PWM output. 禁用 PWM 输出。

Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 95 of file esp_pwm.hpp.

96 {
97 esp_err_t err = ledc_stop(static_cast<ledc_mode_t>(0), channel_, 0);
98 return (err == ESP_OK) ? ErrorCode::OK : ErrorCode::FAILED;
99 }

◆ Enable()

ErrorCode LibXR::ESP32PWM::Enable ( )
inlineoverridevirtual

Enables the PWM output. 启用 PWM 输出。

Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 89 of file esp_pwm.hpp.

90 {
91 esp_err_t err = ledc_update_duty(static_cast<ledc_mode_t>(0), channel_);
92 return (err == ESP_OK) ? ErrorCode::OK : ErrorCode::FAILED;
93 }

◆ SetConfig()

ErrorCode LibXR::ESP32PWM::SetConfig ( Configuration config)
inlineoverridevirtual

Configures the PWM settings. 配置 PWM 参数。

Parameters
configThe configuration structure containing PWM settings. 配置结构体,包含 PWM 设置。
Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 68 of file esp_pwm.hpp.

69 {
70 if (config.frequency == 0)
71 {
72 return ErrorCode::ARG_ERR;
73 }
74
75 ledc_timer_config_t timer_conf = {};
76 timer_conf.speed_mode = static_cast<ledc_mode_t>(0);
77 timer_conf.duty_resolution = resolution_;
78 timer_conf.timer_num = timer_;
79 timer_conf.freq_hz = config.frequency;
80 timer_conf.clk_cfg = LEDC_AUTO_CLK;
81
82 esp_err_t err = ledc_timer_config(&timer_conf);
83 if (err != ESP_OK) return ErrorCode::INIT_ERR;
84
85 max_duty_ = (1 << resolution_) - 1;
86 return ErrorCode::OK;
87 }

◆ SetDutyCycle()

ErrorCode LibXR::ESP32PWM::SetDutyCycle ( float value)
inlineoverridevirtual

Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。

Parameters
valueThe duty cycle as a floating-point value (0.0 to 1.0). 占空比,浮点值(0.0 到 1.0)。
Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::PWM.

Definition at line 49 of file esp_pwm.hpp.

50 {
51 if (value < 0.0f)
52 {
53 value = 0.0f;
54 }
55 else if (value > 1.0f)
56 {
57 value = 1.0f;
58 }
59
60 uint32_t duty = static_cast<uint32_t>(max_duty_ * value);
61 esp_err_t err = ledc_set_duty(static_cast<ledc_mode_t>(0), channel_, duty);
62 if (err != ESP_OK) return ErrorCode::FAILED;
63
64 err = ledc_update_duty(static_cast<ledc_mode_t>(0), channel_);
65 return (err == ESP_OK) ? ErrorCode::OK : ErrorCode::FAILED;
66 }

Field Documentation

◆ channel_

ledc_channel_t LibXR::ESP32PWM::channel_
private

Definition at line 103 of file esp_pwm.hpp.

◆ gpio_num_

int LibXR::ESP32PWM::gpio_num_
private

Definition at line 102 of file esp_pwm.hpp.

◆ max_duty_

uint32_t LibXR::ESP32PWM::max_duty_
private

Definition at line 106 of file esp_pwm.hpp.

◆ resolution_

ledc_timer_bit_t LibXR::ESP32PWM::resolution_
private

Definition at line 105 of file esp_pwm.hpp.

◆ timer_

ledc_timer_t LibXR::ESP32PWM::timer_
private

Definition at line 104 of file esp_pwm.hpp.


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