From 99f932a47d4705c7ddde83256fada9aa7aa3bda6 Mon Sep 17 00:00:00 2001 From: KemoNine Date: Tue, 15 Dec 2020 06:00:52 +0000 Subject: [PATCH] (feat) Allow setting underglow color by key press --- app/dts/behaviors/rgb_underglow.dtsi | 2 +- .../behaviors/zmk,behavior-rgb-underglow.yaml | 2 +- app/include/dt-bindings/zmk/rgb.h | 37 +++++++++++++------ app/include/zmk/rgb_underglow.h | 1 + app/src/behaviors/behavior_rgb_underglow.c | 25 +++++++------ app/src/rgb_underglow.c | 13 +++++++ 6 files changed, 56 insertions(+), 24 deletions(-) diff --git a/app/dts/behaviors/rgb_underglow.dtsi b/app/dts/behaviors/rgb_underglow.dtsi index 22aff93f..8b88f8c8 100644 --- a/app/dts/behaviors/rgb_underglow.dtsi +++ b/app/dts/behaviors/rgb_underglow.dtsi @@ -9,7 +9,7 @@ rgb_ug: behavior_rgb_underglow { compatible = "zmk,behavior-rgb-underglow"; label = "RGB_UNDERGLOW"; - #binding-cells = <1>; + #binding-cells = <2>; }; }; }; diff --git a/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml b/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml index 2cb74b9c..d301998a 100644 --- a/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml +++ b/app/dts/bindings/behaviors/zmk,behavior-rgb-underglow.yaml @@ -5,4 +5,4 @@ description: RGB Underglow Action compatible: "zmk,behavior-rgb-underglow" -include: one_param.yaml +include: two_param.yaml diff --git a/app/include/dt-bindings/zmk/rgb.h b/app/include/dt-bindings/zmk/rgb.h index eb721807..1f7b44ec 100644 --- a/app/include/dt-bindings/zmk/rgb.h +++ b/app/include/dt-bindings/zmk/rgb.h @@ -4,14 +4,29 @@ * SPDX-License-Identifier: MIT */ -#define RGB_TOG 0 -#define RGB_HUI 1 -#define RGB_HUD 2 -#define RGB_SAI 3 -#define RGB_SAD 4 -#define RGB_BRI 5 -#define RGB_BRD 6 -#define RGB_SPI 7 -#define RGB_SPD 8 -#define RGB_EFF 9 -#define RGB_EFR 10 +#define RGB_TOG_CMD 0 +#define RGB_HUI_CMD 1 +#define RGB_HUD_CMD 2 +#define RGB_SAI_CMD 3 +#define RGB_SAD_CMD 4 +#define RGB_BRI_CMD 5 +#define RGB_BRD_CMD 6 +#define RGB_SPI_CMD 7 +#define RGB_SPD_CMD 8 +#define RGB_EFF_CMD 9 +#define RGB_EFR_CMD 10 +#define RGB_COLOR_HSB_CMD 11 + +#define RGB_TOG RGB_TOG_CMD 0 +#define RGB_HUI RGB_HUI_CMD 0 +#define RGB_HUD RGB_HUD_CMD 0 +#define RGB_SAI RGB_SAI_CMD 0 +#define RGB_SAD RGB_SAD_CMD 0 +#define RGB_BRI RGB_BRI_CMD 0 +#define RGB_BRD RGB_BRD_CMD 0 +#define RGB_SPI RGB_SPI_CMD 0 +#define RGB_SPD RGB_SPD_CMD 0 +#define RGB_EFF RGB_EFF_CMD 0 +#define RGB_EFR RGB_EFR_CMD 0 +#define RGB_COLOR_HSB(h, s, v) RGB_COLOR_HSB_CMD(((h) << 16) + ((s) << 8) + (v)) +#define RGB_COLOR_HSV RGB_COLOR_HSB \ No newline at end of file diff --git a/app/include/zmk/rgb_underglow.h b/app/include/zmk/rgb_underglow.h index 94cc32cc..7fcd42dd 100644 --- a/app/include/zmk/rgb_underglow.h +++ b/app/include/zmk/rgb_underglow.h @@ -12,3 +12,4 @@ int zmk_rgb_underglow_change_hue(int direction); int zmk_rgb_underglow_change_sat(int direction); int zmk_rgb_underglow_change_brt(int direction); int zmk_rgb_underglow_change_spd(int direction); +int zmk_rgb_underglow_set_hsb(uint16_t hue, uint8_t saturation, uint8_t brightness); \ No newline at end of file diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c index 399123a3..07f24940 100644 --- a/app/src/behaviors/behavior_rgb_underglow.c +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -21,28 +21,31 @@ static int behavior_rgb_underglow_init(const struct device *dev) { return 0; } static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { switch (binding->param1) { - case RGB_TOG: + case RGB_TOG_CMD: return zmk_rgb_underglow_toggle(); - case RGB_HUI: + case RGB_HUI_CMD: return zmk_rgb_underglow_change_hue(1); - case RGB_HUD: + case RGB_HUD_CMD: return zmk_rgb_underglow_change_hue(-1); - case RGB_SAI: + case RGB_SAI_CMD: return zmk_rgb_underglow_change_sat(1); - case RGB_SAD: + case RGB_SAD_CMD: return zmk_rgb_underglow_change_sat(-1); - case RGB_BRI: + case RGB_BRI_CMD: return zmk_rgb_underglow_change_brt(1); - case RGB_BRD: + case RGB_BRD_CMD: return zmk_rgb_underglow_change_brt(-1); - case RGB_SPI: + case RGB_SPI_CMD: return zmk_rgb_underglow_change_spd(1); - case RGB_SPD: + case RGB_SPD_CMD: return zmk_rgb_underglow_change_spd(-1); - case RGB_EFF: + case RGB_EFF_CMD: return zmk_rgb_underglow_cycle_effect(1); - case RGB_EFR: + case RGB_EFR_CMD: return zmk_rgb_underglow_cycle_effect(-1); + case RGB_COLOR_HSB_CMD: + return zmk_rgb_underglow_set_hsb((binding->param2 >> 16) & 0xFFFF, + (binding->param2 >> 8) & 0xFF, binding->param2 & 0xFF); } return -ENOTSUP; diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 73a5c5e9..a2c02ed8 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -346,6 +346,19 @@ int zmk_rgb_underglow_toggle() { return zmk_rgb_underglow_save_state(); } +int zmk_rgb_underglow_set_hsb(uint16_t hue, uint8_t saturation, uint8_t brightness) { + if (hue > 360 || saturation > 100 || brightness > 100) { + return -ENOTSUP; + } + + state.hue = hue; + state.saturation = saturation; + state.brightness = brightness; + state.current_effect = UNDERGLOW_EFFECT_SOLID; + + return 0; +} + int zmk_rgb_underglow_change_hue(int direction) { if (!led_strip) return -ENODEV;