diff --git a/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen.c b/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen.c index 023c7f87e..8f8a2bef5 100644 --- a/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen.c +++ b/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen.c @@ -52,7 +52,7 @@ static void xtreme_app_scene_misc_screen_lcd_color_changed(VariableItem* item, u XtremeApp* app = variable_item_get_context(item); uint8_t index = variable_item_get_current_value_index(item); variable_item_set_current_value_text(item, lcd_colors[index].name); - rgb_backlight_set_color(led, lcd_colors[index].color); + rgb_backlight_set_color(led, &lcd_colors[index].color); app->save_backlight = true; } static void xtreme_app_scene_misc_screen_lcd_color_0_changed(VariableItem* item) { @@ -169,11 +169,12 @@ void xtreme_app_scene_misc_screen_on_enter(void* context) { }; size_t lcd_sz = COUNT_OF(lcd_colors); + RgbColor color; for(size_t i = 0; i < COUNT_OF(lcd_cols); i++) { char name[12]; snprintf(name, sizeof(name), "LCD LED %u", lcd_cols[i].led + 1); item = variable_item_list_add(var_item_list, name, lcd_sz, lcd_cols[i].cb, app); - RgbColor color = rgb_backlight_get_color(lcd_cols[i].led); + rgb_backlight_get_color(lcd_cols[i].led, &color); bool found = false; for(size_t i = 0; i < lcd_sz; i++) { if(rgbcmp(&color, &lcd_colors[i].color) != 0) continue; diff --git a/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen_color.c b/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen_color.c index a998a1cb3..91d2421a0 100644 --- a/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen_color.c +++ b/applications/main/xtreme_app/scenes/xtreme_app_scene_misc_screen_color.c @@ -16,8 +16,9 @@ void xtreme_app_scene_misc_screen_color_on_enter(void* context) { byte_input_set_header_text(byte_input, "Set LCD Color (#RRGGBB)"); - app->lcd_color = rgb_backlight_get_color( - scene_manager_get_scene_state(app->scene_manager, XtremeAppSceneMiscScreenColor)); + rgb_backlight_get_color( + scene_manager_get_scene_state(app->scene_manager, XtremeAppSceneMiscScreenColor), + &app->lcd_color); byte_input_set_result_callback( byte_input, @@ -40,7 +41,7 @@ bool xtreme_app_scene_misc_screen_color_on_event(void* context, SceneManagerEven case ByteInputResultOk: rgb_backlight_set_color( scene_manager_get_scene_state(app->scene_manager, XtremeAppSceneMiscScreenColor), - app->lcd_color); + &app->lcd_color); app->save_backlight = true; scene_manager_previous_scene(app->scene_manager); break; diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 58d6e44ef..49446ad2a 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -1753,7 +1753,7 @@ Function,+,hex_char_to_hex_nibble,_Bool,"char, uint8_t*" Function,+,hex_char_to_uint8,_Bool,"char, char, uint8_t*" Function,+,hex_chars_to_uint64,_Bool,"const char*, uint64_t*" Function,+,hex_chars_to_uint8,_Bool,"const char*, uint8_t*" -Function,+,hsv2rgb,RgbColor,HsvColor +Function,+,hsv2rgb,void,"const HsvColor*, RgbColor*" Function,+,hsvcmp,int,"const HsvColor*, const HsvColor*" Function,-,hypot,double,"double, double" Function,-,hypotf,float,"float, float" @@ -2583,8 +2583,8 @@ Function,-,rfal_platform_spi_acquire,void, Function,-,rfal_platform_spi_release,void, Function,-,rfal_set_callback_context,void,void* Function,-,rfal_set_state_changed_callback,void,RfalStateChangedCallback -Function,+,rgb2hsv,HsvColor,RgbColor -Function,+,rgb_backlight_get_color,RgbColor,uint8_t +Function,+,rgb2hsv,void,"const RgbColor*, HsvColor*" +Function,+,rgb_backlight_get_color,void,"uint8_t, RgbColor*" Function,+,rgb_backlight_get_rainbow_interval,uint32_t, Function,+,rgb_backlight_get_rainbow_mode,RGBBacklightRainbowMode, Function,+,rgb_backlight_get_rainbow_saturation,uint8_t, @@ -2592,7 +2592,7 @@ Function,+,rgb_backlight_get_rainbow_speed,uint8_t, Function,-,rgb_backlight_load_settings,void,_Bool Function,+,rgb_backlight_reconfigure,void,_Bool Function,+,rgb_backlight_save_settings,void, -Function,+,rgb_backlight_set_color,void,"uint8_t, RgbColor" +Function,+,rgb_backlight_set_color,void,"uint8_t, const RgbColor*" Function,+,rgb_backlight_set_rainbow_interval,void,uint32_t Function,+,rgb_backlight_set_rainbow_mode,void,RGBBacklightRainbowMode Function,+,rgb_backlight_set_rainbow_saturation,void,uint8_t diff --git a/lib/drivers/rgb_backlight.c b/lib/drivers/rgb_backlight.c index 8c6d77bad..7fcb3a472 100644 --- a/lib/drivers/rgb_backlight.c +++ b/lib/drivers/rgb_backlight.c @@ -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; } diff --git a/lib/drivers/rgb_backlight.h b/lib/drivers/rgb_backlight.h index f7f008651..a30b5e268 100644 --- a/lib/drivers/rgb_backlight.h +++ b/lib/drivers/rgb_backlight.h @@ -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 diff --git a/lib/toolbox/colors.c b/lib/toolbox/colors.c index 33ad8152c..22f06ac68 100644 --- a/lib/toolbox/colors.c +++ b/lib/toolbox/colors.c @@ -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); + } } diff --git a/lib/toolbox/colors.h b/lib/toolbox/colors.h index 902b1a5d0..f53f44873 100644 --- a/lib/toolbox/colors.h +++ b/lib/toolbox/colors.h @@ -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 }