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] 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 3502c652..dfe56ad0 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 40d99c7d..fe9670cb 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);