From 87dbd4ca28405ab3d17cf62f3df86581606279b7 Mon Sep 17 00:00:00 2001 From: KemoNine Date: Mon, 28 Dec 2020 20:17:32 -0500 Subject: [PATCH] Add uppermost, current layer status widget to oled (#493) * Add uppermost, current layer status widget to oled * Run clang format * Fixup display widget source includes in CMakeLists * Update layer widget to only be enabled on primary half of a split keyboard and shuffle some of the options specific to the widget to be enabled via the widget * Update to latest lvgl/zmk/zephyr ; remove version text from oled per PR * Fixup file names * Remove last remenants of the version display text from the oled * Fixup clang-format Co-authored-by: KemoNine --- .../zmk/display/widgets/layer_status.h | 18 +++++ app/src/display/CMakeLists.txt | 2 +- app/src/display/status_screen.c | 15 ++-- app/src/display/widgets/CMakeLists.txt | 1 + app/src/display/widgets/Kconfig | 6 ++ app/src/display/widgets/layer_status.c | 68 +++++++++++++++++++ 6 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 app/include/zmk/display/widgets/layer_status.h create mode 100644 app/src/display/widgets/layer_status.c diff --git a/app/include/zmk/display/widgets/layer_status.h b/app/include/zmk/display/widgets/layer_status.h new file mode 100644 index 00000000..3779351a --- /dev/null +++ b/app/include/zmk/display/widgets/layer_status.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include + +struct zmk_widget_layer_status { + sys_snode_t node; + lv_obj_t *obj; +}; + +int zmk_widget_layer_status_init(struct zmk_widget_layer_status *widget, lv_obj_t *parent); +lv_obj_t *zmk_widget_layer_status_obj(struct zmk_widget_layer_status *widget); \ No newline at end of file diff --git a/app/src/display/CMakeLists.txt b/app/src/display/CMakeLists.txt index 3b85fc0f..d289f074 100644 --- a/app/src/display/CMakeLists.txt +++ b/app/src/display/CMakeLists.txt @@ -4,4 +4,4 @@ target_sources_ifdef(CONFIG_ZMK_DISPLAY app PRIVATE main.c) target_sources_ifdef(CONFIG_ZMK_DISPLAY_STATUS_SCREEN_BUILT_IN app PRIVATE status_screen.c) -add_subdirectory(widgets/) \ No newline at end of file +add_subdirectory_ifdef(CONFIG_ZMK_DISPLAY widgets/) diff --git a/app/src/display/status_screen.c b/app/src/display/status_screen.c index 9a5c7b41..0c88717a 100644 --- a/app/src/display/status_screen.c +++ b/app/src/display/status_screen.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -19,14 +20,15 @@ static struct zmk_widget_battery_status battery_status_widget; static struct zmk_widget_output_status output_status_widget; #endif +#if IS_ENABLED(CONFIG_ZMK_WIDGET_LAYER_STATUS) +static struct zmk_widget_layer_status layer_status_widget; +#endif + lv_obj_t *zmk_display_status_screen() { lv_obj_t *screen; - lv_obj_t *zmk_version_label; screen = lv_obj_create(NULL, NULL); - zmk_version_label = lv_label_create(screen, NULL); - #if IS_ENABLED(CONFIG_ZMK_WIDGET_BATTERY_STATUS) zmk_widget_battery_status_init(&battery_status_widget, screen); lv_obj_align(zmk_widget_battery_status_obj(&battery_status_widget), NULL, LV_ALIGN_IN_TOP_RIGHT, @@ -39,8 +41,11 @@ lv_obj_t *zmk_display_status_screen() { 0); #endif - lv_label_set_text(zmk_version_label, "ZMK v0.1.0"); - lv_obj_align(zmk_version_label, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0); +#if IS_ENABLED(CONFIG_ZMK_WIDGET_LAYER_STATUS) + zmk_widget_layer_status_init(&layer_status_widget, screen); + lv_obj_align(zmk_widget_layer_status_obj(&layer_status_widget), NULL, LV_ALIGN_IN_BOTTOM_LEFT, + 0, 0); +#endif return screen; } diff --git a/app/src/display/widgets/CMakeLists.txt b/app/src/display/widgets/CMakeLists.txt index 1d4cca1f..ad7e132a 100644 --- a/app/src/display/widgets/CMakeLists.txt +++ b/app/src/display/widgets/CMakeLists.txt @@ -3,3 +3,4 @@ target_sources_ifdef(CONFIG_ZMK_WIDGET_BATTERY_STATUS app PRIVATE battery_status.c) target_sources_ifdef(CONFIG_ZMK_WIDGET_OUTPUT_STATUS app PRIVATE output_status.c) +target_sources_ifdef(CONFIG_ZMK_WIDGET_LAYER_STATUS app PRIVATE layer_status.c) diff --git a/app/src/display/widgets/Kconfig b/app/src/display/widgets/Kconfig index 01bc587e..bdb3024e 100644 --- a/app/src/display/widgets/Kconfig +++ b/app/src/display/widgets/Kconfig @@ -3,6 +3,12 @@ menu "ZMK Display Widgets" +config ZMK_WIDGET_LAYER_STATUS + bool "Widget for highest, active layer using small icons" + default y + depends on !ZMK_SPLIT || ZMK_SPLIT_BLE_ROLE_CENTRAL + select LVGL_FONT_MONTSERRAT_12 + config ZMK_WIDGET_BATTERY_STATUS bool "Widget for battery charge information, using small icons" depends on BT diff --git a/app/src/display/widgets/layer_status.c b/app/src/display/widgets/layer_status.c new file mode 100644 index 00000000..fb9f6897 --- /dev/null +++ b/app/src/display/widgets/layer_status.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include +#include +#include +#include +#include + +static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); +static lv_style_t label_style; + +static bool style_initialized = false; + +void layer_status_init() { + if (style_initialized) { + return; + } + + style_initialized = true; + lv_style_init(&label_style); + lv_style_set_text_color(&label_style, LV_STATE_DEFAULT, LV_COLOR_BLACK); + lv_style_set_text_font(&label_style, LV_STATE_DEFAULT, &lv_font_montserrat_12); + lv_style_set_text_letter_space(&label_style, LV_STATE_DEFAULT, 1); + lv_style_set_text_line_space(&label_style, LV_STATE_DEFAULT, 1); +} + +void set_layer_symbol(lv_obj_t *label) { + int active_layer_index = zmk_keymap_highest_layer_active(); + char text[6] = {}; + + LOG_DBG("Layer changed to %i", active_layer_index); + sprintf(text, LV_SYMBOL_KEYBOARD "%i ", active_layer_index); + + lv_label_set_text(label, text); +} + +int zmk_widget_layer_status_init(struct zmk_widget_layer_status *widget, lv_obj_t *parent) { + layer_status_init(); + widget->obj = lv_label_create(parent, NULL); + lv_obj_add_style(widget->obj, LV_LABEL_PART_MAIN, &label_style); + + lv_obj_set_size(widget->obj, 40, 15); + set_layer_symbol(widget->obj); + + sys_slist_append(&widgets, &widget->node); + + return 0; +} + +lv_obj_t *zmk_widget_layer_status_obj(struct zmk_widget_layer_status *widget) { + return widget->obj; +} + +int layer_status_listener(const struct zmk_event_header *eh) { + struct zmk_widget_layer_status *widget; + SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_layer_symbol(widget->obj); } + return 0; +} + +ZMK_LISTENER(widget_layer_status, layer_status_listener) +ZMK_SUBSCRIPTION(widget_layer_status, layer_state_changed); \ No newline at end of file