libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ESP32PWM Class Reference
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)))
 
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

Definition at line 10 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

Definition at line 13 of file esp_pwm.hpp.

16 : gpio_num_(gpio_num),
17 channel_(channel),
18 timer_(timer),
19 resolution_(resolution),
20 max_duty_((1 << resolution) - 1)
21 {
22 ledc_channel_config_t channel_conf = {};
23 channel_conf.gpio_num = gpio_num_;
24 channel_conf.speed_mode = static_cast<ledc_mode_t>(0);
25 channel_conf.channel = channel_;
26 channel_conf.intr_type = LEDC_INTR_DISABLE;
27 channel_conf.timer_sel = timer_;
28 channel_conf.duty = 0;
29 channel_conf.hpoint = 0;
30
31 auto err = ledc_channel_config(&channel_conf);
32 if (err != ESP_OK)
33 {
34 ASSERT(false);
35 };
36 }

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 84 of file esp_pwm.hpp.

85 {
86 esp_err_t err = ledc_stop(static_cast<ledc_mode_t>(0), channel_, 0);
87 return (err == ESP_OK) ? ErrorCode::OK : ErrorCode::FAILED;
88 }

◆ Enable()

ErrorCode LibXR::ESP32PWM::Enable ( )
inlineoverridevirtual

Enables the PWM output. 启用 PWM 输出。

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

Implements LibXR::PWM.

Definition at line 78 of file esp_pwm.hpp.

79 {
80 esp_err_t err = ledc_update_duty(static_cast<ledc_mode_t>(0), channel_);
81 return (err == ESP_OK) ? ErrorCode::OK : ErrorCode::FAILED;
82 }

◆ 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 57 of file esp_pwm.hpp.

58 {
59 if (config.frequency == 0)
60 {
61 return ErrorCode::ARG_ERR;
62 }
63
64 ledc_timer_config_t timer_conf = {};
65 timer_conf.speed_mode = static_cast<ledc_mode_t>(0);
66 timer_conf.duty_resolution = resolution_;
67 timer_conf.timer_num = timer_;
68 timer_conf.freq_hz = config.frequency;
69 timer_conf.clk_cfg = LEDC_AUTO_CLK;
70
71 esp_err_t err = ledc_timer_config(&timer_conf);
72 if (err != ESP_OK) return ErrorCode::INIT_ERR;
73
74 max_duty_ = (1 << resolution_) - 1;
75 return ErrorCode::OK;
76 }

◆ 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 38 of file esp_pwm.hpp.

39 {
40 if (value < 0.0f)
41 {
42 value = 0.0f;
43 }
44 else if (value > 1.0f)
45 {
46 value = 1.0f;
47 }
48
49 uint32_t duty = static_cast<uint32_t>(max_duty_ * value);
50 esp_err_t err = ledc_set_duty(static_cast<ledc_mode_t>(0), channel_, duty);
51 if (err != ESP_OK) return ErrorCode::FAILED;
52
53 err = ledc_update_duty(static_cast<ledc_mode_t>(0), channel_);
54 return (err == ESP_OK) ? ErrorCode::OK : ErrorCode::FAILED;
55 }

Field Documentation

◆ channel_

ledc_channel_t LibXR::ESP32PWM::channel_
private

Definition at line 92 of file esp_pwm.hpp.

◆ gpio_num_

int LibXR::ESP32PWM::gpio_num_
private

Definition at line 91 of file esp_pwm.hpp.

◆ max_duty_

uint32_t LibXR::ESP32PWM::max_duty_
private

Definition at line 95 of file esp_pwm.hpp.

◆ resolution_

ledc_timer_bit_t LibXR::ESP32PWM::resolution_
private

Definition at line 94 of file esp_pwm.hpp.

◆ timer_

ledc_timer_t LibXR::ESP32PWM::timer_
private

Definition at line 93 of file esp_pwm.hpp.


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