rename St7789 to LCD

This commit is contained in:
Terrence 2025-01-05 21:20:30 +08:00
parent c7c5b74d37
commit b94c8a6e8b
12 changed files with 105 additions and 130 deletions

View File

@ -4,7 +4,7 @@
# CMakeLists in this exact order for cmake to work correctly # CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
set(PROJECT_VER "0.9.7") set(PROJECT_VER "0.9.8")
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(xiaozhi) project(xiaozhi)

View File

@ -7,7 +7,7 @@ set(SOURCES "audio_codecs/audio_codec.cc"
"led/circular_strip.cc" "led/circular_strip.cc"
"display/display.cc" "display/display.cc"
"display/no_display.cc" "display/no_display.cc"
"display/st7789_display.cc" "display/lcd_display.cc"
"display/ssd1306_display.cc" "display/ssd1306_display.cc"
"protocols/protocol.cc" "protocols/protocol.cc"
"protocols/mqtt_protocol.cc" "protocols/mqtt_protocol.cc"

View File

@ -548,6 +548,7 @@ void Application::SetDeviceState(DeviceState state) {
case kDeviceStateIdle: case kDeviceStateIdle:
display->SetStatus("待命"); display->SetStatus("待命");
display->SetEmotion("neutral"); display->SetEmotion("neutral");
display->SetChatMessage("", "");
#ifdef CONFIG_IDF_TARGET_ESP32S3 #ifdef CONFIG_IDF_TARGET_ESP32S3
audio_processor_.Stop(); audio_processor_.Stop();
#endif #endif

View File

@ -77,8 +77,8 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
i2s_chan_config_t chan_cfg = { i2s_chan_config_t chan_cfg = {
.id = (i2s_port_t)0, .id = (i2s_port_t)0,
.role = I2S_ROLE_MASTER, .role = I2S_ROLE_MASTER,
.dma_desc_num = 6, .dma_desc_num = 2,
.dma_frame_num = 240, .dma_frame_num = 240 * 3,
.auto_clear_after_cb = true, .auto_clear_after_cb = true,
.auto_clear_before_cb = false, .auto_clear_before_cb = false,
.intr_priority = 0, .intr_priority = 0,
@ -138,8 +138,8 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
// Create a new channel for speaker // Create a new channel for speaker
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)1, I2S_ROLE_MASTER); i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)1, I2S_ROLE_MASTER);
tx_chan_cfg.dma_desc_num = 6; tx_chan_cfg.dma_desc_num = 2;
tx_chan_cfg.dma_frame_num = 240; tx_chan_cfg.dma_frame_num = 240 * 3;
tx_chan_cfg.auto_clear_after_cb = true; tx_chan_cfg.auto_clear_after_cb = true;
tx_chan_cfg.auto_clear_before_cb = false; tx_chan_cfg.auto_clear_before_cb = false;
tx_chan_cfg.intr_priority = 0; tx_chan_cfg.intr_priority = 0;

View File

@ -32,7 +32,7 @@ public:
virtual void StartNetwork() = 0; virtual void StartNetwork() = 0;
virtual ~Board() = default; virtual ~Board() = default;
virtual Led* GetLed() = 0; virtual Led* GetLed();
virtual AudioCodec* GetAudioCodec() = 0; virtual AudioCodec* GetAudioCodec() = 0;
virtual Display* GetDisplay(); virtual Display* GetDisplay();
virtual Http* CreateHttp() = 0; virtual Http* CreateHttp() = 0;

View File

@ -1,6 +1,6 @@
#include "wifi_board.h" #include "wifi_board.h"
#include "audio_codecs/box_audio_codec.h" #include "audio_codecs/box_audio_codec.h"
#include "display/st7789_display.h" #include "display/lcd_display.h"
#include "esp_lcd_ili9341.h" #include "esp_lcd_ili9341.h"
#include "font_awesome_symbols.h" #include "font_awesome_symbols.h"
#include "application.h" #include "application.h"
@ -42,7 +42,7 @@ static const ili9341_lcd_init_cmd_t vendor_specific_init[] = {
}; };
// Example Display and UI overwrite in different board // Example Display and UI overwrite in different board
class Ili9341Display : public St7789Display { class Ili9341Display : public LcdDisplay {
private: private:
lv_obj_t *user_messge_label_ = nullptr; lv_obj_t *user_messge_label_ = nullptr;
lv_obj_t *ai_messge_label_ = nullptr; lv_obj_t *ai_messge_label_ = nullptr;
@ -50,44 +50,27 @@ public:
Ili9341Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, Ili9341Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
gpio_num_t backlight_pin, bool backlight_output_invert, 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) int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy)
: St7789Display(panel_io, panel, backlight_pin, backlight_output_invert, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy) {} : LcdDisplay(panel_io, panel, backlight_pin, backlight_output_invert, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy) {}
void SetStatus(const std::string &status) override
{
if (status_label_ == nullptr) {
return;
}
if(status=="待命")
{
SetChatMessage(" "," ");
}
DisplayLockGuard lock(this);
lv_label_set_text(status_label_, status.c_str());
}
void SetChatMessage(const std::string &role, const std::string &content) override { void SetChatMessage(const std::string &role, const std::string &content) override {
if (ai_messge_label_== nullptr || user_messge_label_== nullptr) { if (ai_messge_label_== nullptr || user_messge_label_== nullptr) {
return; return;
} }
DisplayLockGuard lock(this); DisplayLockGuard lock(this);
ESP_LOGI(TAG,"role:%s",role.c_str()); ESP_LOGI(TAG, "role:%s", role.c_str());
if(role=="assistant") if(role == "assistant") {
{ std::string new_content = "AI: " + content;
std::string new_content = "AI:" + content;
lv_label_set_text(ai_messge_label_, new_content.c_str()); lv_label_set_text(ai_messge_label_, new_content.c_str());
} } else if(role == "user") {
else if(role=="user") std::string new_content = "User: " + content;
{
std::string new_content = "User:" + content;
lv_label_set_text(user_messge_label_, new_content.c_str());
}
else{
std::string new_content = "AI:";
lv_label_set_text(ai_messge_label_, new_content.c_str());
new_content="User:";
lv_label_set_text(user_messge_label_, new_content.c_str()); lv_label_set_text(user_messge_label_, new_content.c_str());
} else{
lv_label_set_text(ai_messge_label_, "AI: ");
lv_label_set_text(user_messge_label_, "User: ");
} }
} }
void SetupUI() override { void SetupUI() override {
DisplayLockGuard lock(this); DisplayLockGuard lock(this);
@ -97,23 +80,23 @@ public:
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0); lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0);
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP); lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
lv_obj_align(emotion_label_,LV_ALIGN_TOP_MID, 0, -10); // 左侧居中向右偏移10个单位 lv_obj_align(emotion_label_, LV_ALIGN_TOP_MID, 0, -10); // 左侧居中向右偏移10个单位
static lv_style_t style_msg; static lv_style_t style_msg;
lv_style_init(&style_msg); lv_style_init(&style_msg);
lv_style_set_width(&style_msg, LV_HOR_RES-25); lv_style_set_width(&style_msg, LV_HOR_RES - 25);
user_messge_label_ = lv_label_create(content_); user_messge_label_ = lv_label_create(content_);
lv_obj_set_style_text_font(user_messge_label_, &font_puhui_14_1, 0); lv_obj_set_style_text_font(user_messge_label_, &font_puhui_14_1, 0);
lv_label_set_text(user_messge_label_, "User:"); lv_label_set_text(user_messge_label_, "User: ");
lv_obj_add_style(user_messge_label_, &style_msg, 0); lv_obj_add_style(user_messge_label_, &style_msg, 0);
lv_obj_align(user_messge_label_,LV_ALIGN_TOP_LEFT, 2, 25); lv_obj_align(user_messge_label_, LV_ALIGN_TOP_LEFT, 2, 25);
ai_messge_label_ = lv_label_create(content_); ai_messge_label_ = lv_label_create(content_);
lv_obj_set_style_text_font(ai_messge_label_, &font_puhui_14_1, 0); lv_obj_set_style_text_font(ai_messge_label_, &font_puhui_14_1, 0);
lv_label_set_text(ai_messge_label_, "AI:"); lv_label_set_text(ai_messge_label_, "AI: ");
lv_obj_add_style(ai_messge_label_, &style_msg, 0); lv_obj_add_style(ai_messge_label_, &style_msg, 0);
lv_obj_align(ai_messge_label_,LV_ALIGN_TOP_LEFT, 2, 77); lv_obj_align(ai_messge_label_, LV_ALIGN_TOP_LEFT, 2, 77);
} }
}; };
@ -228,13 +211,6 @@ public:
virtual Display* GetDisplay() override { virtual Display* GetDisplay() override {
return display_; return display_;
} }
virtual bool GetBatteryLevel(int &level, bool& charging) override
{
charging = false;
level = 60;
return true;
};
}; };
DECLARE_BOARD(EspBox3Board); DECLARE_BOARD(EspBox3Board);

View File

@ -1,6 +1,6 @@
#include "wifi_board.h" #include "wifi_board.h"
#include "audio_codecs/box_audio_codec.h" #include "audio_codecs/box_audio_codec.h"
#include "display/st7789_display.h" #include "display/lcd_display.h"
#include "application.h" #include "application.h"
#include "button.h" #include "button.h"
#include "config.h" #include "config.h"
@ -36,7 +36,7 @@ private:
i2c_master_bus_handle_t i2c_bus_; i2c_master_bus_handle_t i2c_bus_;
i2c_master_dev_handle_t pca9557_handle_; i2c_master_dev_handle_t pca9557_handle_;
Button boot_button_; Button boot_button_;
St7789Display* display_; LcdDisplay* display_;
Pca9557* pca9557_; Pca9557* pca9557_;
void InitializeI2c() { void InitializeI2c() {
@ -115,7 +115,7 @@ private:
esp_lcd_panel_invert_color(panel, true); esp_lcd_panel_invert_color(panel, true);
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
display_ = new St7789Display(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT, display_ = new LcdDisplay(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT,
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
} }

View File

@ -1,9 +1,8 @@
#include "wifi_board.h" #include "wifi_board.h"
#include "audio_codecs/cores3_audio_codec.h" #include "audio_codecs/cores3_audio_codec.h"
#include "display/st7789_display.h" #include "display/lcd_display.h"
#include "application.h" #include "application.h"
#include "button.h" #include "button.h"
#include "led.h"
#include "config.h" #include "config.h"
#include "i2c_device.h" #include "i2c_device.h"
#include "iot/thing_manager.h" #include "iot/thing_manager.h"
@ -31,6 +30,18 @@ public:
WriteReg(0x94, 33 - 5); WriteReg(0x94, 33 - 5);
WriteReg(0x95, 33 - 5); WriteReg(0x95, 33 - 5);
} }
int GetBatteryCurrentDirection() {
return (ReadReg(0x01) & 0b01100000) >> 5;
}
bool IsCharging() {
return GetBatteryCurrentDirection() == 1;
}
int GetBatteryLevel() {
return ReadReg(0xA4);
}
}; };
class Aw9523 : public I2cDevice { class Aw9523 : public I2cDevice {
@ -103,7 +114,7 @@ private:
Axp2101* axp2101_; Axp2101* axp2101_;
Aw9523* aw9523_; Aw9523* aw9523_;
Ft6336* ft6336_; Ft6336* ft6336_;
St7789Display* display_; LcdDisplay* display_;
Button boot_button_; Button boot_button_;
void InitializeI2c() { void InitializeI2c() {
@ -227,7 +238,7 @@ private:
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
display_ = new St7789Display(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT, display_ = new LcdDisplay(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT,
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
} }
@ -256,11 +267,6 @@ public:
InitializeIot(); InitializeIot();
} }
virtual Led* GetBuiltinLed() override {
static Led led(GPIO_NUM_NC);
return &led;
}
virtual AudioCodec* GetAudioCodec() override { virtual AudioCodec* GetAudioCodec() override {
static CoreS3AudioCodec* audio_codec = nullptr; static CoreS3AudioCodec* audio_codec = nullptr;
if (audio_codec == nullptr) { if (audio_codec == nullptr) {
@ -276,6 +282,19 @@ public:
return display_; return display_;
} }
virtual bool GetBatteryLevel(int &level, bool& charging) override {
static int last_level = 0;
static bool last_charging = false;
level = axp2101_->GetBatteryLevel();
charging = axp2101_->IsCharging();
if (level != last_level || charging != last_charging) {
last_level = level;
last_charging = charging;
ESP_LOGI(TAG, "Battery level: %d, charging: %d", level, charging);
}
return true;
}
Ft6336* GetTouchpad() { Ft6336* GetTouchpad() {
return ft6336_; return ft6336_;
} }

View File

@ -1,9 +1,9 @@
#include "wifi_board.h" #include "wifi_board.h"
#include "display/st7789_display.h" #include "display/lcd_display.h"
#include "audio_codecs/es8311_audio_codec.h" #include "audio_codecs/es8311_audio_codec.h"
#include "application.h" #include "application.h"
#include "button.h" #include "button.h"
#include "led.h" #include "led/single_led.h"
#include "config.h" #include "config.h"
#include <esp_lcd_panel_vendor.h> #include <esp_lcd_panel_vendor.h>
#include <wifi_station.h> #include <wifi_station.h>
@ -17,7 +17,7 @@ class magiclick_2p4 : public WifiBoard {
private: private:
i2c_master_bus_handle_t codec_i2c_bus_; i2c_master_bus_handle_t codec_i2c_bus_;
Button boot_button_; Button boot_button_;
St7789Display* display_; LcdDisplay* display_;
void InitializeCodecI2c() { void InitializeCodecI2c() {
// Initialize I2C peripheral // Initialize I2C peripheral
@ -39,7 +39,7 @@ private:
void InitializeButtons() { void InitializeButtons() {
boot_button_.OnClick([this]() { boot_button_.OnClick([this]() {
auto& app = Application::GetInstance(); auto& app = Application::GetInstance();
if (app.GetChatState() == kChatStateUnknown && !WifiStation::GetInstance().IsConnected()) { if (app.GetDeviceState() == kDeviceStateUnknown && !WifiStation::GetInstance().IsConnected()) {
ResetWifiConfiguration(); ResetWifiConfiguration();
} }
}); });
@ -57,6 +57,7 @@ private:
gpio_set_direction(BUILTIN_LED_POWER, GPIO_MODE_OUTPUT); gpio_set_direction(BUILTIN_LED_POWER, GPIO_MODE_OUTPUT);
gpio_set_level(BUILTIN_LED_POWER, BUILTIN_LED_POWER_OUTPUT_INVERT ? 0 : 1); gpio_set_level(BUILTIN_LED_POWER, BUILTIN_LED_POWER_OUTPUT_INVERT ? 0 : 1);
} }
void InitializeSpi() { void InitializeSpi() {
spi_bus_config_t buscfg = {}; spi_bus_config_t buscfg = {};
buscfg.mosi_io_num = DISPLAY_SDA_PIN; buscfg.mosi_io_num = DISPLAY_SDA_PIN;
@ -68,7 +69,7 @@ private:
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
} }
void InitializeSt7789Display(){ void InitializeNv3023Display(){
esp_lcd_panel_io_handle_t panel_io = nullptr; esp_lcd_panel_io_handle_t panel_io = nullptr;
esp_lcd_panel_handle_t panel = nullptr; esp_lcd_panel_handle_t panel = nullptr;
// 液晶屏控制IO初始化 // 液晶屏控制IO初始化
@ -83,7 +84,7 @@ private:
io_config.lcd_param_bits = 8; io_config.lcd_param_bits = 8;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io)); ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
// 初始化液晶屏驱动芯片ST7789 // 初始化液晶屏驱动芯片NV3023
ESP_LOGD(TAG, "Install LCD driver"); ESP_LOGD(TAG, "Install LCD driver");
esp_lcd_panel_dev_config_t panel_config = {}; esp_lcd_panel_dev_config_t panel_config = {};
panel_config.reset_gpio_num = DISPLAY_RST_PIN; panel_config.reset_gpio_num = DISPLAY_RST_PIN;
@ -99,7 +100,7 @@ private:
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true)); ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
display_ = new St7789Display(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT, display_ = new LcdDisplay(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT,
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
} }
@ -110,13 +111,11 @@ public:
InitializeButtons(); InitializeButtons();
InitializeLedPower(); InitializeLedPower();
InitializeSpi(); InitializeSpi();
InitializeSt7789Display(); InitializeNv3023Display();
} }
virtual Led* GetBuiltinLed() override { virtual Led* GetLed() override {
// static Led led(BUILTIN_LED_GPIO,BUILTIN_LED_NUM); static SingleLed led(BUILTIN_LED_GPIO);
static Led led(BUILTIN_LED_GPIO);
return &led; return &led;
} }

View File

@ -1,4 +1,4 @@
#include "st7789_display.h" #include "lcd_display.h"
#include "font_awesome_symbols.h" #include "font_awesome_symbols.h"
#include <esp_log.h> #include <esp_log.h>
@ -6,21 +6,21 @@
#include <driver/ledc.h> #include <driver/ledc.h>
#include <vector> #include <vector>
#define TAG "St7789Display" #define TAG "LcdDisplay"
#define LCD_LEDC_CH LEDC_CHANNEL_0 #define LCD_LEDC_CH LEDC_CHANNEL_0
#define ST7789_LVGL_TICK_PERIOD_MS 2 #define LCD_LVGL_TICK_PERIOD_MS 2
#define ST7789_LVGL_TASK_MAX_DELAY_MS 20 #define LCD_LVGL_TASK_MAX_DELAY_MS 20
#define ST7789_LVGL_TASK_MIN_DELAY_MS 1 #define LCD_LVGL_TASK_MIN_DELAY_MS 1
#define ST7789_LVGL_TASK_STACK_SIZE (4 * 1024) #define LCD_LVGL_TASK_STACK_SIZE (4 * 1024)
#define ST7789_LVGL_TASK_PRIORITY 10 #define LCD_LVGL_TASK_PRIORITY 10
LV_FONT_DECLARE(font_puhui_14_1); LV_FONT_DECLARE(font_puhui_14_1);
LV_FONT_DECLARE(font_awesome_30_1); LV_FONT_DECLARE(font_awesome_30_1);
LV_FONT_DECLARE(font_awesome_14_1); LV_FONT_DECLARE(font_awesome_14_1);
static lv_disp_drv_t disp_drv; static lv_disp_drv_t disp_drv;
static void st7789_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map) static void lcd_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{ {
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data; esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
int offsetx1 = area->x1; int offsetx1 = area->x1;
@ -33,7 +33,7 @@ static void st7789_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_c
} }
/* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */ /* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */
static void st7789_lvgl_port_update_callback(lv_disp_drv_t *drv) static void lcd_lvgl_port_update_callback(lv_disp_drv_t *drv)
{ {
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data; esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
@ -43,48 +43,28 @@ static void st7789_lvgl_port_update_callback(lv_disp_drv_t *drv)
// Rotate LCD display // Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false); esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, true, false); esp_lcd_panel_mirror(panel_handle, true, false);
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
// Rotate LCD touch
esp_lcd_touch_set_mirror_y(tp, false);
esp_lcd_touch_set_mirror_x(tp, false);
#endif
break; break;
case LV_DISP_ROT_90: case LV_DISP_ROT_90:
// Rotate LCD display // Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true); esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, true, true); esp_lcd_panel_mirror(panel_handle, true, true);
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
// Rotate LCD touch
esp_lcd_touch_set_mirror_y(tp, false);
esp_lcd_touch_set_mirror_x(tp, false);
#endif
break; break;
case LV_DISP_ROT_180: case LV_DISP_ROT_180:
// Rotate LCD display // Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, false); esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, false, true); esp_lcd_panel_mirror(panel_handle, false, true);
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
// Rotate LCD touch
esp_lcd_touch_set_mirror_y(tp, false);
esp_lcd_touch_set_mirror_x(tp, false);
#endif
break; break;
case LV_DISP_ROT_270: case LV_DISP_ROT_270:
// Rotate LCD display // Rotate LCD display
esp_lcd_panel_swap_xy(panel_handle, true); esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, false, false); esp_lcd_panel_mirror(panel_handle, false, false);
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
// Rotate LCD touch
esp_lcd_touch_set_mirror_y(tp, false);
esp_lcd_touch_set_mirror_x(tp, false);
#endif
break; break;
} }
} }
void St7789Display::LvglTask() { void LcdDisplay::LvglTask() {
ESP_LOGI(TAG, "Starting LVGL task"); ESP_LOGI(TAG, "Starting LVGL task");
uint32_t task_delay_ms = ST7789_LVGL_TASK_MAX_DELAY_MS; uint32_t task_delay_ms = LCD_LVGL_TASK_MAX_DELAY_MS;
while (1) while (1)
{ {
// Lock the mutex due to the LVGL APIs are not thread-safe // Lock the mutex due to the LVGL APIs are not thread-safe
@ -93,20 +73,20 @@ void St7789Display::LvglTask() {
task_delay_ms = lv_timer_handler(); task_delay_ms = lv_timer_handler();
Unlock(); Unlock();
} }
if (task_delay_ms > ST7789_LVGL_TASK_MAX_DELAY_MS) if (task_delay_ms > LCD_LVGL_TASK_MAX_DELAY_MS)
{ {
task_delay_ms = ST7789_LVGL_TASK_MAX_DELAY_MS; task_delay_ms = LCD_LVGL_TASK_MAX_DELAY_MS;
} }
else if (task_delay_ms < ST7789_LVGL_TASK_MIN_DELAY_MS) else if (task_delay_ms < LCD_LVGL_TASK_MIN_DELAY_MS)
{ {
task_delay_ms = ST7789_LVGL_TASK_MIN_DELAY_MS; task_delay_ms = LCD_LVGL_TASK_MIN_DELAY_MS;
} }
vTaskDelay(pdMS_TO_TICKS(task_delay_ms)); vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
} }
} }
St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, 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, 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) int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy)
: panel_io_(panel_io), panel_(panel), backlight_pin_(backlight_pin), backlight_output_invert_(backlight_output_invert), : panel_io_(panel_io), panel_(panel), backlight_pin_(backlight_pin), backlight_output_invert_(backlight_output_invert),
@ -147,8 +127,8 @@ St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
disp_drv.ver_res = height_; disp_drv.ver_res = height_;
disp_drv.offset_x = offset_x_; disp_drv.offset_x = offset_x_;
disp_drv.offset_y = offset_y_; disp_drv.offset_y = offset_y_;
disp_drv.flush_cb = st7789_lvgl_flush_cb; disp_drv.flush_cb = lcd_lvgl_flush_cb;
disp_drv.drv_update_cb = st7789_lvgl_port_update_callback; disp_drv.drv_update_cb = lcd_lvgl_port_update_callback;
disp_drv.draw_buf = &disp_buf; disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_; disp_drv.user_data = panel_;
@ -158,7 +138,7 @@ St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event) // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = { const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = [](void* arg) { .callback = [](void* arg) {
lv_tick_inc(ST7789_LVGL_TICK_PERIOD_MS); lv_tick_inc(LCD_LVGL_TICK_PERIOD_MS);
}, },
.arg = NULL, .arg = NULL,
.dispatch_method = ESP_TIMER_TASK, .dispatch_method = ESP_TIMER_TASK,
@ -166,22 +146,22 @@ St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
.skip_unhandled_events = false .skip_unhandled_events = false
}; };
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer_)); ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer_));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer_, ST7789_LVGL_TICK_PERIOD_MS * 1000)); ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer_, LCD_LVGL_TICK_PERIOD_MS * 1000));
lvgl_mutex_ = xSemaphoreCreateRecursiveMutex(); lvgl_mutex_ = xSemaphoreCreateRecursiveMutex();
assert(lvgl_mutex_ != nullptr); assert(lvgl_mutex_ != nullptr);
ESP_LOGI(TAG, "Create LVGL task"); ESP_LOGI(TAG, "Create LVGL task");
xTaskCreate([](void *arg) { xTaskCreate([](void *arg) {
static_cast<St7789Display*>(arg)->LvglTask(); static_cast<LcdDisplay*>(arg)->LvglTask();
vTaskDelete(NULL); vTaskDelete(NULL);
}, "LVGL", ST7789_LVGL_TASK_STACK_SIZE, this, ST7789_LVGL_TASK_PRIORITY, NULL); }, "LVGL", LCD_LVGL_TASK_STACK_SIZE, this, LCD_LVGL_TASK_PRIORITY, NULL);
SetBacklight(100); SetBacklight(100);
SetupUI(); SetupUI();
} }
St7789Display::~St7789Display() { LcdDisplay::~LcdDisplay() {
ESP_ERROR_CHECK(esp_timer_stop(lvgl_tick_timer_)); ESP_ERROR_CHECK(esp_timer_stop(lvgl_tick_timer_));
ESP_ERROR_CHECK(esp_timer_delete(lvgl_tick_timer_)); ESP_ERROR_CHECK(esp_timer_delete(lvgl_tick_timer_));
@ -207,7 +187,7 @@ St7789Display::~St7789Display() {
vSemaphoreDelete(lvgl_mutex_); vSemaphoreDelete(lvgl_mutex_);
} }
void St7789Display::InitializeBacklight(gpio_num_t backlight_pin) { void LcdDisplay::InitializeBacklight(gpio_num_t backlight_pin) {
if (backlight_pin == GPIO_NUM_NC) { if (backlight_pin == GPIO_NUM_NC) {
return; return;
} }
@ -238,7 +218,7 @@ void St7789Display::InitializeBacklight(gpio_num_t backlight_pin) {
ESP_ERROR_CHECK(ledc_channel_config(&backlight_channel)); ESP_ERROR_CHECK(ledc_channel_config(&backlight_channel));
} }
void St7789Display::SetBacklight(uint8_t brightness) { void LcdDisplay::SetBacklight(uint8_t brightness) {
if (backlight_pin_ == GPIO_NUM_NC) { if (backlight_pin_ == GPIO_NUM_NC) {
return; return;
} }
@ -254,18 +234,18 @@ void St7789Display::SetBacklight(uint8_t brightness) {
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH)); ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH));
} }
bool St7789Display::Lock(int timeout_ms) { bool LcdDisplay::Lock(int timeout_ms) {
// Convert timeout in milliseconds to FreeRTOS ticks // Convert timeout in milliseconds to FreeRTOS ticks
// If `timeout_ms` is set to 0, the program will block until the condition is met // 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); const TickType_t timeout_ticks = (timeout_ms == 0) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTakeRecursive(lvgl_mutex_, timeout_ticks) == pdTRUE; return xSemaphoreTakeRecursive(lvgl_mutex_, timeout_ticks) == pdTRUE;
} }
void St7789Display::Unlock() { void LcdDisplay::Unlock() {
xSemaphoreGiveRecursive(lvgl_mutex_); xSemaphoreGiveRecursive(lvgl_mutex_);
} }
void St7789Display::SetupUI() { void LcdDisplay::SetupUI() {
DisplayLockGuard lock(this); DisplayLockGuard lock(this);
auto screen = lv_disp_get_scr_act(lv_disp_get_default()); auto screen = lv_disp_get_scr_act(lv_disp_get_default());
@ -336,7 +316,7 @@ void St7789Display::SetupUI() {
lv_obj_set_style_text_font(battery_label_, &font_awesome_14_1, 0); lv_obj_set_style_text_font(battery_label_, &font_awesome_14_1, 0);
} }
void St7789Display::SetChatMessage(const std::string &role, const std::string &content) { void LcdDisplay::SetChatMessage(const std::string &role, const std::string &content) {
if (chat_message_label_ == nullptr) { if (chat_message_label_ == nullptr) {
return; return;
} }

View File

@ -1,5 +1,5 @@
#ifndef ST7789_DISPLAY_H #ifndef LCD_DISPLAY_H
#define ST7789_DISPLAY_H #define LCD_DISPLAY_H
#include "display.h" #include "display.h"
@ -10,7 +10,7 @@
#include <esp_lcd_panel_ops.h> #include <esp_lcd_panel_ops.h>
#include <esp_timer.h> #include <esp_timer.h>
class St7789Display : public Display { class LcdDisplay : public Display {
protected: protected:
esp_lcd_panel_io_handle_t panel_io_ = nullptr; esp_lcd_panel_io_handle_t panel_io_ = nullptr;
esp_lcd_panel_handle_t panel_ = nullptr; esp_lcd_panel_handle_t panel_ = nullptr;
@ -39,12 +39,12 @@ protected:
virtual void Unlock() override; virtual void Unlock() override;
public: public:
St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
gpio_num_t backlight_pin, bool backlight_output_invert, 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); int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy);
~St7789Display(); ~LcdDisplay();
void SetChatMessage(const std::string &role, const std::string &content) override; void SetChatMessage(const std::string &role, const std::string &content) override;
}; };
#endif // ST7789_DISPLAY_H #endif // LCD_DISPLAY_H

View File

@ -35,7 +35,7 @@ Ssd1306Display::Ssd1306Display(void* i2c_master_handle, int width, int height, b
.dc_low_on_data = 0, .dc_low_on_data = 0,
.disable_control_phase = 0, .disable_control_phase = 0,
}, },
.scl_speed_hz = 400 * 1000, .scl_speed_hz = 100 * 1000,
}; };
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2((i2c_master_bus_t*)i2c_master_handle, &io_config, &panel_io_)); ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2((i2c_master_bus_t*)i2c_master_handle, &io_config, &panel_io_));