From 063d98e3dfa8e0089aa0039b24489d29b062cf5e Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 12 Aug 2021 03:43:07 +0000 Subject: [PATCH] feat(display): Optional dedicated work queue. * Add new Kconfig settingsx to allow selecting system or dedicated work queue for performing UI updates. * Allow UI updates to not block other system tasks when display is updating, especially important for e-ink displays. --- app/include/zmk/display.h | 2 ++ app/src/display/Kconfig | 23 ++++++++++++++++++++++ app/src/display/main.c | 41 +++++++++++++++++++++++++++++++++------ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/app/include/zmk/display.h b/app/include/zmk/display.h index 3f4eb524..d3bd042e 100644 --- a/app/include/zmk/display.h +++ b/app/include/zmk/display.h @@ -6,4 +6,6 @@ #pragma once +struct k_work_q *zmk_display_work_q(); + int zmk_display_init(); \ No newline at end of file diff --git a/app/src/display/Kconfig b/app/src/display/Kconfig index 95b5d479..9c70911e 100644 --- a/app/src/display/Kconfig +++ b/app/src/display/Kconfig @@ -29,6 +29,29 @@ config ZMK_DISPLAY_STATUS_SCREEN_CUSTOM endchoice +choice ZMK_DISPLAY_WORK_QUEUE + prompt "Work queue selection for UI updates" + +config ZMK_DISPLAY_WORK_QUEUE_SYSTEM + bool "Use default system work queue for UI updates" + +config ZMK_DISPLAY_WORK_QUEUE_DEDICATED + bool "Use dedicated work queue for UI updates" + +endchoice + +if ZMK_DISPLAY_WORK_QUEUE_DEDICATED + +config ZMK_DISPLAY_DEDICATED_THREAD_STACK_SIZE + int "Stack size for dedicated UI thread/queue" + default 2048 + +config ZMK_DISPLAY_DEDICATED_THREAD_PRIORITY + int "Thread priority for dedicated UI thread/queue" + default 5 + +endif # ZMK_DISPLAY_WORK_QUEUE_DEDICATED + rsource "widgets/Kconfig" endif diff --git a/app/src/display/main.c b/app/src/display/main.c index 17d68bf3..95b607e8 100644 --- a/app/src/display/main.c +++ b/app/src/display/main.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: MIT */ +#include #include #include #include @@ -32,19 +33,41 @@ void display_tick_cb(struct k_work *work) { lv_task_handler(); } K_WORK_DEFINE(display_tick_work, display_tick_cb); -void display_timer_cb() { - lv_tick_inc(TICK_MS); - k_work_submit(&display_tick_work); +#if IS_ENABLED(CONFIG_ZMK_DISPLAY_WORK_QUEUE_DEDICATED) + +K_THREAD_STACK_DEFINE(display_work_stack_area, CONFIG_ZMK_DISPLAY_DEDICATED_THREAD_STACK_SIZE); + +static struct k_work_q display_work_q; + +#endif + +struct k_work_q *zmk_display_work_q() { +#if IS_ENABLED(CONFIG_ZMK_DISPLAY_WORK_QUEUE_DEDICATED) + return &display_work_q; +#else + return &k_sys_work_q; +#endif } +void display_timer_cb() { + lv_tick_inc(TICK_MS); + k_work_submit_to_queue(zmk_display_work_q(), &display_tick_work); +} + +void blank_display_cb(struct k_work *work) { display_blanking_on(display); } + +void unblank_display_cb(struct k_work *work) { display_blanking_off(display); } + K_TIMER_DEFINE(display_timer, display_timer_cb, NULL); +K_WORK_DEFINE(blank_display_work, blank_display_cb); +K_WORK_DEFINE(unblank_display_work, unblank_display_cb); static void start_display_updates() { if (display == NULL) { return; } - display_blanking_off(display); + k_work_submit_to_queue(zmk_display_work_q(), &unblank_display_work); k_timer_start(&display_timer, K_MSEC(TICK_MS), K_MSEC(TICK_MS)); } @@ -54,7 +77,7 @@ static void stop_display_updates() { return; } - display_blanking_on(display); + k_work_submit_to_queue(zmk_display_work_q(), &blank_display_work); k_timer_stop(&display_timer); } @@ -68,6 +91,12 @@ int zmk_display_init() { return -EINVAL; } +#if IS_ENABLED(CONFIG_ZMK_DISPLAY_WORK_QUEUE_DEDICATED) + k_work_q_start(&display_work_q, display_work_stack_area, + K_THREAD_STACK_SIZEOF(display_work_stack_area), + CONFIG_ZMK_DISPLAY_DEDICATED_THREAD_PRIORITY); +#endif + screen = zmk_display_status_screen(); if (screen == NULL) { @@ -105,4 +134,4 @@ int display_event_handler(const zmk_event_t *eh) { } ZMK_LISTENER(display, display_event_handler); -ZMK_SUBSCRIPTION(display, zmk_activity_state_changed); \ No newline at end of file +ZMK_SUBSCRIPTION(display, zmk_activity_state_changed);