libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_pwm.hpp
1#pragma once
2
3#include "driver/ledc.h"
4#include "esp_err.h"
5#include "pwm.hpp"
6
7namespace LibXR
8{
9
13class ESP32PWM : public PWM
14{
15 public:
24 ESP32PWM(int gpio_num, ledc_channel_t channel, ledc_timer_t timer = LEDC_TIMER_0,
25 ledc_timer_bit_t resolution = static_cast<ledc_timer_bit_t>(
26 (static_cast<uint8_t>(LEDC_TIMER_BIT_MAX) - 1)))
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 }
48
49 ErrorCode SetDutyCycle(float value) override
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 }
67
68 ErrorCode SetConfig(Configuration config) override
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 }
88
89 ErrorCode Enable() override
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 }
94
95 ErrorCode Disable() override
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 }
100
101 private:
102 int gpio_num_;
103 ledc_channel_t channel_;
104 ledc_timer_t timer_;
105 ledc_timer_bit_t resolution_;
106 uint32_t max_duty_;
107};
108
109} // namespace LibXR
ESP32 PWM 驱动实现 / ESP32 PWM driver implementation.
Definition esp_pwm.hpp:14
ErrorCode Enable() override
Enables the PWM output. 启用 PWM 输出。
Definition esp_pwm.hpp:89
ErrorCode SetConfig(Configuration config) override
Configures the PWM settings. 配置 PWM 参数。
Definition esp_pwm.hpp:68
ErrorCode SetDutyCycle(float value) override
Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。
Definition esp_pwm.hpp:49
ErrorCode Disable() override
Disables the PWM output. 禁用 PWM 输出。
Definition esp_pwm.hpp:95
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
Definition esp_pwm.hpp:24
Abstract base class for PWM (Pulse Width Modulation) control. PWM(脉冲宽度调制)控制的抽象基类。
Definition pwm.hpp:14
LibXR 命名空间
Definition ch32_can.hpp:14
Configuration parameters for PWM. PWM 配置参数。
Definition pwm.hpp:23
uint32_t frequency
PWM signal frequency in Hz. PWM 信号的频率(Hz)。
Definition pwm.hpp:24