feat(display): 集成LVGL图形库并重构显示模块
- 将display_test_module重命名为display_module - 集成LVGL配置到prj.conf中,包括颜色深度、双缓冲等设置 - 添加UI主界面实现(ui_main.c),包含标题和副标题显示 - 实现背光控制功能替代原有的测试图案绘制 - 调整LCD配置参数(mdac从0x70改为0xA0) - 修改日志级别从DEBUG降至ERROR以优化性能 - 在CMakeLists.txt中添加UI模块源文件引用
This commit is contained in:
@@ -17,10 +17,11 @@ target_sources(app PRIVATE
|
|||||||
src/ble_adv_uuid16.c
|
src/ble_adv_uuid16.c
|
||||||
src/ble_bas_module.c
|
src/ble_bas_module.c
|
||||||
src/ble_hid_module.c
|
src/ble_hid_module.c
|
||||||
src/display_test_module.c
|
src/display_module.c
|
||||||
src/encoder_module.c
|
src/encoder_module.c
|
||||||
src/hid_flowctrl_module.c
|
src/hid_flowctrl_module.c
|
||||||
src/keyboard_core_module.c
|
src/keyboard_core_module.c
|
||||||
|
src/ui/ui_main.c
|
||||||
src/usb_hid_module.c
|
src/usb_hid_module.c
|
||||||
src/events/bat_state_event.c
|
src/events/bat_state_event.c
|
||||||
src/events/encoder_event.c
|
src/events/encoder_event.c
|
||||||
|
|||||||
@@ -76,7 +76,7 @@
|
|||||||
y-offset = <34>;
|
y-offset = <34>;
|
||||||
vcom = <0x34>;
|
vcom = <0x34>;
|
||||||
gctrl = <0x00>;
|
gctrl = <0x00>;
|
||||||
mdac = <0x70>;
|
mdac = <0xA0>;
|
||||||
gamma = <0x01>;
|
gamma = <0x01>;
|
||||||
colmod = <0x05>;
|
colmod = <0x05>;
|
||||||
lcm = <0x2c>;
|
lcm = <0x2c>;
|
||||||
|
|||||||
19
prj.conf
19
prj.conf
@@ -16,8 +16,8 @@ CONFIG_REBOOT=y
|
|||||||
CONFIG_SENSOR=y
|
CONFIG_SENSOR=y
|
||||||
CONFIG_ADC=y
|
CONFIG_ADC=y
|
||||||
CONFIG_DISPLAY=y
|
CONFIG_DISPLAY=y
|
||||||
CONFIG_DISPLAY_LOG_LEVEL_DBG=y
|
CONFIG_DISPLAY_LOG_LEVEL_ERR=y
|
||||||
CONFIG_MIPI_DBI_LOG_LEVEL_DBG=y
|
CONFIG_MIPI_DBI_LOG_LEVEL_ERR=y
|
||||||
CONFIG_SETTINGS=y
|
CONFIG_SETTINGS=y
|
||||||
CONFIG_SETTINGS_NVS=y
|
CONFIG_SETTINGS_NVS=y
|
||||||
CONFIG_FLASH=y
|
CONFIG_FLASH=y
|
||||||
@@ -95,3 +95,18 @@ CONFIG_BT_ADV_PROV_FLAGS=y
|
|||||||
CONFIG_BT_ADV_PROV_GAP_APPEARANCE=y
|
CONFIG_BT_ADV_PROV_GAP_APPEARANCE=y
|
||||||
CONFIG_BT_ADV_PROV_DEVICE_NAME=y
|
CONFIG_BT_ADV_PROV_DEVICE_NAME=y
|
||||||
CONFIG_BT_ADV_PROV_DEVICE_NAME_SD=y
|
CONFIG_BT_ADV_PROV_DEVICE_NAME_SD=y
|
||||||
|
|
||||||
|
# LVGL
|
||||||
|
CONFIG_LVGL=y
|
||||||
|
CONFIG_LV_Z_AUTO_INIT=n
|
||||||
|
CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE=y
|
||||||
|
CONFIG_LV_Z_LVGL_MUTEX=y
|
||||||
|
CONFIG_LV_COLOR_DEPTH_16=y
|
||||||
|
CONFIG_LV_COLOR_16_SWAP=y
|
||||||
|
CONFIG_LV_Z_BITS_PER_PIXEL=16
|
||||||
|
CONFIG_LV_Z_VDB_SIZE=25
|
||||||
|
CONFIG_LV_Z_DOUBLE_VDB=y
|
||||||
|
CONFIG_LV_Z_MEM_POOL_SIZE=16384
|
||||||
|
CONFIG_LV_USE_LABEL=y
|
||||||
|
CONFIG_LV_FONT_MONTSERRAT_14=y
|
||||||
|
CONFIG_MAIN_STACK_SIZE=4096
|
||||||
|
|||||||
@@ -1,26 +1,22 @@
|
|||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#include <app_event_manager.h>
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
#define MODULE display_test_module
|
#define MODULE display_module
|
||||||
#include <caf/events/module_state_event.h>
|
#include <caf/events/module_state_event.h>
|
||||||
#include <caf/events/power_event.h>
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <lvgl_zephyr.h>
|
||||||
#include <zephyr/device.h>
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/drivers/display.h>
|
#include <zephyr/drivers/display.h>
|
||||||
#include <zephyr/drivers/led.h>
|
#include <zephyr/drivers/led.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zephyr/sys/util.h>
|
|
||||||
|
#include "ui/ui_main.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
#define DISPLAY_TEST_LINE_PIXELS 320U
|
|
||||||
#define COLOR_RED 0xF800U
|
|
||||||
#define COLOR_GREEN 0x07E0U
|
|
||||||
#define COLOR_BLUE 0x001FU
|
|
||||||
#define COLOR_YELLOW 0xFFE0U
|
|
||||||
|
|
||||||
BUILD_ASSERT(DT_HAS_CHOSEN(zephyr_display), "Missing zephyr,display chosen node");
|
BUILD_ASSERT(DT_HAS_CHOSEN(zephyr_display), "Missing zephyr,display chosen node");
|
||||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(DT_ALIAS(backlight), okay),
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(DT_ALIAS(backlight), okay),
|
||||||
"Missing backlight alias");
|
"Missing backlight alias");
|
||||||
@@ -30,80 +26,24 @@ static const struct device *const display_dev =
|
|||||||
static const struct device *const backlight_dev =
|
static const struct device *const backlight_dev =
|
||||||
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
||||||
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
||||||
static uint16_t line_buf[DISPLAY_TEST_LINE_PIXELS];
|
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
|
static bool lvgl_initialized;
|
||||||
|
|
||||||
static void fill_test_line(size_t width)
|
static int backlight_set(bool on)
|
||||||
{
|
{
|
||||||
const uint16_t colors[] = {
|
if (on) {
|
||||||
COLOR_RED,
|
return led_on(backlight_dev, backlight_idx);
|
||||||
COLOR_GREEN,
|
|
||||||
COLOR_BLUE,
|
|
||||||
COLOR_YELLOW,
|
|
||||||
};
|
|
||||||
size_t segment = MAX(width / ARRAY_SIZE(colors), 1U);
|
|
||||||
|
|
||||||
for (size_t x = 0; x < width; x++) {
|
|
||||||
size_t color_idx = MIN(x / segment, ARRAY_SIZE(colors) - 1U);
|
|
||||||
|
|
||||||
line_buf[x] = colors[color_idx];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int draw_test_pattern(void)
|
|
||||||
{
|
|
||||||
struct display_capabilities caps;
|
|
||||||
struct display_buffer_descriptor desc;
|
|
||||||
int err;
|
|
||||||
|
|
||||||
display_get_capabilities(display_dev, &caps);
|
|
||||||
LOG_INF("Display caps: %ux%u fmt:%u orient:%u info:0x%x",
|
|
||||||
caps.x_resolution, caps.y_resolution,
|
|
||||||
caps.current_pixel_format, caps.current_orientation,
|
|
||||||
caps.screen_info);
|
|
||||||
|
|
||||||
if (caps.x_resolution > ARRAY_SIZE(line_buf)) {
|
|
||||||
LOG_ERR("Display width %u exceeds line buffer", caps.x_resolution);
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (caps.current_pixel_format != PIXEL_FORMAT_RGB_565) {
|
return led_off(backlight_dev, backlight_idx);
|
||||||
LOG_WRN("Unexpected pixel format %u", caps.current_pixel_format);
|
|
||||||
}
|
|
||||||
|
|
||||||
fill_test_line(caps.x_resolution);
|
|
||||||
|
|
||||||
desc.width = caps.x_resolution;
|
|
||||||
desc.height = 1U;
|
|
||||||
desc.pitch = caps.x_resolution;
|
|
||||||
desc.buf_size = caps.x_resolution * sizeof(line_buf[0]);
|
|
||||||
|
|
||||||
for (uint16_t y = 0; y < caps.y_resolution; y++) {
|
|
||||||
err = display_write(display_dev, 0U, y, &desc, line_buf);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("display_write failed at line %u (%d)", y, err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = display_blanking_off(display_dev);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("display_blanking_off failed (%d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Display test pattern rendered (%ux%u)",
|
|
||||||
caps.x_resolution, caps.y_resolution);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int module_init(void)
|
static int module_init(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
LOG_INF("Display test init on %s", display_dev->name);
|
LOG_INF("Display init on %s", display_dev->name);
|
||||||
|
|
||||||
if (!device_is_ready(display_dev)) {
|
if (!device_is_ready(display_dev)) {
|
||||||
LOG_ERR("Display device %s not ready", display_dev->name);
|
LOG_ERR("Display device %s not ready", display_dev->name);
|
||||||
@@ -115,15 +55,12 @@ static int module_init(void)
|
|||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = led_off(backlight_dev, backlight_idx);
|
err = backlight_set(false);
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_ERR("Backlight off failed (%d)", err);
|
LOG_ERR("Backlight off failed (%d)", err);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("Backlight device %s channel %u ready",
|
|
||||||
backlight_dev->name, backlight_idx);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,24 +72,35 @@ static int module_start(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("Display test start");
|
if (!lvgl_initialized) {
|
||||||
|
err = lvgl_init();
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("lvgl_init failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
err = led_on(backlight_dev, backlight_idx);
|
lvgl_initialized = true;
|
||||||
|
|
||||||
|
lvgl_lock();
|
||||||
|
ui_main_init();
|
||||||
|
lvgl_unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
err = backlight_set(true);
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_ERR("Backlight enable failed (%d)", err);
|
LOG_ERR("Backlight enable failed (%d)", err);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("Backlight enabled");
|
err = display_blanking_off(display_dev);
|
||||||
k_sleep(K_MSEC(50));
|
|
||||||
|
|
||||||
err = draw_test_pattern();
|
|
||||||
if (err) {
|
if (err) {
|
||||||
(void)led_off(backlight_dev, backlight_idx);
|
LOG_ERR("display_blanking_off failed (%d)", err);
|
||||||
|
(void)backlight_set(false);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
|
LOG_INF("LVGL display started");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -163,10 +111,10 @@ static void module_pause(void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("Display test pause");
|
|
||||||
(void)display_blanking_on(display_dev);
|
(void)display_blanking_on(display_dev);
|
||||||
(void)led_off(backlight_dev, backlight_idx);
|
(void)backlight_set(false);
|
||||||
running = false;
|
running = false;
|
||||||
|
LOG_INF("LVGL display paused");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool app_event_handler(const struct app_event_header *aeh)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
@@ -176,7 +124,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
int err;
|
int err;
|
||||||
|
|
||||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||||
LOG_INF("main READY received");
|
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
err = module_init();
|
err = module_init();
|
||||||
if (err) {
|
if (err) {
|
||||||
34
src/ui/ui_main.c
Normal file
34
src/ui/ui_main.c
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include <lvgl.h>
|
||||||
|
|
||||||
|
#include "ui_main.h"
|
||||||
|
|
||||||
|
static bool ui_initialized;
|
||||||
|
|
||||||
|
void ui_main_init(void)
|
||||||
|
{
|
||||||
|
lv_obj_t *screen;
|
||||||
|
lv_obj_t *title;
|
||||||
|
lv_obj_t *subtitle;
|
||||||
|
|
||||||
|
if (ui_initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
screen = lv_screen_active();
|
||||||
|
lv_obj_set_style_bg_color(screen, lv_color_hex(0x101418), 0);
|
||||||
|
lv_obj_set_style_text_color(screen, lv_color_hex(0xF5F7FA), 0);
|
||||||
|
|
||||||
|
title = lv_label_create(screen);
|
||||||
|
lv_label_set_text(title, "Hello World");
|
||||||
|
lv_obj_set_style_text_font(title, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_align(title, LV_ALIGN_CENTER, 0, -10);
|
||||||
|
|
||||||
|
subtitle = lv_label_create(screen);
|
||||||
|
lv_label_set_text(subtitle, "WH Mini Keyboard");
|
||||||
|
lv_obj_set_style_text_opa(subtitle, LV_OPA_70, 0);
|
||||||
|
lv_obj_align(subtitle, LV_ALIGN_CENTER, 0, 14);
|
||||||
|
|
||||||
|
ui_initialized = true;
|
||||||
|
}
|
||||||
14
src/ui/ui_main.h
Normal file
14
src/ui/ui_main.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef BLINKY_UI_MAIN_H_
|
||||||
|
#define BLINKY_UI_MAIN_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void ui_main_init(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_UI_MAIN_H_ */
|
||||||
Reference in New Issue
Block a user