mirror of
https://github.com/78/xiaozhi-esp32.git
synced 2025-08-06 18:29:41 +08:00
268 lines
8.7 KiB
C++
268 lines
8.7 KiB
C++
#include "ssd1306_display.h"
|
|
#include "font_awesome_symbols.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <esp_err.h>
|
|
#include <esp_lcd_panel_ops.h>
|
|
#include <esp_lcd_panel_vendor.h>
|
|
#include <esp_lvgl_port.h>
|
|
|
|
#define TAG "Ssd1306Display"
|
|
|
|
LV_FONT_DECLARE(font_awesome_30_1);
|
|
|
|
Ssd1306Display::Ssd1306Display(void* i2c_master_handle, int width, int height, bool mirror_x, bool mirror_y,
|
|
const lv_font_t* text_font, const lv_font_t* icon_font)
|
|
: mirror_x_(mirror_x), mirror_y_(mirror_y), text_font_(text_font), icon_font_(icon_font) {
|
|
width_ = width;
|
|
height_ = height;
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL");
|
|
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
|
lvgl_port_init(&port_cfg);
|
|
|
|
// SSD1306 config
|
|
esp_lcd_panel_io_i2c_config_t io_config = {
|
|
.dev_addr = 0x3C,
|
|
.on_color_trans_done = nullptr,
|
|
.user_ctx = nullptr,
|
|
.control_phase_bytes = 1,
|
|
.dc_bit_offset = 6,
|
|
.lcd_cmd_bits = 8,
|
|
.lcd_param_bits = 8,
|
|
.flags = {
|
|
.dc_low_on_data = 0,
|
|
.disable_control_phase = 0,
|
|
},
|
|
.scl_speed_hz = 400 * 1000,
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c_v2((i2c_master_bus_t*)i2c_master_handle, &io_config, &panel_io_));
|
|
|
|
ESP_LOGI(TAG, "Install SSD1306 driver");
|
|
esp_lcd_panel_dev_config_t panel_config = {};
|
|
panel_config.reset_gpio_num = -1;
|
|
panel_config.bits_per_pixel = 1;
|
|
|
|
esp_lcd_panel_ssd1306_config_t ssd1306_config = {
|
|
.height = static_cast<uint8_t>(height_),
|
|
};
|
|
panel_config.vendor_config = &ssd1306_config;
|
|
|
|
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(panel_io_, &panel_config, &panel_));
|
|
ESP_LOGI(TAG, "SSD1306 driver installed");
|
|
|
|
// Reset the display
|
|
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
|
|
if (esp_lcd_panel_init(panel_) != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to initialize display");
|
|
return;
|
|
}
|
|
|
|
// Set the display to on
|
|
ESP_LOGI(TAG, "Turning display on");
|
|
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
|
|
|
|
ESP_LOGI(TAG, "Adding LCD screen");
|
|
const lvgl_port_display_cfg_t display_cfg = {
|
|
.io_handle = panel_io_,
|
|
.panel_handle = panel_,
|
|
.control_handle = nullptr,
|
|
.buffer_size = static_cast<uint32_t>(width_ * height_),
|
|
.double_buffer = false,
|
|
.trans_size = 0,
|
|
.hres = static_cast<uint32_t>(width_),
|
|
.vres = static_cast<uint32_t>(height_),
|
|
.monochrome = true,
|
|
.rotation = {
|
|
.swap_xy = false,
|
|
.mirror_x = mirror_x_,
|
|
.mirror_y = mirror_y_,
|
|
},
|
|
.flags = {
|
|
.buff_dma = 1,
|
|
.buff_spiram = 0,
|
|
.sw_rotate = 0,
|
|
.full_refresh = 0,
|
|
.direct_mode = 0,
|
|
},
|
|
};
|
|
|
|
disp_ = lvgl_port_add_disp(&display_cfg);
|
|
|
|
if (disp_ == nullptr) {
|
|
ESP_LOGE(TAG, "Failed to add display");
|
|
return;
|
|
}
|
|
|
|
if (height_ == 64) {
|
|
SetupUI_128x64();
|
|
} else {
|
|
SetupUI_128x32();
|
|
}
|
|
}
|
|
|
|
Ssd1306Display::~Ssd1306Display() {
|
|
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_);
|
|
}
|
|
|
|
if (panel_ != nullptr) {
|
|
esp_lcd_panel_del(panel_);
|
|
}
|
|
if (panel_io_ != nullptr) {
|
|
esp_lcd_panel_io_del(panel_io_);
|
|
}
|
|
lvgl_port_deinit();
|
|
}
|
|
|
|
bool Ssd1306Display::Lock(int timeout_ms) {
|
|
return lvgl_port_lock(timeout_ms);
|
|
}
|
|
|
|
void Ssd1306Display::Unlock() {
|
|
lvgl_port_unlock();
|
|
}
|
|
|
|
void Ssd1306Display::SetupUI_128x64() {
|
|
DisplayLockGuard lock(this);
|
|
|
|
auto screen = lv_disp_get_scr_act(disp_);
|
|
lv_obj_set_style_text_font(screen, text_font_, 0);
|
|
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_);
|
|
lv_obj_set_size(status_bar_, LV_HOR_RES, 18);
|
|
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(status_bar_, 0, 0);
|
|
lv_obj_set_width(content_, LV_HOR_RES);
|
|
lv_obj_set_flex_grow(content_, 1);
|
|
|
|
emotion_label_ = lv_label_create(content_);
|
|
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0);
|
|
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
|
|
lv_obj_center(emotion_label_);
|
|
|
|
/* 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);
|
|
|
|
network_label_ = lv_label_create(status_bar_);
|
|
lv_label_set_text(network_label_, "");
|
|
lv_obj_set_style_text_font(network_label_, icon_font_, 0);
|
|
|
|
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);
|
|
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_, "");
|
|
lv_obj_set_style_text_font(mute_label_, icon_font_, 0);
|
|
|
|
battery_label_ = lv_label_create(status_bar_);
|
|
lv_label_set_text(battery_label_, "");
|
|
lv_obj_set_style_text_font(battery_label_, icon_font_, 0);
|
|
}
|
|
|
|
void Ssd1306Display::SetupUI_128x32() {
|
|
DisplayLockGuard lock(this);
|
|
|
|
auto screen = lv_disp_get_scr_act(disp_);
|
|
lv_obj_set_style_text_font(screen, text_font_, 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_ROW);
|
|
lv_obj_set_style_pad_all(container_, 0, 0);
|
|
lv_obj_set_style_border_width(container_, 0, 0);
|
|
lv_obj_set_style_pad_column(container_, 0, 0);
|
|
|
|
/* Left side */
|
|
side_bar_ = lv_obj_create(container_);
|
|
lv_obj_set_flex_grow(side_bar_, 1);
|
|
lv_obj_set_flex_flow(side_bar_, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_style_pad_all(side_bar_, 0, 0);
|
|
lv_obj_set_style_border_width(side_bar_, 0, 0);
|
|
lv_obj_set_style_radius(side_bar_, 0, 0);
|
|
lv_obj_set_style_pad_row(side_bar_, 0, 0);
|
|
|
|
/* Emotion label on the right side */
|
|
content_ = lv_obj_create(container_);
|
|
lv_obj_set_size(content_, 32, 32);
|
|
lv_obj_set_style_pad_all(content_, 0, 0);
|
|
lv_obj_set_style_border_width(content_, 0, 0);
|
|
lv_obj_set_style_radius(content_, 0, 0);
|
|
|
|
emotion_label_ = lv_label_create(content_);
|
|
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0);
|
|
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
|
|
lv_obj_center(emotion_label_);
|
|
|
|
/* Status bar */
|
|
status_bar_ = lv_obj_create(side_bar_);
|
|
lv_obj_set_size(status_bar_, LV_SIZE_CONTENT, 16);
|
|
lv_obj_set_style_radius(status_bar_, 0, 0);
|
|
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);
|
|
|
|
network_label_ = lv_label_create(status_bar_);
|
|
lv_label_set_text(network_label_, "");
|
|
lv_obj_set_style_text_font(network_label_, icon_font_, 0);
|
|
|
|
mute_label_ = lv_label_create(status_bar_);
|
|
lv_label_set_text(mute_label_, "");
|
|
lv_obj_set_style_text_font(mute_label_, icon_font_, 0);
|
|
|
|
battery_label_ = lv_label_create(status_bar_);
|
|
lv_label_set_text(battery_label_, "");
|
|
lv_obj_set_style_text_font(battery_label_, icon_font_, 0);
|
|
|
|
status_label_ = lv_label_create(side_bar_);
|
|
lv_obj_set_flex_grow(status_label_, 1);
|
|
lv_obj_set_width(status_label_, width_ - 32);
|
|
lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
|
lv_label_set_text(status_label_, "正在初始化");
|
|
|
|
notification_label_ = lv_label_create(side_bar_);
|
|
lv_obj_set_flex_grow(notification_label_, 1);
|
|
lv_obj_set_width(notification_label_, width_ - 32);
|
|
lv_label_set_long_mode(notification_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
|
lv_label_set_text(notification_label_, "通知");
|
|
lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
|
|
}
|
|
|