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.
This commit is contained in:
Peter Johanson 2021-08-12 03:43:07 +00:00 committed by Pete Johanson
parent 9f2785786f
commit 063d98e3df
3 changed files with 60 additions and 6 deletions

View file

@ -6,4 +6,6 @@
#pragma once #pragma once
struct k_work_q *zmk_display_work_q();
int zmk_display_init(); int zmk_display_init();

View file

@ -29,6 +29,29 @@ config ZMK_DISPLAY_STATUS_SCREEN_CUSTOM
endchoice 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" rsource "widgets/Kconfig"
endif endif

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <kernel.h>
#include <init.h> #include <init.h>
#include <device.h> #include <device.h>
#include <devicetree.h> #include <devicetree.h>
@ -32,19 +33,41 @@ void display_tick_cb(struct k_work *work) { lv_task_handler(); }
K_WORK_DEFINE(display_tick_work, display_tick_cb); K_WORK_DEFINE(display_tick_work, display_tick_cb);
void display_timer_cb() { #if IS_ENABLED(CONFIG_ZMK_DISPLAY_WORK_QUEUE_DEDICATED)
lv_tick_inc(TICK_MS);
k_work_submit(&display_tick_work); 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_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() { static void start_display_updates() {
if (display == NULL) { if (display == NULL) {
return; 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)); k_timer_start(&display_timer, K_MSEC(TICK_MS), K_MSEC(TICK_MS));
} }
@ -54,7 +77,7 @@ static void stop_display_updates() {
return; return;
} }
display_blanking_on(display); k_work_submit_to_queue(zmk_display_work_q(), &blank_display_work);
k_timer_stop(&display_timer); k_timer_stop(&display_timer);
} }
@ -68,6 +91,12 @@ int zmk_display_init() {
return -EINVAL; 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(); screen = zmk_display_status_screen();
if (screen == NULL) { if (screen == NULL) {