From 11e19b3490fbce0d9a7cb646ed3a6de749b79b49 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Tue, 2 Nov 2021 21:21:16 +0000 Subject: [PATCH 01/14] initial rgb auto off port --- app/Kconfig | 9 +++++- app/src/rgb_underglow.c | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index 3502c652757..dfe56ad00c6 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -317,6 +317,14 @@ config ZMK_RGB_UNDERGLOW_ON_START bool "RGB underglow starts on by default" default y +config ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE + bool "Turn off RGB underglow when keyboard goes into idle state" + default y + +config ZMK_RGB_UNDERGLOW_AUTO_OFF_USB + bool "Turn off RGB underglow when USB is disconnected" + default n + #ZMK_RGB_UNDERGLOW endif @@ -509,4 +517,3 @@ osource "$(ZMK_CONFIG)/boards/shields/*/Kconfig.shield" source "Kconfig.zephyr" - diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 40d99c7d15d..fe9670cb2f7 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -19,6 +19,12 @@ #include +#include +#include +#include +#include +#include + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define STRIP_LABEL DT_LABEL(DT_CHOSEN(zmk_underglow)) @@ -434,4 +440,66 @@ int zmk_rgb_underglow_change_spd(int direction) { return zmk_rgb_underglow_save_state(); } +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) +static bool auto_off_idle_prev_state = false; +#endif + +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +static bool auto_off_usb_prev_state = false; +#endif + +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +static int rgb_underglow_event_listener(const zmk_event_t *eh) { + +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) + if (as_zmk_activity_state_changed(eh)) { + bool new_state = (zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); + if (state.on == new_state) { + return 0; + } + if (new_state) { + state.on = auto_off_idle_prev_state; + auto_off_idle_prev_state = false; + return zmk_rgb_underglow_on(); + } else { + state.on = false; + auto_off_idle_prev_state = true; + return zmk_rgb_underglow_off(); + } + } +#endif + +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) + if (as_zmk_usb_conn_state_changed(eh)) { + bool new_state = zmk_usb_is_powered(); + if (state.on == new_state) { + return 0; + } + if (new_state) { + state.on = auto_off_usb_prev_state; + auto_off_usb_prev_state = false; + return zmk_rgb_underglow_on(); + } else { + state.on = false; + auto_off_usb_prev_state = true; + return zmk_rgb_underglow_off(); + } + } +#endif + + return -ENOTSUP; +} + +ZMK_LISTENER(rgb_underglow, rgb_underglow_event_listener); +#endif // IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || + // IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) + +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) +ZMK_SUBSCRIPTION(rgb_underglow, zmk_activity_state_changed); +#endif + +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +ZMK_SUBSCRIPTION(rgb_underglow, zmk_usb_conn_state_changed); +#endif + SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); From 7830285f849ead4ac55cc312d924ffc6f8381fa0 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Tue, 2 Nov 2021 21:56:38 +0000 Subject: [PATCH 02/14] fix(formatting): minimise changes to files --- app/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Kconfig b/app/Kconfig index dfe56ad00c6..b07cfa1e425 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -517,3 +517,4 @@ osource "$(ZMK_CONFIG)/boards/shields/*/Kconfig.shield" source "Kconfig.zephyr" + From ea6952b3529bc584719c9b53544f797bd3d75173 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Tue, 2 Nov 2021 22:24:57 +0000 Subject: [PATCH 03/14] fix(formatting): clang format --- app/src/rgb_underglow.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index fe9670cb2f7..1700b095938 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -448,7 +448,8 @@ static bool auto_off_idle_prev_state = false; static bool auto_off_usb_prev_state = false; #endif -#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || +IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) static int rgb_underglow_event_listener(const zmk_event_t *eh) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) From 5db27d1b44d26b53e83c152d108f8d0b9b8bc353 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Wed, 3 Nov 2021 10:13:32 +0000 Subject: [PATCH 04/14] clang --- app/src/rgb_underglow.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 1700b095938..f5ed2c16bad 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -448,8 +448,8 @@ static bool auto_off_idle_prev_state = false; static bool auto_off_usb_prev_state = false; #endif -#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || -IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || \ + IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) static int rgb_underglow_event_listener(const zmk_event_t *eh) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) From f1f04d27777c4218c4f410b224bc8f8c86e13575 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Mon, 8 Nov 2021 11:15:31 +0000 Subject: [PATCH 05/14] Update app/Kconfig Co-authored-by: Pete Johanson --- app/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index b07cfa1e425..214bf383598 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -323,7 +323,7 @@ config ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE config ZMK_RGB_UNDERGLOW_AUTO_OFF_USB bool "Turn off RGB underglow when USB is disconnected" - default n + depends on USB #ZMK_RGB_UNDERGLOW endif From 20a034fb3724455698a66ad0b2bb59ff6e41937b Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Sat, 13 Nov 2021 23:02:03 +0000 Subject: [PATCH 06/14] fix(underglow): function tweak --- app/src/rgb_underglow.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index f5ed2c16bad..c8b06f9cad9 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -450,41 +450,34 @@ static bool auto_off_usb_prev_state = false; #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || \ IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +static int rgb_underglow_auto_state(bool *prev_state, bool *new_state) { + if (state.on == *new_state) { + return 0; + } + if (new_state) { + state.on = *prev_state; + *prev_state = false; + return zmk_rgb_underglow_on(); + } else { + state.on = false; + *prev_state = true; + return zmk_rgb_underglow_off(); + } +} + static int rgb_underglow_event_listener(const zmk_event_t *eh) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) if (as_zmk_activity_state_changed(eh)) { bool new_state = (zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); - if (state.on == new_state) { - return 0; - } - if (new_state) { - state.on = auto_off_idle_prev_state; - auto_off_idle_prev_state = false; - return zmk_rgb_underglow_on(); - } else { - state.on = false; - auto_off_idle_prev_state = true; - return zmk_rgb_underglow_off(); - } + return rgb_underglow_auto_state(&auto_off_idle_prev_state, &new_state); } #endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) if (as_zmk_usb_conn_state_changed(eh)) { bool new_state = zmk_usb_is_powered(); - if (state.on == new_state) { - return 0; - } - if (new_state) { - state.on = auto_off_usb_prev_state; - auto_off_usb_prev_state = false; - return zmk_rgb_underglow_on(); - } else { - state.on = false; - auto_off_usb_prev_state = true; - return zmk_rgb_underglow_off(); - } + return rgb_underglow_auto_state(&auto_off_usb_prev_state, &new_state); } #endif From 91a3dfa68c304d5543bdd24080044adbdf9418ad Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Thu, 18 Nov 2021 17:52:56 +0000 Subject: [PATCH 07/14] feat(underglow): fix bug --- app/src/rgb_underglow.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index c8b06f9cad9..69838cf59aa 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -454,7 +454,7 @@ static int rgb_underglow_auto_state(bool *prev_state, bool *new_state) { if (state.on == *new_state) { return 0; } - if (new_state) { + if (*new_state) { state.on = *prev_state; *prev_state = false; return zmk_rgb_underglow_on(); From 6a7b56ebcd33e74eba98836a593d66c43f406665 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Tue, 1 Feb 2022 11:10:45 +0000 Subject: [PATCH 08/14] Update rgb_underglow.c --- app/src/rgb_underglow.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 69838cf59aa..8af922960b3 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -440,21 +440,13 @@ int zmk_rgb_underglow_change_spd(int direction) { return zmk_rgb_underglow_save_state(); } -#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) -static bool auto_off_idle_prev_state = false; -#endif - -#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) -static bool auto_off_usb_prev_state = false; -#endif - #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) || \ IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) -static int rgb_underglow_auto_state(bool *prev_state, bool *new_state) { - if (state.on == *new_state) { +static int rgb_underglow_auto_state(bool *prev_state, bool new_state) { + if (state.on == new_state) { return 0; } - if (*new_state) { + if (new_state) { state.on = *prev_state; *prev_state = false; return zmk_rgb_underglow_on(); @@ -469,15 +461,16 @@ static int rgb_underglow_event_listener(const zmk_event_t *eh) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) if (as_zmk_activity_state_changed(eh)) { - bool new_state = (zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); - return rgb_underglow_auto_state(&auto_off_idle_prev_state, &new_state); + static bool prev_state = false; + return rgb_underglow_auto_state(&prev_state, + zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); } #endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) if (as_zmk_usb_conn_state_changed(eh)) { - bool new_state = zmk_usb_is_powered(); - return rgb_underglow_auto_state(&auto_off_usb_prev_state, &new_state); + static bool prev_state = false; + return rgb_underglow_auto_state(&prev_state, zmk_usb_is_powered()); } #endif From d5cc804176144107c50d245f38b48d689f8704ee Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Sun, 20 Feb 2022 15:17:01 +0000 Subject: [PATCH 09/14] Update app/Kconfig Co-authored-by: Pete Johanson --- app/Kconfig | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index 214bf383598..7c8fd5b6fce 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -319,7 +319,6 @@ config ZMK_RGB_UNDERGLOW_ON_START config ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE bool "Turn off RGB underglow when keyboard goes into idle state" - default y config ZMK_RGB_UNDERGLOW_AUTO_OFF_USB bool "Turn off RGB underglow when USB is disconnected" From 590c0c6e4fee0eb152ddb74588bec9de0170078f Mon Sep 17 00:00:00 2001 From: ReFil Date: Thu, 23 Jun 2022 11:19:12 +0100 Subject: [PATCH 10/14] Z3 fixes and tweaks --- app/Kconfig | 2 +- app/src/rgb_underglow.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index b586f7fc015..02c0205eb23 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -330,7 +330,7 @@ config ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE config ZMK_RGB_UNDERGLOW_AUTO_OFF_USB bool "Turn off RGB underglow when USB is disconnected" - depends on USB + depends on USB_DEVICE_STACK #ZMK_RGB_UNDERGLOW endif diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 60fcbadc73c..6b9b477e500 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -271,6 +271,11 @@ static int zmk_rgb_underglow_init(const struct device *_arg) { settings_load_subtree("rgb/underglow"); #endif +#if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) + if(!zmk_usb_is_powered()) + state.on = false; +#endif + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); return 0; From badf3271a2b46ed904c83f4eb194541de8c45d3a Mon Sep 17 00:00:00 2001 From: ReFil Date: Thu, 23 Jun 2022 11:20:53 +0100 Subject: [PATCH 11/14] Clang format --- app/src/rgb_underglow.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 6b9b477e500..25a17aa7823 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -272,7 +272,7 @@ static int zmk_rgb_underglow_init(const struct device *_arg) { #endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) - if(!zmk_usb_is_powered()) + if (!zmk_usb_is_powered()) state.on = false; #endif From 75750ec1c1ca4bc9f1dc359e9c4f1f6dc11f05d1 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Thu, 23 Jun 2022 14:04:17 +0000 Subject: [PATCH 12/14] Update app/src/rgb_underglow.c Co-authored-by: Pete Johanson --- app/src/rgb_underglow.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 25a17aa7823..c5315504c37 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -272,8 +272,7 @@ static int zmk_rgb_underglow_init(const struct device *_arg) { #endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) - if (!zmk_usb_is_powered()) - state.on = false; + state.on = zmk_usb_is_powered(); #endif k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); From 497b0a2256a061cb748cdfab320952370bf3bba1 Mon Sep 17 00:00:00 2001 From: ReFil Date: Thu, 23 Jun 2022 15:48:06 +0100 Subject: [PATCH 13/14] Start timer only when appropriate --- app/src/rgb_underglow.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index c5315504c37..e6d22c484c9 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -273,9 +273,11 @@ static int zmk_rgb_underglow_init(const struct device *_arg) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) state.on = zmk_usb_is_powered(); -#endif - + if (state.on) + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); +#else k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); +#endif return 0; } From 111a6daecd70a94ae4aec38cb976ddab8706c9de Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Sat, 25 Jun 2022 11:31:59 +0000 Subject: [PATCH 14/14] Update app/src/rgb_underglow.c Co-authored-by: Pete Johanson --- app/src/rgb_underglow.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index e6d22c484c9..a5c15503ea9 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -273,12 +273,12 @@ static int zmk_rgb_underglow_init(const struct device *_arg) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) state.on = zmk_usb_is_powered(); - if (state.on) - k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); -#else - k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); #endif + if (state.on) { + k_timer_start(&underglow_tick, K_NO_WAIT, K_MSEC(50)); + } + return 0; }