forked from xiaozhi/xiaozhi-esp32
使用定时器调节屏幕亮度
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
#include <driver/ledc.h>
|
||||
#include <vector>
|
||||
#include <esp_lvgl_port.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
#include "board.h"
|
||||
|
||||
#define TAG "LcdDisplay"
|
||||
@@ -13,7 +15,6 @@
|
||||
|
||||
LV_FONT_DECLARE(font_awesome_30_4);
|
||||
|
||||
|
||||
LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
|
||||
gpio_num_t backlight_pin, bool backlight_output_invert,
|
||||
int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
|
||||
@@ -23,6 +24,18 @@ LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
|
||||
// 创建背光渐变定时器
|
||||
const esp_timer_create_args_t timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
LcdDisplay* display = static_cast<LcdDisplay*>(arg);
|
||||
display->OnBacklightTimer();
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "backlight_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &backlight_timer_));
|
||||
InitializeBacklight(backlight_pin);
|
||||
|
||||
// draw white
|
||||
@@ -79,12 +92,16 @@ LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_
|
||||
lv_display_set_offset(display_, offset_x, offset_y);
|
||||
}
|
||||
|
||||
SetBacklight(100);
|
||||
|
||||
SetupUI();
|
||||
|
||||
SetBacklight(brightness_);
|
||||
}
|
||||
|
||||
LcdDisplay::~LcdDisplay() {
|
||||
if (backlight_timer_ != nullptr) {
|
||||
esp_timer_stop(backlight_timer_);
|
||||
esp_timer_delete(backlight_timer_);
|
||||
}
|
||||
// 然后再清理 LVGL 对象
|
||||
if (content_ != nullptr) {
|
||||
lv_obj_del(content_);
|
||||
@@ -132,7 +149,7 @@ void LcdDisplay::InitializeBacklight(gpio_num_t backlight_pin) {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.duty_resolution = LEDC_TIMER_10_BIT,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.freq_hz = 20000,//背光pwm频率需要高一点,防止电感啸叫
|
||||
.freq_hz = 20000, //背光pwm频率需要高一点,防止电感啸叫
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
.deconfigure = false
|
||||
};
|
||||
@@ -141,6 +158,23 @@ void LcdDisplay::InitializeBacklight(gpio_num_t backlight_pin) {
|
||||
ESP_ERROR_CHECK(ledc_channel_config(&backlight_channel));
|
||||
}
|
||||
|
||||
void LcdDisplay::OnBacklightTimer() {
|
||||
if (current_brightness_ < brightness_) {
|
||||
current_brightness_++;
|
||||
} else if (current_brightness_ > brightness_) {
|
||||
current_brightness_--;
|
||||
}
|
||||
|
||||
// LEDC resolution set to 10bits, thus: 100% = 1023
|
||||
uint32_t duty_cycle = (1023 * current_brightness_) / 100;
|
||||
ledc_set_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH, duty_cycle);
|
||||
ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH);
|
||||
|
||||
if (current_brightness_ == brightness_) {
|
||||
esp_timer_stop(backlight_timer_);
|
||||
}
|
||||
}
|
||||
|
||||
void LcdDisplay::SetBacklight(uint8_t brightness) {
|
||||
if (backlight_pin_ == GPIO_NUM_NC) {
|
||||
return;
|
||||
@@ -151,10 +185,12 @@ void LcdDisplay::SetBacklight(uint8_t brightness) {
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Setting LCD backlight: %d%%", brightness);
|
||||
// LEDC resolution set to 10bits, thus: 100% = 1023
|
||||
uint32_t duty_cycle = (1023 * brightness) / 100;
|
||||
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH, duty_cycle));
|
||||
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH));
|
||||
// 停止现有的定时器(如果正在运行)
|
||||
esp_timer_stop(backlight_timer_);
|
||||
|
||||
Display::SetBacklight(brightness);
|
||||
// 启动定时器,每 5ms 更新一次
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(backlight_timer_, 5 * 1000));
|
||||
}
|
||||
|
||||
bool LcdDisplay::Lock(int timeout_ms) {
|
||||
|
||||
Reference in New Issue
Block a user