Files
xiaozhi-esp32/main/display/lcd_display.cc

382 lines
13 KiB
C++
Raw Normal View History

2025-01-05 21:20:30 +08:00
#include "lcd_display.h"
2024-11-18 06:17:39 +08:00
#include "font_awesome_symbols.h"
2024-11-06 10:06:05 +08:00
#include <esp_log.h>
#include <esp_err.h>
#include <driver/ledc.h>
#include <vector>
2025-01-23 13:50:22 +08:00
#include <emoji_font.h>
#include "board.h"
2024-11-06 10:06:05 +08:00
2025-01-05 21:20:30 +08:00
#define TAG "LcdDisplay"
2024-11-06 10:06:05 +08:00
#define LCD_LEDC_CH LEDC_CHANNEL_0
2025-01-05 21:20:30 +08:00
#define LCD_LVGL_TICK_PERIOD_MS 2
2025-01-23 13:50:22 +08:00
#define LCD_LVGL_TASK_MAX_DELAY_MS 60
2025-01-05 21:20:30 +08:00
#define LCD_LVGL_TASK_MIN_DELAY_MS 1
#define LCD_LVGL_TASK_STACK_SIZE (4 * 1024)
2025-01-23 13:50:22 +08:00
#define LCD_LVGL_TASK_PRIORITY 1
LV_FONT_DECLARE(font_awesome_30_4);
2024-11-30 19:00:05 +08:00
2024-11-18 06:17:39 +08:00
2024-11-30 19:00:05 +08:00
static lv_disp_drv_t disp_drv;
2025-01-05 21:20:30 +08:00
static void lcd_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
2024-11-30 19:00:05 +08:00
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
int offsetx1 = area->x1;
int offsetx2 = area->x2;
int offsety1 = area->y1;
int offsety2 = area->y2;
// copy a buffer's content to a specific area of the display
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
lv_disp_flush_ready(&disp_drv);
}
/* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */
2025-01-05 21:20:30 +08:00
static void lcd_lvgl_port_update_callback(lv_disp_drv_t *drv)
2024-11-30 19:00:05 +08:00
{
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
switch (drv->rotated)
{
case LV_DISP_ROT_NONE:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, true, false);
break;
case LV_DISP_ROT_90:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, true, true);
break;
case LV_DISP_ROT_180:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, false, true);
break;
case LV_DISP_ROT_270:
// Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, false, false);
break;
}
}
2025-01-05 21:20:30 +08:00
void LcdDisplay::LvglTask() {
2024-11-30 19:00:05 +08:00
ESP_LOGI(TAG, "Starting LVGL task");
2025-01-05 21:20:30 +08:00
uint32_t task_delay_ms = LCD_LVGL_TASK_MAX_DELAY_MS;
2024-11-30 19:00:05 +08:00
while (1)
{
// Lock the mutex due to the LVGL APIs are not thread-safe
2024-12-03 12:33:33 +08:00
if (Lock())
2024-11-30 19:00:05 +08:00
{
task_delay_ms = lv_timer_handler();
2024-12-03 12:33:33 +08:00
Unlock();
2024-11-30 19:00:05 +08:00
}
2025-01-05 21:20:30 +08:00
if (task_delay_ms > LCD_LVGL_TASK_MAX_DELAY_MS)
2024-11-30 19:00:05 +08:00
{
2025-01-05 21:20:30 +08:00
task_delay_ms = LCD_LVGL_TASK_MAX_DELAY_MS;
2024-11-30 19:00:05 +08:00
}
2025-01-05 21:20:30 +08:00
else if (task_delay_ms < LCD_LVGL_TASK_MIN_DELAY_MS)
2024-11-30 19:00:05 +08:00
{
2025-01-05 21:20:30 +08:00
task_delay_ms = LCD_LVGL_TASK_MIN_DELAY_MS;
2024-11-30 19:00:05 +08:00
}
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
}
2025-01-05 21:20:30 +08:00
LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
2024-11-16 03:25:55 +08:00
gpio_num_t backlight_pin, bool backlight_output_invert,
2025-01-23 13:50:22 +08:00
int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
const lv_font_t* text_font, const lv_font_t* icon_font)
2024-11-16 03:25:55 +08:00
: panel_io_(panel_io), panel_(panel), backlight_pin_(backlight_pin), backlight_output_invert_(backlight_output_invert),
2025-01-23 13:50:22 +08:00
mirror_x_(mirror_x), mirror_y_(mirror_y), swap_xy_(swap_xy),
text_font_(text_font), icon_font_(icon_font) {
2024-11-06 10:06:05 +08:00
width_ = width;
height_ = height;
2024-11-30 19:00:05 +08:00
offset_x_ = offset_x;
offset_y_ = offset_y;
2024-11-06 10:06:05 +08:00
2024-11-30 19:00:05 +08:00
emoji_font_init();
2024-11-06 10:06:05 +08:00
InitializeBacklight(backlight_pin);
// draw white
std::vector<uint16_t> buffer(width_, 0xFFFF);
for (int y = 0; y < height_; y++) {
esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
}
// Set the display to on
ESP_LOGI(TAG, "Turning display on");
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
2024-11-30 19:00:05 +08:00
ESP_LOGI(TAG, "Initialize LVGL library");
lv_init();
// alloc draw buffers used by LVGL
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
// it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
lv_color_t *buf1 = (lv_color_t *)heap_caps_malloc(width_ * 10 * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf1);
lv_color_t *buf2 = (lv_color_t *)heap_caps_malloc(width_ * 10 * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf2);
// initialize LVGL draw buffers
lv_disp_draw_buf_init(&disp_buf, buf1, buf2, width_ * 10);
ESP_LOGI(TAG, "Register display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = width_;
disp_drv.ver_res = height_;
disp_drv.offset_x = offset_x_;
disp_drv.offset_y = offset_y_;
2025-01-05 21:20:30 +08:00
disp_drv.flush_cb = lcd_lvgl_flush_cb;
disp_drv.drv_update_cb = lcd_lvgl_port_update_callback;
2024-11-30 19:00:05 +08:00
disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_;
2024-12-03 12:33:33 +08:00
lv_disp_drv_register(&disp_drv);
2024-11-30 19:00:05 +08:00
ESP_LOGI(TAG, "Install LVGL tick timer");
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
2024-12-03 12:33:33 +08:00
.callback = [](void* arg) {
2025-01-05 21:20:30 +08:00
lv_tick_inc(LCD_LVGL_TICK_PERIOD_MS);
2024-12-03 12:33:33 +08:00
},
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "LVGL Tick Timer",
.skip_unhandled_events = false
};
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer_));
2025-01-05 21:20:30 +08:00
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer_, LCD_LVGL_TICK_PERIOD_MS * 1000));
2024-12-03 12:33:33 +08:00
lvgl_mutex_ = xSemaphoreCreateRecursiveMutex();
assert(lvgl_mutex_ != nullptr);
2024-11-30 19:00:05 +08:00
ESP_LOGI(TAG, "Create LVGL task");
2024-12-03 12:33:33 +08:00
xTaskCreate([](void *arg) {
2025-01-05 21:20:30 +08:00
static_cast<LcdDisplay*>(arg)->LvglTask();
2024-12-03 12:33:33 +08:00
vTaskDelete(NULL);
2025-01-05 21:20:30 +08:00
}, "LVGL", LCD_LVGL_TASK_STACK_SIZE, this, LCD_LVGL_TASK_PRIORITY, NULL);
2024-11-06 10:06:05 +08:00
SetBacklight(100);
2024-11-18 06:17:39 +08:00
SetupUI();
2024-11-06 10:06:05 +08:00
}
2025-01-05 21:20:30 +08:00
LcdDisplay::~LcdDisplay() {
2024-12-03 12:33:33 +08:00
ESP_ERROR_CHECK(esp_timer_stop(lvgl_tick_timer_));
ESP_ERROR_CHECK(esp_timer_delete(lvgl_tick_timer_));
2024-11-18 06:17:39 +08:00
if (content_ != nullptr) {
lv_obj_del(content_);
}
if (status_bar_ != nullptr) {
lv_obj_del(status_bar_);
}
if (side_bar_ != nullptr) {
lv_obj_del(side_bar_);
}
if (container_ != nullptr) {
lv_obj_del(container_);
}
2024-11-06 10:06:05 +08:00
if (panel_ != nullptr) {
esp_lcd_panel_del(panel_);
}
if (panel_io_ != nullptr) {
esp_lcd_panel_io_del(panel_io_);
}
2024-12-03 12:33:33 +08:00
vSemaphoreDelete(lvgl_mutex_);
2024-11-06 10:06:05 +08:00
}
2025-01-05 21:20:30 +08:00
void LcdDisplay::InitializeBacklight(gpio_num_t backlight_pin) {
2024-11-06 10:06:05 +08:00
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 = {
2024-11-16 03:25:55 +08:00
.output_invert = backlight_output_invert_,
2024-11-06 10:06:05 +08:00
}
};
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 = 5000,
.clk_cfg = LEDC_AUTO_CLK,
.deconfigure = false
};
ESP_ERROR_CHECK(ledc_timer_config(&backlight_timer));
ESP_ERROR_CHECK(ledc_channel_config(&backlight_channel));
}
2025-01-05 21:20:30 +08:00
void LcdDisplay::SetBacklight(uint8_t brightness) {
2024-11-16 03:25:55 +08:00
if (backlight_pin_ == GPIO_NUM_NC) {
return;
}
2024-11-06 10:06:05 +08:00
if (brightness > 100) {
brightness = 100;
}
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));
}
2025-01-05 21:20:30 +08:00
bool LcdDisplay::Lock(int timeout_ms) {
2024-12-03 12:33:33 +08:00
// Convert timeout in milliseconds to FreeRTOS ticks
// If `timeout_ms` is set to 0, the program will block until the condition is met
const TickType_t timeout_ticks = (timeout_ms == 0) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTakeRecursive(lvgl_mutex_, timeout_ticks) == pdTRUE;
2024-11-06 10:06:05 +08:00
}
2025-01-05 21:20:30 +08:00
void LcdDisplay::Unlock() {
2024-12-03 12:33:33 +08:00
xSemaphoreGiveRecursive(lvgl_mutex_);
2024-11-06 10:06:05 +08:00
}
2024-11-18 06:17:39 +08:00
2025-01-05 21:20:30 +08:00
void LcdDisplay::SetupUI() {
2024-11-18 06:17:39 +08:00
DisplayLockGuard lock(this);
2024-11-30 19:00:05 +08:00
auto screen = lv_disp_get_scr_act(lv_disp_get_default());
2025-01-23 13:50:22 +08:00
lv_obj_set_style_text_font(screen, text_font_, 0);
2024-11-18 06:17:39 +08:00
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
/* Container */
container_ = lv_obj_create(screen);
lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_all(container_, 0, 0);
lv_obj_set_style_border_width(container_, 0, 0);
lv_obj_set_style_pad_row(container_, 0, 0);
/* Status bar */
status_bar_ = lv_obj_create(container_);
2025-01-23 13:50:22 +08:00
lv_obj_set_size(status_bar_, LV_HOR_RES, text_font_->line_height);
2024-11-18 06:17:39 +08:00
lv_obj_set_style_radius(status_bar_, 0, 0);
/* Content */
content_ = lv_obj_create(container_);
lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_style_radius(content_, 0, 0);
lv_obj_set_width(content_, LV_HOR_RES);
lv_obj_set_flex_grow(content_, 1);
2024-12-26 12:24:53 +08:00
lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN); // 垂直布局(从上到下)
lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY); // 子对象居中对齐,等距分布
2024-11-18 06:17:39 +08:00
emotion_label_ = lv_label_create(content_);
2025-01-23 13:50:22 +08:00
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
2024-11-18 06:17:39 +08:00
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
2024-12-26 12:24:53 +08:00
// lv_obj_center(emotion_label_);
chat_message_label_ = lv_label_create(content_);
lv_label_set_text(chat_message_label_, "");
2025-01-23 13:50:22 +08:00
lv_obj_set_width(chat_message_label_, LV_HOR_RES * 0.9); // 限制宽度为屏幕宽度的 90%
2024-12-26 12:24:53 +08:00
lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_WRAP); // 设置为自动换行模式
lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_CENTER, 0); // 设置文本居中对齐
2024-11-18 06:17:39 +08:00
/* Status bar */
lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_all(status_bar_, 0, 0);
lv_obj_set_style_border_width(status_bar_, 0, 0);
lv_obj_set_style_pad_column(status_bar_, 0, 0);
2025-01-23 13:50:22 +08:00
lv_obj_set_style_pad_left(status_bar_, 2, 0);
lv_obj_set_style_pad_right(status_bar_, 2, 0);
2024-11-18 06:17:39 +08:00
network_label_ = lv_label_create(status_bar_);
lv_label_set_text(network_label_, "");
2025-01-23 13:50:22 +08:00
lv_obj_set_style_text_font(network_label_, icon_font_, 0);
2024-11-18 06:17:39 +08:00
notification_label_ = lv_label_create(status_bar_);
lv_obj_set_flex_grow(notification_label_, 1);
lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text(notification_label_, "通知");
lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
status_label_ = lv_label_create(status_bar_);
lv_obj_set_flex_grow(status_label_, 1);
2025-01-06 01:26:39 +08:00
lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
2024-11-18 06:17:39 +08:00
lv_label_set_text(status_label_, "正在初始化");
lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
mute_label_ = lv_label_create(status_bar_);
lv_label_set_text(mute_label_, "");
2025-01-23 13:50:22 +08:00
lv_obj_set_style_text_font(mute_label_, icon_font_, 0);
2024-11-18 06:17:39 +08:00
battery_label_ = lv_label_create(status_bar_);
lv_label_set_text(battery_label_, "");
2025-01-23 13:50:22 +08:00
lv_obj_set_style_text_font(battery_label_, icon_font_, 0);
2024-11-18 06:17:39 +08:00
}
2024-12-26 12:24:53 +08:00
2025-01-05 21:20:30 +08:00
void LcdDisplay::SetChatMessage(const std::string &role, const std::string &content) {
2024-12-26 12:24:53 +08:00
if (chat_message_label_ == nullptr) {
return;
}
lv_label_set_text(chat_message_label_, content.c_str());
}
void LcdDisplay::SetEmotion(const std::string &emotion) {
if (emotion_label_ == nullptr) {
return;
}
struct Emotion {
const char* icon;
const char* text;
};
static const std::vector<Emotion> emotions = {
{"😶", "neutral"},
{"😊", "happy"},
{"😆", "laughing"},
{"😂", "funny"},
{"😔", "sad"},
{"😠", "angry"},
{"😭", "crying"},
{"😍", "loving"},
{"😳", "embarrassed"},
{"😲", "surprised"},
{"😨", "shocked"},
{"🤔", "thinking"},
{"😉", "winking"},
{"😎", "cool"},
{"😌", "relaxed"},
{"😋", "delicious"},
{"😘", "kissy"},
{"😏", "confident"},
{"😴", "sleepy"},
{"🤪", "silly"},
{"😕", "confused"}
};
DisplayLockGuard lock(this);
// 查找匹配的表情
auto it = std::find_if(emotions.begin(), emotions.end(),
[&emotion](const Emotion& e) { return e.text == emotion; });
// 如果找到匹配的表情就显示对应图标否则显示默认的neutral表情
lv_obj_set_style_text_font(emotion_label_, emoji_font, 0);
if (it != emotions.end()) {
lv_label_set_text(emotion_label_, it->icon);
} else {
lv_label_set_text(emotion_label_, FONT_AWESOME_EMOJI_NEUTRAL);
}
}