feat(underglow): simplify calculations

Eliminates redundant calculations when all LEDs are the same color.
Switches to mostly integer calculations to improve calculation time.
This commit is contained in:
Kellen 2021-10-15 08:19:24 -07:00
parent 6f29453041
commit 9c64949081

View file

@ -68,67 +68,78 @@ static struct zmk_led_hsb hsb_scale_zero_max(struct zmk_led_hsb hsb) {
return hsb; return hsb;
} }
// Conversion algorithm taken from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB
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; struct led_rgb rgb;
uint8_t i = hsb.h / 60; uint32_t from_h = hsb.h;
double v = hsb.b / ((float)BRT_MAX); uint32_t from_s = hsb.s;
double s = hsb.s / ((float)SAT_MAX); uint32_t from_b = hsb.b;
double f = hsb.h / ((float)HUE_MAX) * 6 - i;
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
switch (i % 6) { // Scale brightness from [0-100] to [0-255]
uint32_t max_brightness = from_b * 2.55f;
uint32_t min_brightness = from_b * (100 - from_s) * 0.0255f;
uint32_t sector = from_h / 60;
uint32_t distance_into_sector = sector % 60;
uint32_t offset = (max_brightness - min_brightness) * distance_into_sector / 60;
switch (sector) {
case 0: case 0:
r = v; rgb.r = max_brightness;
g = t; rgb.g = min_brightness + offset;
b = p; rgb.b = min_brightness;
break; break;
case 1: case 1:
r = q; rgb.r = max_brightness - offset;
g = v; rgb.g = max_brightness;
b = p; rgb.b = min_brightness;
break; break;
case 2: case 2:
r = p; rgb.r = min_brightness;
g = v; rgb.g = max_brightness;
b = t; rgb.b = min_brightness + offset;
break; break;
case 3: case 3:
r = p; rgb.r = min_brightness;
g = q; rgb.g = max_brightness - offset;
b = v; rgb.b = max_brightness;
break; break;
case 4: case 4:
r = t; rgb.r = min_brightness + offset;
g = p; rgb.g = min_brightness;
b = v; rgb.b = max_brightness;
break; break;
case 5: case 5:
r = v; rgb.r = max_brightness;
g = p; rgb.g = min_brightness;
b = q; rgb.b = max_brightness - offset;
break;
default:
rgb.r = 0;
rgb.g = 0;
rgb.b = 0;
break; break;
} }
struct led_rgb rgb = {r : r * 255, g : g * 255, b : b * 255};
return rgb; return rgb;
} }
static void zmk_rgb_underglow_effect_solid() { static void zmk_rgb_underglow_effect_solid() {
struct led_rgb rgb = hsb_to_rgb(hsb_scale_min_max(state.color));
for (int i = 0; i < STRIP_NUM_PIXELS; i++) { for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
pixels[i] = hsb_to_rgb(hsb_scale_min_max(state.color)); pixels[i] = rgb;
} }
} }
static void zmk_rgb_underglow_effect_breathe() { static void zmk_rgb_underglow_effect_breathe() {
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
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;
struct led_rgb rgb = hsb_to_rgb(hsb_scale_zero_max(hsb));
pixels[i] = hsb_to_rgb(hsb_scale_zero_max(hsb)); for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
pixels[i] = rgb;
} }
state.animation_step += state.animation_speed * 10; state.animation_step += state.animation_speed * 10;
@ -139,11 +150,12 @@ static void zmk_rgb_underglow_effect_breathe() {
} }
static void zmk_rgb_underglow_effect_spectrum() { static void zmk_rgb_underglow_effect_spectrum() {
for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
struct zmk_led_hsb hsb = state.color; struct zmk_led_hsb hsb = state.color;
hsb.h = state.animation_step; hsb.h = state.animation_step;
struct led_rgb rgb = hsb_to_rgb(hsb_scale_min_max(hsb));
pixels[i] = hsb_to_rgb(hsb_scale_min_max(hsb)); for (int i = 0; i < STRIP_NUM_PIXELS; i++) {
pixels[i] = rgb;
} }
state.animation_step += state.animation_speed; state.animation_step += state.animation_speed;