forked from xiaozhi/xiaozhi-esp32
Add Backlight and PowerSaveTimer
This commit is contained in:
@@ -13,10 +13,6 @@
|
||||
#define TAG "Display"
|
||||
|
||||
Display::Display() {
|
||||
// Load brightness from settings
|
||||
Settings settings("display");
|
||||
brightness_ = settings.GetInt("brightness", 100);
|
||||
|
||||
// Notification timer
|
||||
esp_timer_create_args_t notification_timer_args = {
|
||||
.callback = [](void *arg) {
|
||||
@@ -40,14 +36,14 @@ Display::Display() {
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "update_display_timer",
|
||||
.name = "display_update_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&update_display_timer_args, &update_timer_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(update_timer_, 1000000));
|
||||
|
||||
// Create a power management lock
|
||||
auto ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "ml307", &pm_lock_);
|
||||
auto ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "display_update", &pm_lock_);
|
||||
if (ret == ESP_ERR_NOT_SUPPORTED) {
|
||||
ESP_LOGI(TAG, "Power management not supported");
|
||||
} else {
|
||||
@@ -236,9 +232,3 @@ void Display::SetChatMessage(const char* role, const char* content) {
|
||||
}
|
||||
lv_label_set_text(chat_message_label_, content);
|
||||
}
|
||||
|
||||
void Display::SetBacklight(uint8_t brightness) {
|
||||
Settings settings("display", true);
|
||||
settings.SetInt("brightness", brightness);
|
||||
brightness_ = brightness;
|
||||
}
|
||||
|
||||
@@ -25,17 +25,13 @@ public:
|
||||
virtual void SetEmotion(const char* emotion);
|
||||
virtual void SetChatMessage(const char* role, const char* content);
|
||||
virtual void SetIcon(const char* icon);
|
||||
virtual void SetBacklight(uint8_t brightness);
|
||||
|
||||
inline int width() const { return width_; }
|
||||
inline int height() const { return height_; }
|
||||
inline uint8_t brightness() const { return brightness_; }
|
||||
|
||||
protected:
|
||||
int width_ = 0;
|
||||
int height_ = 0;
|
||||
uint8_t brightness_ = 0;
|
||||
|
||||
|
||||
esp_pm_lock_handle_t pm_lock_ = nullptr;
|
||||
lv_display_t *display_ = nullptr;
|
||||
|
||||
@@ -1,43 +1,25 @@
|
||||
#include "lcd_display.h"
|
||||
|
||||
#include <vector>
|
||||
#include <font_awesome_symbols.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_err.h>
|
||||
#include <driver/ledc.h>
|
||||
#include <vector>
|
||||
#include <esp_lvgl_port.h>
|
||||
#include <esp_timer.h>
|
||||
#include "assets/lang_config.h"
|
||||
|
||||
#include "board.h"
|
||||
|
||||
#define TAG "LcdDisplay"
|
||||
#define LCD_LEDC_CH LEDC_CHANNEL_0
|
||||
|
||||
LV_FONT_DECLARE(font_awesome_30_4);
|
||||
|
||||
SpiLcdDisplay::SpiLcdDisplay(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,
|
||||
DisplayFonts fonts)
|
||||
: LcdDisplay(panel_io, panel, backlight_pin, backlight_output_invert, fonts) {
|
||||
: LcdDisplay(panel_io, panel, fonts) {
|
||||
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
|
||||
std::vector<uint16_t> buffer(width_, 0xFFFF);
|
||||
for (int y = 0; y < height_; y++) {
|
||||
@@ -53,6 +35,7 @@ SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
|
||||
ESP_LOGI(TAG, "Initialize LVGL port");
|
||||
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
||||
port_cfg.task_priority = 1;
|
||||
lvgl_port_init(&port_cfg);
|
||||
|
||||
ESP_LOGI(TAG, "Adding LCD screen");
|
||||
@@ -93,33 +76,16 @@ SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
}
|
||||
|
||||
SetupUI();
|
||||
|
||||
SetBacklight(brightness_);
|
||||
}
|
||||
|
||||
// RGB LCD实现
|
||||
RgbLcdDisplay::RgbLcdDisplay(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,
|
||||
DisplayFonts fonts)
|
||||
: LcdDisplay(panel_io, panel, backlight_pin, backlight_output_invert, fonts) {
|
||||
: LcdDisplay(panel_io, panel, fonts) {
|
||||
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
|
||||
std::vector<uint16_t> buffer(width_, 0xFFFF);
|
||||
@@ -132,6 +98,7 @@ RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
|
||||
ESP_LOGI(TAG, "Initialize LVGL port");
|
||||
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
||||
port_cfg.task_priority = 1;
|
||||
lvgl_port_init(&port_cfg);
|
||||
|
||||
ESP_LOGI(TAG, "Adding LCD screen");
|
||||
@@ -173,15 +140,9 @@ RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
}
|
||||
|
||||
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_);
|
||||
@@ -207,72 +168,6 @@ LcdDisplay::~LcdDisplay() {
|
||||
}
|
||||
}
|
||||
|
||||
void LcdDisplay::InitializeBacklight(gpio_num_t backlight_pin) {
|
||||
if (backlight_pin == GPIO_NUM_NC) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup LEDC peripheral for PWM backlight control
|
||||
const ledc_channel_config_t backlight_channel = {
|
||||
.gpio_num = backlight_pin,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.channel = LCD_LEDC_CH,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.duty = 0,
|
||||
.hpoint = 0,
|
||||
.flags = {
|
||||
.output_invert = backlight_output_invert_,
|
||||
}
|
||||
};
|
||||
const ledc_timer_config_t backlight_timer = {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.duty_resolution = LEDC_TIMER_10_BIT,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.freq_hz = 20000, //背光pwm频率需要高一点,防止电感啸叫
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
.deconfigure = false
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(ledc_timer_config(&backlight_timer));
|
||||
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;
|
||||
}
|
||||
|
||||
if (brightness > 100) {
|
||||
brightness = 100;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Setting LCD backlight: %d%%", brightness);
|
||||
// 停止现有的定时器(如果正在运行)
|
||||
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) {
|
||||
return lvgl_port_lock(timeout_ms);
|
||||
}
|
||||
|
||||
@@ -3,13 +3,8 @@
|
||||
|
||||
#include "display.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
#include <esp_timer.h>
|
||||
#include <font_emoji.h>
|
||||
|
||||
#include <atomic>
|
||||
@@ -18,8 +13,6 @@ class LcdDisplay : public Display {
|
||||
protected:
|
||||
esp_lcd_panel_io_handle_t panel_io_ = nullptr;
|
||||
esp_lcd_panel_handle_t panel_ = nullptr;
|
||||
gpio_num_t backlight_pin_ = GPIO_NUM_NC;
|
||||
bool backlight_output_invert_ = false;
|
||||
|
||||
lv_draw_buf_t draw_buf_;
|
||||
lv_obj_t* status_bar_ = nullptr;
|
||||
@@ -29,36 +22,25 @@ protected:
|
||||
|
||||
DisplayFonts fonts_;
|
||||
|
||||
esp_timer_handle_t backlight_timer_ = nullptr;
|
||||
uint8_t current_brightness_ = 0;
|
||||
void InitializeBacklight(gpio_num_t backlight_pin);
|
||||
|
||||
virtual void SetupUI();
|
||||
virtual bool Lock(int timeout_ms = 0) override;
|
||||
virtual void Unlock() override;
|
||||
|
||||
protected:
|
||||
// 添加protected构造函数
|
||||
LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
|
||||
gpio_num_t backlight_pin, bool backlight_output_invert,
|
||||
DisplayFonts fonts)
|
||||
: panel_io_(panel_io), panel_(panel),
|
||||
backlight_pin_(backlight_pin), backlight_output_invert_(backlight_output_invert),
|
||||
fonts_(fonts) {}
|
||||
LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, DisplayFonts fonts)
|
||||
: panel_io_(panel_io), panel_(panel), fonts_(fonts) {}
|
||||
|
||||
public:
|
||||
~LcdDisplay();
|
||||
virtual void OnBacklightTimer();
|
||||
virtual void SetEmotion(const char* emotion) override;
|
||||
virtual void SetIcon(const char* icon) override;
|
||||
virtual void SetBacklight(uint8_t brightness) override;
|
||||
};
|
||||
|
||||
// RGB LCD显示器
|
||||
class RgbLcdDisplay : public LcdDisplay {
|
||||
public:
|
||||
RgbLcdDisplay(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,
|
||||
DisplayFonts fonts);
|
||||
@@ -68,7 +50,6 @@ public:
|
||||
class MipiLcdDisplay : public LcdDisplay {
|
||||
public:
|
||||
MipiLcdDisplay(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,
|
||||
DisplayFonts fonts);
|
||||
@@ -78,7 +59,6 @@ public:
|
||||
class SpiLcdDisplay : public LcdDisplay {
|
||||
public:
|
||||
SpiLcdDisplay(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,
|
||||
DisplayFonts fonts);
|
||||
@@ -88,7 +68,6 @@ public:
|
||||
class QspiLcdDisplay : public LcdDisplay {
|
||||
public:
|
||||
QspiLcdDisplay(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,
|
||||
DisplayFonts fonts);
|
||||
@@ -98,7 +77,6 @@ public:
|
||||
class Mcu8080LcdDisplay : public LcdDisplay {
|
||||
public:
|
||||
Mcu8080LcdDisplay(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,
|
||||
DisplayFonts fonts);
|
||||
|
||||
Reference in New Issue
Block a user