From ca2ea82172f72c5071e924327eb3e89271d93542 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:24:24 +0000 Subject: [PATCH 1/2] feat(backlight): Add backlight state changed event Add a backlight state changed event whenever the backlight is updated --- app/CMakeLists.txt | 2 ++ app/include/zmk/backlight.h | 5 +++++ .../zmk/events/backlight_state_changed.h | 17 +++++++++++++++++ app/src/backlight.c | 11 ++++------- app/src/events/backlight_state_changed.c | 10 ++++++++++ 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 app/include/zmk/events/backlight_state_changed.h create mode 100644 app/src/events/backlight_state_changed.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 072d5672912..d23c29226a2 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -88,7 +88,9 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) endif() target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) + target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_backlight.c) +target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/events/backlight_state_changed.c) target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c) target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c) diff --git a/app/include/zmk/backlight.h b/app/include/zmk/backlight.h index af8fc76d03d..16f798fd0f8 100644 --- a/app/include/zmk/backlight.h +++ b/app/include/zmk/backlight.h @@ -6,6 +6,11 @@ #pragma once +struct zmk_backlight_state { + uint8_t brightness; + bool on; +}; + int zmk_backlight_on(void); int zmk_backlight_off(void); int zmk_backlight_toggle(void); diff --git a/app/include/zmk/events/backlight_state_changed.h b/app/include/zmk/events/backlight_state_changed.h new file mode 100644 index 00000000000..96eec2efc64 --- /dev/null +++ b/app/include/zmk/events/backlight_state_changed.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2024 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include + +struct zmk_backlight_state_changed { + struct zmk_backlight_state state; +}; + +ZMK_EVENT_DECLARE(zmk_backlight_state_changed); \ No newline at end of file diff --git a/app/src/backlight.c b/app/src/backlight.c index 22b730669ab..88d3a7389a9 100644 --- a/app/src/backlight.c +++ b/app/src/backlight.c @@ -19,6 +19,7 @@ #include #include #include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -34,13 +35,8 @@ static const struct device *const backlight_dev = DEVICE_DT_GET(DT_CHOSEN(zmk_ba #define BRT_MAX 100 -struct backlight_state { - uint8_t brightness; - bool on; -}; - -static struct backlight_state state = {.brightness = CONFIG_ZMK_BACKLIGHT_BRT_START, - .on = IS_ENABLED(CONFIG_ZMK_BACKLIGHT_ON_START)}; +static struct zmk_backlight_state state = {.brightness = CONFIG_ZMK_BACKLIGHT_BRT_START, + .on = IS_ENABLED(CONFIG_ZMK_BACKLIGHT_ON_START)}; static int zmk_backlight_update(void) { uint8_t brt = zmk_backlight_get_brt(); @@ -53,6 +49,7 @@ static int zmk_backlight_update(void) { return rc; } } + raise_zmk_backlight_state_changed((struct zmk_backlight_state_changed){.state = state}); return 0; } diff --git a/app/src/events/backlight_state_changed.c b/app/src/events/backlight_state_changed.c new file mode 100644 index 00000000000..8ec86b4c2ee --- /dev/null +++ b/app/src/events/backlight_state_changed.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2024 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +ZMK_EVENT_IMPL(zmk_backlight_state_changed); \ No newline at end of file From b1534efdf8cb7e8e577d52c8418b7032cd9372be Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:38:46 +0000 Subject: [PATCH 2/2] feat(underglow): Add underglow state changed event Add event triggered when underglow state is saved --- app/CMakeLists.txt | 1 + .../zmk/events/underglow_state_changed.h | 17 +++++++++++++++++ app/include/zmk/rgb_underglow.h | 8 ++++++++ app/src/events/underglow_state_changed.c | 10 ++++++++++ app/src/rgb_underglow.c | 19 ++++++++----------- 5 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 app/include/zmk/events/underglow_state_changed.h create mode 100644 app/src/events/underglow_state_changed.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d23c29226a2..a286f172101 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -88,6 +88,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) endif() target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) +target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/events/underglow_state_changed.c) target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_backlight.c) target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/events/backlight_state_changed.c) diff --git a/app/include/zmk/events/underglow_state_changed.h b/app/include/zmk/events/underglow_state_changed.h new file mode 100644 index 00000000000..f5762733eee --- /dev/null +++ b/app/include/zmk/events/underglow_state_changed.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2024 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include + +struct zmk_underglow_state_changed { + struct zmk_underglow_state state; +}; + +ZMK_EVENT_DECLARE(zmk_underglow_state_changed); \ No newline at end of file diff --git a/app/include/zmk/rgb_underglow.h b/app/include/zmk/rgb_underglow.h index be0ef252290..956dddf80ee 100644 --- a/app/include/zmk/rgb_underglow.h +++ b/app/include/zmk/rgb_underglow.h @@ -12,6 +12,14 @@ struct zmk_led_hsb { uint8_t b; }; +struct zmk_underglow_state { + struct zmk_led_hsb color; + uint8_t animation_speed; + uint8_t current_effect; + uint16_t animation_step; + bool on; +}; + int zmk_rgb_underglow_toggle(void); int zmk_rgb_underglow_get_state(bool *state); int zmk_rgb_underglow_on(void); diff --git a/app/src/events/underglow_state_changed.c b/app/src/events/underglow_state_changed.c new file mode 100644 index 00000000000..90886ee9788 --- /dev/null +++ b/app/src/events/underglow_state_changed.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2024 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +ZMK_EVENT_IMPL(zmk_underglow_state_changed); \ No newline at end of file diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 3453fb44e0d..67cbd56cf45 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -24,6 +24,7 @@ #include #include #include +#include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -52,19 +53,11 @@ enum rgb_underglow_effect { UNDERGLOW_EFFECT_NUMBER // Used to track number of underglow effects }; -struct rgb_underglow_state { - struct zmk_led_hsb color; - uint8_t animation_speed; - uint8_t current_effect; - uint16_t animation_step; - bool on; -}; - static const struct device *led_strip; static struct led_rgb pixels[STRIP_NUM_PIXELS]; -static struct rgb_underglow_state state; +static struct zmk_underglow_state state; #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_EXT_POWER) static const struct device *const ext_power = DEVICE_DT_GET(DT_INST(0, zmk_ext_power_generic)); @@ -190,7 +183,8 @@ static void zmk_rgb_underglow_tick(struct k_work *work) { zmk_rgb_underglow_effect_swirl(); break; } - + if (state.animation_step == 0) + raise_zmk_underglow_state_changed((struct zmk_underglow_state_changed){.state = state}); int err = led_strip_update_rgb(led_strip, pixels, STRIP_NUM_PIXELS); if (err < 0) { LOG_ERR("Failed to update the RGB strip (%d)", err); @@ -253,7 +247,7 @@ static int zmk_rgb_underglow_init(void) { } #endif - state = (struct rgb_underglow_state){ + state = (struct zmk_underglow_state){ color : { h : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START, s : CONFIG_ZMK_RGB_UNDERGLOW_SAT_START, @@ -277,10 +271,13 @@ static int zmk_rgb_underglow_init(void) { k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); } + raise_zmk_underglow_state_changed((struct zmk_underglow_state_changed){.state = state}); + return 0; } int zmk_rgb_underglow_save_state(void) { + raise_zmk_underglow_state_changed((struct zmk_underglow_state_changed){.state = state}); #if IS_ENABLED(CONFIG_SETTINGS) int ret = k_work_reschedule(&underglow_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE)); return MIN(ret, 0);