From c5b8dd85fdf28d1912ef0d2cefce8b55652c0db7 Mon Sep 17 00:00:00 2001 From: Simon Malinge Date: Thu, 7 Oct 2021 03:54:49 +0000 Subject: [PATCH] feat(underglow): Add support for configurable min/max brightness Co-authored-by: jrhrsmit --- app/Kconfig | 12 +++++++++++- app/src/rgb_underglow.c | 29 +++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 35bf77da..fa168242 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -209,7 +209,7 @@ endif endif #ZMK_SPLIT -endif +endif if ZMK_BLE @@ -262,6 +262,16 @@ config ZMK_RGB_UNDERGLOW_EXT_POWER bool "RGB underglow toggling also controls external power" default y +config ZMK_RGB_UNDERGLOW_BRT_MIN + int "RGB underglow minimum brightness in percent" + range 0 100 + default 0 + +config ZMK_RGB_UNDERGLOW_BRT_MAX + int "RGB underglow maximum brightness in percent" + range 0 100 + default 100 + config ZMK_RGB_UNDERGLOW_HUE_STEP int "RGB underglow hue step in degrees of 360" default 10 diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 5f38aba7..73826180 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -28,6 +28,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define SAT_MAX 100 #define BRT_MAX 100 +BUILD_ASSERT(CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN <= CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX, + "ERROR: RGB underglow maximum brightness is less than minimum brightness"); + enum rgb_underglow_effect { UNDERGLOW_EFFECT_SOLID, UNDERGLOW_EFFECT_BREATHE, @@ -54,6 +57,17 @@ static struct rgb_underglow_state state; static const struct device *ext_power; #endif +static struct zmk_led_hsb hsb_scale_min_max(struct zmk_led_hsb hsb) { + hsb.b = CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN + + (CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX - CONFIG_ZMK_RGB_UNDERGLOW_BRT_MIN) * hsb.b / BRT_MAX; + return hsb; +} + +static struct zmk_led_hsb hsb_scale_zero_max(struct zmk_led_hsb hsb) { + hsb.b = hsb.b * CONFIG_ZMK_RGB_UNDERGLOW_BRT_MAX / BRT_MAX; + return hsb; +} + static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) { double r, g, b; @@ -105,7 +119,7 @@ static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) { static void zmk_rgb_underglow_effect_solid() { for (int i = 0; i < STRIP_NUM_PIXELS; i++) { - pixels[i] = hsb_to_rgb(state.color); + pixels[i] = hsb_to_rgb(hsb_scale_min_max(state.color)); } } @@ -114,7 +128,7 @@ static void zmk_rgb_underglow_effect_breathe() { struct zmk_led_hsb hsb = state.color; hsb.b = abs(state.animation_step - 1200) / 12; - pixels[i] = hsb_to_rgb(hsb); + pixels[i] = hsb_to_rgb(hsb_scale_zero_max(hsb)); } state.animation_step += state.animation_speed * 10; @@ -129,7 +143,7 @@ static void zmk_rgb_underglow_effect_spectrum() { struct zmk_led_hsb hsb = state.color; hsb.h = state.animation_step; - pixels[i] = hsb_to_rgb(hsb); + pixels[i] = hsb_to_rgb(hsb_scale_min_max(hsb)); } state.animation_step += state.animation_speed; @@ -141,7 +155,7 @@ static void zmk_rgb_underglow_effect_swirl() { struct zmk_led_hsb hsb = state.color; hsb.h = (HUE_MAX / STRIP_NUM_PIXELS * i + state.animation_step) % HUE_MAX; - pixels[i] = hsb_to_rgb(hsb); + pixels[i] = hsb_to_rgb(hsb_scale_min_max(hsb)); } state.animation_step += state.animation_speed * 2; @@ -371,12 +385,7 @@ struct zmk_led_hsb zmk_rgb_underglow_calc_brt(int direction) { struct zmk_led_hsb color = state.color; int b = color.b + (direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP); - if (b < 0) { - b = 0; - } else if (b > BRT_MAX) { - b = BRT_MAX; - } - color.b = b; + color.b = CLAMP(b, 0, BRT_MAX); return color; }