From 7dd4ff73c3af37f14e28dc99279058279e14de12 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Sun, 18 Aug 2024 21:25:41 -0700 Subject: [PATCH] Add header and expose indicate methods --- CMakeLists.txt | 4 +- leds.c => src/widget.c | 92 ++++++++++++++++++------------------------ zephyr/module.yml | 2 + 3 files changed, 44 insertions(+), 54 deletions(-) rename leds.c => src/widget.c (90%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 706c2a3..ede1115 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1 +1,3 @@ -target_sources_ifdef(CONFIG_RGBLED_WIDGET app PRIVATE leds.c) +target_sources_ifdef(CONFIG_RGBLED_WIDGET app PRIVATE src/widget.c) + +zephyr_include_directories(include) diff --git a/leds.c b/src/widget.c similarity index 90% rename from leds.c rename to src/widget.c index f7c5358..c8c38b7 100644 --- a/leds.c +++ b/src/widget.c @@ -16,14 +16,12 @@ #include +#include + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define LED_GPIO_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(gpio_leds) -#define SHOW_LAYER_CHANGE \ - (IS_ENABLED(CONFIG_RGBLED_WIDGET_SHOW_LAYER_CHANGE)) && \ - (!IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)) - BUILD_ASSERT(DT_NODE_EXISTS(DT_ALIAS(led_red)), "An alias for a red LED is not found for RGBLED_WIDGET"); BUILD_ASSERT(DT_NODE_EXISTS(DT_ALIAS(led_green)), @@ -40,32 +38,12 @@ static const uint8_t rgb_idx[] = {DT_NODE_CHILD_IDX(DT_ALIAS(led_red)), // flag to indicate whether the initial boot up sequence is complete static bool initialized = false; -// color values as specified by an RGB bitfield -enum color_t { - LED_BLACK, // 0b000 - LED_RED, // 0b001 - LED_GREEN, // 0b010 - LED_YELLOW, // 0b011 - LED_BLUE, // 0b100 - LED_MAGENTA, // 0b101 - LED_CYAN, // 0b110 - LED_WHITE // 0b111 -}; - -// a blink work item as specified by the color and duration -struct blink_item { - enum color_t color; - uint16_t duration_ms; - bool first_item; - uint16_t sleep_ms; -}; - // define message queue of blink work items, that will be processed by a // separate thread K_MSGQ_DEFINE(led_msgq, sizeof(struct blink_item), 16, 1); #if IS_ENABLED(CONFIG_ZMK_BLE) -static void output_blink(void) { +void indicate_connectivity(void) { struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_OUTPUT_BLINK_MS}; #if !IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) @@ -95,7 +73,7 @@ static void output_blink(void) { static int led_output_listener_cb(const zmk_event_t *eh) { if (initialized) { - output_blink(); + indicate_connectivity(); } return 0; } @@ -111,6 +89,33 @@ ZMK_SUBSCRIPTION(led_output_listener, zmk_split_peripheral_status_changed); #endif // IS_ENABLED(CONFIG_ZMK_BLE) #if IS_ENABLED(CONFIG_ZMK_BATTERY_REPORTING) +void indicate_battery(void) { + struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS, + .first_item = true}; + uint8_t battery_level = zmk_battery_state_of_charge(); + int retry = 0; + while (battery_level == 0 && retry++ < 10) { + k_sleep(K_MSEC(100)); + battery_level = zmk_battery_state_of_charge(); + }; + + if (battery_level == 0) { + LOG_INF("Battery level undetermined (zero), blinking magenta"); + blink.color = LED_MAGENTA; + } else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_HIGH) { + LOG_INF("Battery level %d, blinking green", battery_level); + blink.color = LED_GREEN; + } else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_LOW) { + LOG_INF("Battery level %d, blinking yellow", battery_level); + blink.color = LED_YELLOW; + } else { + LOG_INF("Battery level %d, blinking red", battery_level); + blink.color = LED_RED; + } + + k_msgq_put(&led_msgq, &blink, K_NO_WAIT); +} + static int led_battery_listener_cb(const zmk_event_t *eh) { if (!initialized) { return 0; @@ -145,7 +150,11 @@ static int led_layer_listener_cb(const zmk_event_t *eh) { return 0; } -static void indicate_layer(struct k_work *work) { +static void indicate_layer_cb(struct k_work *work) { + indicate_layer(); +} + +void indicate_layer(void) { uint8_t index = zmk_keymap_highest_layer_active(); static const struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_LAYER_BLINK_MS, .color = LED_CYAN, @@ -171,7 +180,7 @@ extern void led_process_thread(void *d0, void *d1, void *d2) { ARG_UNUSED(d2); #if SHOW_LAYER_CHANGE - k_work_init_delayable(&layer_indicate_work, indicate_layer); + k_work_init_delayable(&layer_indicate_work, indicate_layer_cb); #endif while (true) { @@ -221,30 +230,7 @@ extern void led_init_thread(void *d0, void *d1, void *d2) { // check and indicate battery level on thread start LOG_INF("Indicating initial battery status"); - struct blink_item blink = {.duration_ms = CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS, - .first_item = true}; - uint8_t battery_level = zmk_battery_state_of_charge(); - int retry = 0; - while (battery_level == 0 && retry++ < 10) { - k_sleep(K_MSEC(100)); - battery_level = zmk_battery_state_of_charge(); - }; - - if (battery_level == 0) { - LOG_INF("Battery level undetermined (zero), blinking magenta"); - blink.color = LED_MAGENTA; - } else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_HIGH) { - LOG_INF("Battery level %d, blinking green", battery_level); - blink.color = LED_GREEN; - } else if (battery_level >= CONFIG_RGBLED_WIDGET_BATTERY_LEVEL_LOW) { - LOG_INF("Battery level %d, blinking yellow", battery_level); - blink.color = LED_YELLOW; - } else { - LOG_INF("Battery level %d, blinking red", battery_level); - blink.color = LED_RED; - } - - k_msgq_put(&led_msgq, &blink, K_NO_WAIT); + indicate_battery(); // wait until blink should be displayed for further checks k_sleep(K_MSEC(CONFIG_RGBLED_WIDGET_BATTERY_BLINK_MS + CONFIG_RGBLED_WIDGET_INTERVAL_MS)); @@ -253,7 +239,7 @@ extern void led_init_thread(void *d0, void *d1, void *d2) { #if IS_ENABLED(CONFIG_ZMK_BLE) // check and indicate current profile or peripheral connectivity status LOG_INF("Indicating initial connectivity status"); - output_blink(); + indicate_connectivity(); #endif // IS_ENABLED(CONFIG_ZMK_BLE) initialized = true; diff --git a/zephyr/module.yml b/zephyr/module.yml index 3b84067..a022118 100644 --- a/zephyr/module.yml +++ b/zephyr/module.yml @@ -1,5 +1,7 @@ +name: zmk-rgbled-widget build: cmake: . kconfig: Kconfig settings: board_root: . + dts_root: .