feat(underglow): Add support for configurable min/max brightness

Co-authored-by: jrhrsmit <jasper.770@gmail.com>
This commit is contained in:
Simon Malinge 2021-10-07 03:54:49 +00:00 committed by Nick Winans
parent 96fea949d5
commit c5b8dd85fd
2 changed files with 30 additions and 11 deletions

View file

@ -262,6 +262,16 @@ config ZMK_RGB_UNDERGLOW_EXT_POWER
bool "RGB underglow toggling also controls external power" bool "RGB underglow toggling also controls external power"
default y 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 config ZMK_RGB_UNDERGLOW_HUE_STEP
int "RGB underglow hue step in degrees of 360" int "RGB underglow hue step in degrees of 360"
default 10 default 10

View file

@ -28,6 +28,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#define SAT_MAX 100 #define SAT_MAX 100
#define BRT_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 { enum rgb_underglow_effect {
UNDERGLOW_EFFECT_SOLID, UNDERGLOW_EFFECT_SOLID,
UNDERGLOW_EFFECT_BREATHE, UNDERGLOW_EFFECT_BREATHE,
@ -54,6 +57,17 @@ static struct rgb_underglow_state state;
static const struct device *ext_power; static const struct device *ext_power;
#endif #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) { static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) {
double r, g, b; 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() { static void zmk_rgb_underglow_effect_solid() {
for (int i = 0; i < STRIP_NUM_PIXELS; i++) { 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; struct zmk_led_hsb hsb = state.color;
hsb.b = abs(state.animation_step - 1200) / 12; 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; 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; struct zmk_led_hsb hsb = state.color;
hsb.h = state.animation_step; 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; state.animation_step += state.animation_speed;
@ -141,7 +155,7 @@ static void zmk_rgb_underglow_effect_swirl() {
struct zmk_led_hsb hsb = state.color; struct zmk_led_hsb hsb = state.color;
hsb.h = (HUE_MAX / STRIP_NUM_PIXELS * i + state.animation_step) % HUE_MAX; 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; 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; struct zmk_led_hsb color = state.color;
int b = color.b + (direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP); int b = color.b + (direction * CONFIG_ZMK_RGB_UNDERGLOW_BRT_STEP);
if (b < 0) { color.b = CLAMP(b, 0, BRT_MAX);
b = 0;
} else if (b > BRT_MAX) {
b = BRT_MAX;
}
color.b = b;
return color; return color;
} }