diff --git a/CMakeLists.txt b/CMakeLists.txt index 7affda1..dc277c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,10 +17,11 @@ target_sources(app PRIVATE src/ble_adv_uuid16.c src/ble_bas_module.c src/ble_hid_module.c - src/display_test_module.c + src/display_module.c src/encoder_module.c src/hid_flowctrl_module.c src/keyboard_core_module.c + src/ui/ui_main.c src/usb_hid_module.c src/events/bat_state_event.c src/events/encoder_event.c diff --git a/boards/atguigu/mini_keyboard/mini_keyboard.dts b/boards/atguigu/mini_keyboard/mini_keyboard.dts index 564aebe..3b6542d 100644 --- a/boards/atguigu/mini_keyboard/mini_keyboard.dts +++ b/boards/atguigu/mini_keyboard/mini_keyboard.dts @@ -76,7 +76,7 @@ y-offset = <34>; vcom = <0x34>; gctrl = <0x00>; - mdac = <0x70>; + mdac = <0xA0>; gamma = <0x01>; colmod = <0x05>; lcm = <0x2c>; diff --git a/prj.conf b/prj.conf index 6512ef2..c5ad358 100644 --- a/prj.conf +++ b/prj.conf @@ -16,8 +16,8 @@ CONFIG_REBOOT=y CONFIG_SENSOR=y CONFIG_ADC=y CONFIG_DISPLAY=y -CONFIG_DISPLAY_LOG_LEVEL_DBG=y -CONFIG_MIPI_DBI_LOG_LEVEL_DBG=y +CONFIG_DISPLAY_LOG_LEVEL_ERR=y +CONFIG_MIPI_DBI_LOG_LEVEL_ERR=y CONFIG_SETTINGS=y CONFIG_SETTINGS_NVS=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_DEVICE_NAME=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 diff --git a/src/display_test_module.c b/src/display_module.c similarity index 55% rename from src/display_test_module.c rename to src/display_module.c index 81dbb3a..7502b5f 100644 --- a/src/display_test_module.c +++ b/src/display_module.c @@ -1,26 +1,22 @@ +#include #include -#include #include -#define MODULE display_test_module +#define MODULE display_module #include #include +#include #include #include #include #include -#include + +#include "ui/ui_main.h" 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_NODE_HAS_STATUS(DT_ALIAS(backlight), okay), "Missing backlight alias"); @@ -30,80 +26,24 @@ static const struct device *const display_dev = static const struct device *const backlight_dev = DEVICE_DT_GET(DT_PARENT(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 running; +static bool lvgl_initialized; -static void fill_test_line(size_t width) +static int backlight_set(bool on) { - const uint16_t colors[] = { - COLOR_RED, - 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 (on) { + return led_on(backlight_dev, backlight_idx); } - if (caps.current_pixel_format != PIXEL_FORMAT_RGB_565) { - 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; + return led_off(backlight_dev, backlight_idx); } static int module_init(void) { 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)) { LOG_ERR("Display device %s not ready", display_dev->name); @@ -115,15 +55,12 @@ static int module_init(void) return -ENODEV; } - err = led_off(backlight_dev, backlight_idx); + err = backlight_set(false); if (err) { LOG_ERR("Backlight off failed (%d)", err); return err; } - LOG_INF("Backlight device %s channel %u ready", - backlight_dev->name, backlight_idx); - return 0; } @@ -135,24 +72,35 @@ static int module_start(void) 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) { LOG_ERR("Backlight enable failed (%d)", err); return err; } - LOG_INF("Backlight enabled"); - k_sleep(K_MSEC(50)); - - err = draw_test_pattern(); + err = display_blanking_off(display_dev); if (err) { - (void)led_off(backlight_dev, backlight_idx); + LOG_ERR("display_blanking_off failed (%d)", err); + (void)backlight_set(false); return err; } running = true; + LOG_INF("LVGL display started"); return 0; } @@ -163,10 +111,10 @@ static void module_pause(void) return; } - LOG_INF("Display test pause"); (void)display_blanking_on(display_dev); - (void)led_off(backlight_dev, backlight_idx); + (void)backlight_set(false); running = false; + LOG_INF("LVGL display paused"); } 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; if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) { - LOG_INF("main READY received"); if (!initialized) { err = module_init(); if (err) { diff --git a/src/ui/ui_main.c b/src/ui/ui_main.c new file mode 100644 index 0000000..db9fa0a --- /dev/null +++ b/src/ui/ui_main.c @@ -0,0 +1,34 @@ +#include + +#include + +#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; +} diff --git a/src/ui/ui_main.h b/src/ui/ui_main.h new file mode 100644 index 0000000..816baee --- /dev/null +++ b/src/ui/ui_main.h @@ -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_ */