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

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))
 Construct a 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_mode_t speed_mode_
 
ledc_hal_context_t hal_ {}
 
ledc_timer_bit_t resolution_
 
uint32_t max_duty_
 

Detailed Description

ESP32 PWM driver implementation.

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

Construct a PWM channel object.

Definition at line 18 of file esp_pwm.hpp.

21 : gpio_num_(gpio_num),
22 channel_(channel),
23 timer_(timer),
24 speed_mode_(LEDC_LOW_SPEED_MODE),
25 resolution_(resolution),
26 max_duty_((1U << static_cast<uint8_t>(resolution_)) - 1U)
27 {
28 ledc_hal_init(&hal_, speed_mode_);
29
30 ledc_channel_config_t channel_conf = {};
31 channel_conf.gpio_num = gpio_num_;
32 channel_conf.speed_mode = speed_mode_;
33 channel_conf.channel = channel_;
34 channel_conf.intr_type = LEDC_INTR_DISABLE;
35 channel_conf.timer_sel = timer_;
36 channel_conf.duty = 0;
37 channel_conf.hpoint = 0;
38
39 const esp_err_t err = ledc_channel_config(&channel_conf);
40 if (err != ESP_OK)
41 {
42 ASSERT(false);
43 }
44 }

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

104 {
105 ledc_hal_set_idle_level(&hal_, channel_, 0);
106 ledc_hal_set_sig_out_en(&hal_, channel_, false);
107 ledc_hal_ls_channel_update(&hal_, channel_);
108 return ErrorCode::OK;
109 }
@ OK
操作成功 | Operation successful

◆ Enable()

ErrorCode LibXR::ESP32PWM::Enable ( )
inlineoverridevirtual

Enables the PWM output. 启用 PWM 输出。

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

Implements LibXR::PWM.

Definition at line 96 of file esp_pwm.hpp.

97 {
98 ledc_hal_set_sig_out_en(&hal_, channel_, true);
99 ledc_hal_ls_channel_update(&hal_, channel_);
100 return ErrorCode::OK;
101 }

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

65 {
66 if (config.frequency == 0)
67 {
68 return ErrorCode::ARG_ERR;
69 }
70
71 for (int bits = static_cast<int>(resolution_);
72 bits >= static_cast<int>(LEDC_TIMER_1_BIT); --bits)
73 {
74 ledc_timer_config_t timer_conf = {};
75 timer_conf.speed_mode = speed_mode_;
76 timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(bits);
77 timer_conf.timer_num = timer_;
78 timer_conf.freq_hz = config.frequency;
79 timer_conf.clk_cfg = LEDC_AUTO_CLK;
80
81 if (ledc_timer_config(&timer_conf) != ESP_OK)
82 {
83 continue;
84 }
85
86 resolution_ = static_cast<ledc_timer_bit_t>(bits);
87 ledc_hal_bind_channel_timer(&hal_, channel_, timer_);
88 ledc_hal_ls_timer_update(&hal_, timer_);
89 max_duty_ = (1U << static_cast<uint8_t>(resolution_)) - 1U;
90 return ErrorCode::OK;
91 }
92
94 }
@ INIT_ERR
初始化错误 | Initialization error
@ ARG_ERR
参数错误 | Argument error

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

47 {
48 if (value < 0.0f)
49 {
50 value = 0.0f;
51 }
52 else if (value > 1.0f)
53 {
54 value = 1.0f;
55 }
56
57 const uint32_t duty = static_cast<uint32_t>(max_duty_ * value);
58 ledc_hal_set_duty_int_part(&hal_, channel_, duty);
59 ledc_hal_set_duty_start(&hal_, channel_);
60 ledc_hal_ls_channel_update(&hal_, channel_);
61 return ErrorCode::OK;
62 }

Field Documentation

◆ channel_

ledc_channel_t LibXR::ESP32PWM::channel_
private

Definition at line 113 of file esp_pwm.hpp.

◆ gpio_num_

int LibXR::ESP32PWM::gpio_num_
private

Definition at line 112 of file esp_pwm.hpp.

◆ hal_

ledc_hal_context_t LibXR::ESP32PWM::hal_ {}
private

Definition at line 116 of file esp_pwm.hpp.

116{};

◆ max_duty_

uint32_t LibXR::ESP32PWM::max_duty_
private

Definition at line 118 of file esp_pwm.hpp.

◆ resolution_

ledc_timer_bit_t LibXR::ESP32PWM::resolution_
private

Definition at line 117 of file esp_pwm.hpp.

◆ speed_mode_

ledc_mode_t LibXR::ESP32PWM::speed_mode_
private

Definition at line 115 of file esp_pwm.hpp.

◆ timer_

ledc_timer_t LibXR::ESP32PWM::timer_
private

Definition at line 114 of file esp_pwm.hpp.


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