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:
parent
6f29453041
commit
9c64949081
1 changed files with 62 additions and 50 deletions
|
@ -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]
|
||||||
case 0:
|
uint32_t max_brightness = from_b * 2.55f;
|
||||||
r = v;
|
uint32_t min_brightness = from_b * (100 - from_s) * 0.0255f;
|
||||||
g = t;
|
|
||||||
b = p;
|
uint32_t sector = from_h / 60;
|
||||||
break;
|
uint32_t distance_into_sector = sector % 60;
|
||||||
case 1:
|
uint32_t offset = (max_brightness - min_brightness) * distance_into_sector / 60;
|
||||||
r = q;
|
|
||||||
g = v;
|
switch (sector) {
|
||||||
b = p;
|
case 0:
|
||||||
break;
|
rgb.r = max_brightness;
|
||||||
case 2:
|
rgb.g = min_brightness + offset;
|
||||||
r = p;
|
rgb.b = min_brightness;
|
||||||
g = v;
|
break;
|
||||||
b = t;
|
case 1:
|
||||||
break;
|
rgb.r = max_brightness - offset;
|
||||||
case 3:
|
rgb.g = max_brightness;
|
||||||
r = p;
|
rgb.b = min_brightness;
|
||||||
g = q;
|
break;
|
||||||
b = v;
|
case 2:
|
||||||
break;
|
rgb.r = min_brightness;
|
||||||
case 4:
|
rgb.g = max_brightness;
|
||||||
r = t;
|
rgb.b = min_brightness + offset;
|
||||||
g = p;
|
break;
|
||||||
b = v;
|
case 3:
|
||||||
break;
|
rgb.r = min_brightness;
|
||||||
case 5:
|
rgb.g = max_brightness - offset;
|
||||||
r = v;
|
rgb.b = max_brightness;
|
||||||
g = p;
|
break;
|
||||||
b = q;
|
case 4:
|
||||||
break;
|
rgb.r = min_brightness + offset;
|
||||||
|
rgb.g = min_brightness;
|
||||||
|
rgb.b = max_brightness;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
rgb.r = max_brightness;
|
||||||
|
rgb.g = min_brightness;
|
||||||
|
rgb.b = max_brightness - offset;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
rgb.r = 0;
|
||||||
|
rgb.g = 0;
|
||||||
|
rgb.b = 0;
|
||||||
|
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;
|
||||||
|
|
Loading…
Add table
Reference in a new issue