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
10class ESP32PWM : public PWM
11{
12 public:
13 ESP32PWM(int gpio_num, ledc_channel_t channel, ledc_timer_t timer = LEDC_TIMER_0,
14 ledc_timer_bit_t resolution = static_cast<ledc_timer_bit_t>(
15 (static_cast<uint8_t>(LEDC_TIMER_BIT_MAX) - 1)))
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 }
37
38 ErrorCode SetDutyCycle(float value) override
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 }
56
57 ErrorCode SetConfig(Configuration config) override
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 }
77
78 ErrorCode Enable() override
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 }
83
84 ErrorCode Disable() override
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 }
89
90 private:
91 int gpio_num_;
92 ledc_channel_t channel_;
93 ledc_timer_t timer_;
94 ledc_timer_bit_t resolution_;
95 uint32_t max_duty_;
96};
97
98} // namespace LibXR
ErrorCode Enable() override
Enables the PWM output. 启用 PWM 输出。
Definition esp_pwm.hpp:78
ErrorCode SetConfig(Configuration config) override
Configures the PWM settings. 配置 PWM 参数。
Definition esp_pwm.hpp:57
ErrorCode SetDutyCycle(float value) override
Sets the duty cycle of the PWM signal. 设置 PWM 信号的占空比。
Definition esp_pwm.hpp:38
ErrorCode Disable() override
Disables the PWM output. 禁用 PWM 输出。
Definition esp_pwm.hpp:84
Abstract base class for PWM (Pulse Width Modulation) control. PWM(脉冲宽度调制)控制的抽象基类。
Definition pwm.hpp:14
LibXR 命名空间
Definition ch32_gpio.hpp:9
Configuration parameters for PWM. PWM 配置参数。
Definition pwm.hpp:23
uint32_t frequency
PWM signal frequency in Hz. PWM 信号的频率(Hz)。
Definition pwm.hpp:24