diff --git a/app/include/zmk/rgb_underglow_layer.h b/app/include/zmk/rgb_underglow_layer.h index ad28f74f..3681a97e 100644 --- a/app/include/zmk/rgb_underglow_layer.h +++ b/app/include/zmk/rgb_underglow_layer.h @@ -7,7 +7,6 @@ #pragma once #include - #define ZMK_RGB_CHILD_LEN_PLUS_ONE(node) 1 + #define ZMK_RGBMAP_LAYERS_LEN \ @@ -15,4 +14,5 @@ const int zmk_rgbmap_id(uint8_t layer); uint32_t *rgb_underglow_get_bindings(uint8_t layer); -uint8_t rgb_underglow_top_layer_with_state(uint32_t state_to_test); \ No newline at end of file +uint8_t rgb_underglow_top_layer_with_state(uint32_t state_to_test); +uint8_t rgb_underglow_top_layer(void); \ No newline at end of file diff --git a/app/include/zmk/split/bluetooth/peripheral_layers.h b/app/include/zmk/split/bluetooth/peripheral_layers.h index e816cad0..974a322c 100644 --- a/app/include/zmk/split/bluetooth/peripheral_layers.h +++ b/app/include/zmk/split/bluetooth/peripheral_layers.h @@ -1,4 +1,5 @@ #pragma once void set_peripheral_layers_state(uint32_t new_layers); -bool peripheral_layer_active(uint8_t layer); \ No newline at end of file +bool peripheral_layer_active(uint8_t layer); +uint8_t peripheral_highest_layer_active(void); \ No newline at end of file diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 981d21ae..5f3959a7 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -52,6 +52,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #endif +#if DT_HAS_COMPAT_STATUS_OKAY(zmk_underglow_layer) +#define UNDERGLOW_LAYER_ENABLED +#endif + #define STRIP_CHOSEN DT_CHOSEN(zmk_underglow) #define STRIP_NUM_PIXELS DT_PROP(STRIP_CHOSEN, chain_length) @@ -437,6 +441,8 @@ static inline struct led_rgb hue_sat(int hue, int sat) { return hsb_to_rgb(hsb_scale_min_max(hsb)); } +#ifdef UNDERGLOW_LAYER_ENABLED + static struct led_rgb hex_to_rgb(uint8_t r, uint8_t g, uint8_t b) { struct zmk_led_hsb hsb = state.color; return (struct led_rgb){ @@ -482,6 +488,7 @@ static void zmk_rgb_underglow_set_layer(uint8_t layer) { zmk_led_write_pixels(); zmk_rgb_set_ext_power(); } +#endif /* UNDERGLOW_LAYER_ENABLED */ static void zmk_rgb_underglow_tick(struct k_work *work) { switch (state.current_effect) { @@ -498,7 +505,7 @@ static void zmk_rgb_underglow_tick(struct k_work *work) { zmk_rgb_underglow_effect_swirl(); break; case UNDERGLOW_EFFECT_LAYER_INDICATORS: - //zmk_rgb_underglow_set_layer(); + // zmk_rgb_underglow_set_layer(); break; } @@ -849,6 +856,9 @@ static int rgb_underglow_auto_state(bool target_wake_state) { if (sleep_state.is_awake) { if (sleep_state.rgb_state_before_sleeping) { +#ifdef UNDERGLOW_LAYER_ENABLED + zmk_rgb_underglow_set_layer(rgb_underglow_top_layer()); +#endif return zmk_rgb_underglow_on(); } else { return zmk_rgb_underglow_off(); @@ -865,16 +875,22 @@ static int rgb_underglow_event_listener(const zmk_event_t *eh) { if (as_zmk_activity_state_changed(eh)) { return rgb_underglow_auto_state(zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); } - if (as_zmk_split_peripheral_layer_changed(eh)) { - const struct zmk_split_peripheral_layer_changed *ev = as_zmk_split_peripheral_layer_changed(eh); - LOG_DBG("zmk_split_peripheral_layer_changed: %08x", ev->layers); +#endif - uint8_t layer = rgb_underglow_top_layer_with_state(ev->layers); +#ifdef UNDERGLOW_LAYER_ENABLED + if (as_zmk_split_peripheral_layer_changed(eh)) { + const struct zmk_split_peripheral_layer_changed *ev = + as_zmk_split_peripheral_layer_changed(eh); + LOG_DBG("zmk_split_peripheral_layer_changed: %08x", ev->layers); +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) + set_peripheral_layers_state(ev->layers); +#endif + uint8_t layer = rgb_underglow_top_layer(); LOG_DBG("top layer: %d", layer); zmk_rgb_underglow_set_layer(layer); return 0; } -#endif +#endif /* UNDERGLOW_LAYER_ENABLED */ #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) if (as_zmk_usb_conn_state_changed(eh)) { @@ -891,8 +907,10 @@ ZMK_LISTENER(rgb_underglow, rgb_underglow_event_listener); #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) ZMK_SUBSCRIPTION(rgb_underglow, zmk_activity_state_changed); +#ifdef UNDERGLOW_LAYER_ENABLED ZMK_SUBSCRIPTION(rgb_underglow, zmk_split_peripheral_layer_changed); #endif +#endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) ZMK_SUBSCRIPTION(rgb_underglow, zmk_usb_conn_state_changed); diff --git a/app/src/rgb_underglow_layer.c b/app/src/rgb_underglow_layer.c index 28dfe26b..fb9876cf 100644 --- a/app/src/rgb_underglow_layer.c +++ b/app/src/rgb_underglow_layer.c @@ -20,17 +20,16 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #endif #define DT_DRV_COMPAT zmk_underglow_layer +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) +#define UNDERGLOW_LAYER_ENABLED #define LAYER_ID(node) DT_PROP(node, layer_id) #define RGB_BINDINGS(node) DT_PROP(node, bindings) static uint32_t zmk_rgbmap[ZMK_RGBMAP_LAYERS_LEN][ZMK_KEYMAP_LEN] = { - DT_INST_FOREACH_CHILD_SEP(0, RGB_BINDINGS, (, )) -}; - -static int zmk_rgbmap_ids[ZMK_RGBMAP_LAYERS_LEN] = { - DT_INST_FOREACH_CHILD_SEP(0, LAYER_ID, (, ))}; + DT_INST_FOREACH_CHILD_SEP(0, RGB_BINDINGS, (, ))}; +static int zmk_rgbmap_ids[ZMK_RGBMAP_LAYERS_LEN] = {DT_INST_FOREACH_CHILD_SEP(0, LAYER_ID, (, ))}; const int zmk_rgbmap_id(uint8_t layer) { for (uint8_t i = 0; i < ZMK_RGBMAP_LAYERS_LEN; i++) { @@ -43,7 +42,7 @@ const int zmk_rgbmap_id(uint8_t layer) { uint32_t *rgb_underglow_get_bindings(uint8_t layer) { int rgblayer = zmk_rgbmap_id(layer); - if (rgblayer == -1){ + if (rgblayer == -1) { return NULL; } else { return zmk_rgbmap[rgblayer]; @@ -59,3 +58,12 @@ uint8_t rgb_underglow_top_layer_with_state(uint32_t state_to_test) { // return default layer (0) return 0; } + +uint8_t rgb_underglow_top_layer(void) { +#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) + return zmk_keymap_highest_layer_active(); +#else + return peripheral_highest_layer_active(); +#endif +} +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ \ No newline at end of file diff --git a/app/src/split/bluetooth/peripheral_layers.c b/app/src/split/bluetooth/peripheral_layers.c index fbbffdd6..c5e8c68d 100644 --- a/app/src/split/bluetooth/peripheral_layers.c +++ b/app/src/split/bluetooth/peripheral_layers.c @@ -3,6 +3,7 @@ #include #include +#include static uint32_t peripheral_layers = 0; @@ -10,4 +11,15 @@ void set_peripheral_layers_state(uint32_t new_layers) { peripheral_layers = new_ bool peripheral_layer_active(uint8_t layer) { return (peripheral_layers & (BIT(layer))) == (BIT(layer)); -}; \ No newline at end of file +}; + +uint8_t peripheral_highest_layer_active(void) { + if (peripheral_layers > 0) { + for (uint8_t layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer > 0; layer--) { + if ((peripheral_layers & (BIT(layer))) == (BIT(layer)) || layer == 0) { + return layer; + } + } + } + return 0; +} \ No newline at end of file