Speedup and consolidate color lib with pointers

This commit is contained in:
Willy-JL
2023-10-09 01:27:38 +01:00
parent ac4ae1f8ed
commit fb2b0e2385
7 changed files with 87 additions and 88 deletions
+20 -14
View File
@@ -97,28 +97,27 @@ void rgb_backlight_save_settings() {
furi_check(furi_mutex_release(rgb_state.mutex) == FuriStatusOk);
}
void rgb_backlight_set_color(uint8_t index, RgbColor color) {
void rgb_backlight_set_color(uint8_t index, const RgbColor* color) {
if(index >= COUNT_OF(rgb_settings.colors)) return;
if(!rgb_state.settings_loaded) return;
furi_check(furi_mutex_acquire(rgb_state.mutex, FuriWaitForever) == FuriStatusOk);
rgb_settings.colors[index] = color;
memcpy(&rgb_settings.colors[index], color, sizeof(RgbColor));
rgb_backlight_reconfigure(rgb_state.enabled);
furi_check(furi_mutex_release(rgb_state.mutex) == FuriStatusOk);
}
RgbColor rgb_backlight_get_color(uint8_t index) {
if(index >= COUNT_OF(rgb_settings.colors)) return (RgbColor){0, 0, 0};
void rgb_backlight_get_color(uint8_t index, RgbColor* color) {
if(index >= COUNT_OF(rgb_settings.colors)) return;
if(!rgb_state.settings_loaded) return (RgbColor){0, 0, 0};
if(!rgb_state.settings_loaded) return;
furi_check(furi_mutex_acquire(rgb_state.mutex, FuriWaitForever) == FuriStatusOk);
RgbColor color = rgb_settings.colors[index];
memcpy(color, &rgb_settings.colors[index], sizeof(RgbColor));
furi_check(furi_mutex_release(rgb_state.mutex) == FuriStatusOk);
return color;
}
void rgb_backlight_set_rainbow_mode(RGBBacklightRainbowMode rainbow_mode) {
@@ -262,15 +261,22 @@ void rgb_backlight_update(uint8_t brightness, bool tick) {
rgb_state.rainbow_hsv.v = brightness;
}
HsvColor hsv = rgb_state.rainbow_hsv;
RgbColor rgb = hsv2rgb(hsv);
RgbColor rgb;
hsv2rgb(&rgb_state.rainbow_hsv, &rgb);
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
if(i && rgb_settings.rainbow_mode == RGBBacklightRainbowModeWave) {
hsv.h += (50 * i);
rgb = hsv2rgb(hsv);
if(rgb_settings.rainbow_mode == RGBBacklightRainbowModeWave) {
HsvColor hsv = rgb_state.rainbow_hsv;
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
if(i) {
hsv.h += (50 * i);
hsv2rgb(&hsv, &rgb);
}
SK6805_set_led_color(i, rgb.r, rgb.g, rgb.b);
}
} else {
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
SK6805_set_led_color(i, rgb.r, rgb.g, rgb.b);
}
SK6805_set_led_color(i, rgb.r, rgb.g, rgb.b);
}
break;
}
+2 -2
View File
@@ -50,9 +50,9 @@ void rgb_backlight_save_settings();
* @param index What led to set the color to (0 - SK6805_LED_COUNT-1)
* @param color RGB color to use
*/
void rgb_backlight_set_color(uint8_t index, RgbColor color);
void rgb_backlight_set_color(uint8_t index, const RgbColor* color);
RgbColor rgb_backlight_get_color(uint8_t index);
void rgb_backlight_get_color(uint8_t index, RgbColor* color);
/**
* @brief Change rainbow mode
+50 -57
View File
@@ -10,86 +10,79 @@ inline int hsvcmp(const HsvColor* a, const HsvColor* b) {
return memcmp(a, b, sizeof(HsvColor));
}
RgbColor hsv2rgb(HsvColor hsv) {
RgbColor rgb;
uint8_t region, remainder, p, q, t;
if(hsv.s == 0) {
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
void hsv2rgb(const HsvColor* hsv, RgbColor* rgb) {
if(hsv->s == 0) {
rgb->r = hsv->v;
rgb->g = hsv->v;
rgb->b = hsv->v;
return;
}
region = hsv.h / 43;
remainder = (hsv.h - (region * 43)) * 6;
uint8_t region = hsv->h / 43;
uint8_t remainder = (hsv->h - (region * 43)) * 6;
p = (hsv.v * (255 - hsv.s)) >> 8;
q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
uint8_t p = (hsv->v * (255 - hsv->s)) >> 8;
uint8_t q = (hsv->v * (255 - ((hsv->s * remainder) >> 8))) >> 8;
uint8_t t = (hsv->v * (255 - ((hsv->s * (255 - remainder)) >> 8))) >> 8;
switch(region) {
case 0:
rgb.r = hsv.v;
rgb.g = t;
rgb.b = p;
rgb->r = hsv->v;
rgb->g = t;
rgb->b = p;
break;
case 1:
rgb.r = q;
rgb.g = hsv.v;
rgb.b = p;
rgb->r = q;
rgb->g = hsv->v;
rgb->b = p;
break;
case 2:
rgb.r = p;
rgb.g = hsv.v;
rgb.b = t;
rgb->r = p;
rgb->g = hsv->v;
rgb->b = t;
break;
case 3:
rgb.r = p;
rgb.g = q;
rgb.b = hsv.v;
rgb->r = p;
rgb->g = q;
rgb->b = hsv->v;
break;
case 4:
rgb.r = t;
rgb.g = p;
rgb.b = hsv.v;
rgb->r = t;
rgb->g = p;
rgb->b = hsv->v;
break;
default:
rgb.r = hsv.v;
rgb.g = p;
rgb.b = q;
rgb->r = hsv->v;
rgb->g = p;
rgb->b = q;
break;
}
return rgb;
}
HsvColor rgb2hsv(RgbColor rgb) {
HsvColor hsv;
uint8_t rgbMin, rgbMax;
void rgb2hsv(const RgbColor* rgb, HsvColor* hsv) {
uint8_t rgbMin = rgb->r < rgb->g ? (rgb->r < rgb->b ? rgb->r : rgb->b) :
(rgb->g < rgb->b ? rgb->g : rgb->b);
uint8_t rgbMax = rgb->r > rgb->g ? (rgb->r > rgb->b ? rgb->r : rgb->b) :
(rgb->g > rgb->b ? rgb->g : rgb->b);
rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
hsv.v = rgbMax;
if(hsv.v == 0) {
hsv.h = 0;
hsv.s = 0;
return hsv;
hsv->v = rgbMax;
if(hsv->v == 0) {
hsv->h = 0;
hsv->s = 0;
return;
}
hsv.s = 255 * ((long)rgbMax - (long)rgbMin) / hsv.v;
if(hsv.s == 0) {
hsv.h = 0;
return hsv;
hsv->s = 255 * ((long)rgbMax - (long)rgbMin) / hsv->v;
if(hsv->s == 0) {
hsv->h = 0;
return;
}
if(rgbMax == rgb.r)
hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
else if(rgbMax == rgb.g)
hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
else
hsv.h = 171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin);
return hsv;
if(rgbMax == rgb->r) {
hsv->h = 0 + 43 * (rgb->g - rgb->b) / (rgbMax - rgbMin);
} else if(rgbMax == rgb->g) {
hsv->h = 85 + 43 * (rgb->b - rgb->r) / (rgbMax - rgbMin);
} else {
hsv->h = 171 + 43 * (rgb->r - rgb->g) / (rgbMax - rgbMin);
}
}
+4 -6
View File
@@ -7,25 +7,23 @@
extern "C" {
#endif
typedef struct RgbColor {
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} RgbColor;
typedef struct HsvColor {
typedef struct {
uint8_t h;
uint8_t s;
uint8_t v;
} HsvColor;
int rgbcmp(const RgbColor* a, const RgbColor* b);
int hsvcmp(const HsvColor* a, const HsvColor* b);
RgbColor hsv2rgb(HsvColor hsv);
HsvColor rgb2hsv(RgbColor rgb);
void hsv2rgb(const HsvColor* hsv, RgbColor* rgb);
void rgb2hsv(const RgbColor* rgb, HsvColor* hsv);
#ifdef __cplusplus
}