From 2c2413dbe95b4b753b0a54a0a1df592d70347747 Mon Sep 17 00:00:00 2001 From: HaxSam Date: Mon, 25 Mar 2024 00:16:28 +0100 Subject: [PATCH] rainbow mode now works over rpc still not done refrash rate from flipper change and is slow so the rainbow mode doesnt look good --- .../scenes/momentum_app_scene_misc_vgm.c | 12 +-- .../momentum_app_scene_misc_vgm_color.c | 2 +- applications/services/rpc/rpc_gui.c | 9 +-- lib/momentum/rpc_rgb.c | 80 +++++++++++++++++++ lib/momentum/rpc_rgb.h | 18 +++++ lib/toolbox/colors.c | 29 ++++++- lib/toolbox/colors.h | 15 ++-- targets/f7/api_symbols.csv | 3 +- 8 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 lib/momentum/rpc_rgb.c create mode 100644 lib/momentum/rpc_rgb.h diff --git a/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm.c b/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm.c index a5148d52db..d3277a0340 100644 --- a/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm.c +++ b/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm.c @@ -37,13 +37,13 @@ static const struct { char* name; RgbColor color; } vgm_colors[] = { - {"Off", {{0, 0, 0}}}, {"Orange", {{255, 69, 0}}}, {"Red", {{255, 0, 0}}}, - {"Maroon", {{128, 0, 0}}}, {"Yellow", {{255, 255, 0}}}, {"Olive", {{128, 128, 0}}}, - {"Lime", {{0, 255, 0}}}, {"Green", {{0, 128, 0}}}, {"Aqua", {{0, 255, 127}}}, - {"Cyan", {{0, 210, 210}}}, {"Azure", {{0, 127, 255}}}, {"Teal", {{0, 128, 128}}}, + {"Off", {{0, 0, 0}}}, {"Orange", {{255, 130, 0}}}, {"Red", {{255, 0, 0}}}, + {"Maroon", {{176, 48, 96}}}, {"Yellow", {{255, 255, 0}}}, {"Olive", {{128, 128, 0}}}, + {"Lime", {{0, 255, 0}}}, {"Green", {{74, 255, 0}}}, {"Aqua", {{0, 255, 255}}}, + {"Cyan", {{0, 255, 255}}}, {"Azure", {{0, 127, 255}}}, {"Teal", {{0, 128, 128}}}, {"Blue", {{0, 0, 255}}}, {"Navy", {{0, 0, 128}}}, {"Purple", {{128, 0, 128}}}, - {"Fuchsia", {{255, 0, 255}}}, {"Pink", {{173, 31, 173}}}, {"Brown", {{165, 42, 42}}}, - {"White", {{255, 192, 203}}}, + {"Fuchsia", {{255, 0, 255}}}, {"Pink", {{173, 31, 173}}}, {"Brown", {{150, 75, 0}}}, + {"White", {{255, 255, 255}}}, }; static const size_t vgm_colors_count = COUNT_OF(vgm_colors); static void momentum_app_scene_misc_vgm_foreground_changed(VariableItem* item) { diff --git a/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm_color.c b/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm_color.c index f8941b2a00..b9985db776 100644 --- a/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm_color.c +++ b/applications/main/momentum_app/scenes/momentum_app_scene_misc_vgm_color.c @@ -14,7 +14,7 @@ void momentum_app_scene_misc_vgm_color_on_enter(void* context) { MomentumApp* app = context; ByteInput* byte_input = app->byte_input; - byte_input_set_header_text(byte_input, "Set VGM Color (RGB565)"); + byte_input_set_header_text(byte_input, "Set VGM Color"); if(scene_manager_get_scene_state(app->scene_manager, MomentumAppSceneMiscVgmColor)) { app->vgm_color = momentum_settings.vgm_color_bg; diff --git a/applications/services/rpc/rpc_gui.c b/applications/services/rpc/rpc_gui.c index b96ab84af2..0a75856001 100644 --- a/applications/services/rpc/rpc_gui.c +++ b/applications/services/rpc/rpc_gui.c @@ -1,8 +1,7 @@ #include "rpc_i.h" #include #include -#include -#include "colors.h" +#include #include #include @@ -99,10 +98,8 @@ static void rpc_system_gui_screen_stream_frame_callback( rpc_gui->transmit_frame->content.gui_screen_frame.orientation = rpc_system_gui_screen_orientation_map[orientation]; - rpc_gui->transmit_frame->content.gui_screen_frame.fg_color = - momentum_settings.vgm_color_fg.value; - rpc_gui->transmit_frame->content.gui_screen_frame.bg_color = - momentum_settings.vgm_color_bg.value; + rpc_gui->transmit_frame->content.gui_screen_frame.fg_color = get_screen_color_fg().value; + rpc_gui->transmit_frame->content.gui_screen_frame.bg_color = get_screen_color_bg().value; furi_thread_flags_set(furi_thread_get_id(rpc_gui->transmit_thread), RpcGuiWorkerFlagTransmit); } diff --git a/lib/momentum/rpc_rgb.c b/lib/momentum/rpc_rgb.c new file mode 100644 index 0000000000..fc0f5e6823 --- /dev/null +++ b/lib/momentum/rpc_rgb.c @@ -0,0 +1,80 @@ +#include "rpc_rgb.h" + +#include "colors.h" +#include "momentum.h" +#include "drivers/rgb_backlight.h" + +static const RgbColor rainbow_colors[] = { + {{255, 0, 0}}, // Red + {{255, 130, 0}}, // Orange + {{255, 255, 0}}, // Yellow + {{0, 255, 0}}, // Green + {{0, 0, 255}}, // Blue + {{123, 0, 255}}, // Indigo + {{255, 0, 255}} // Violet +}; + +typedef struct { + uint8_t color; + uint8_t step; +} rgb_trans; + +static struct { + rgb_trans fg; + rgb_trans bg; +} state = { + .fg = {0, 0}, + .bg = {0, 0}, +}; + +RgbColor rainbow_step(rgb_trans* rgb) { + if(rgb->step >= 20) { + rgb->color = (rgb->color + 1) % 7; + rgb->step = 0; + } + + RgbColor color = interpolate_color( + &rainbow_colors[rgb->color], &rainbow_colors[(rgb->color + 1) % 7], rgb->step, 20); + rgb->step++; + + return color; +} + +RgbColor get_screen_color_fg() { + RgbColor color = {{0, 0, 0}}; + + switch(momentum_settings.vgm_color_mode) { + case VgmColorModeCustom: + color = momentum_settings.vgm_color_fg; + break; + case VgmColorModeRainbow: + //color = rainbow_step(&state.fg); + break; + case VgmColorModeRgbBacklight: + //rgb_backlight_get_color(0, &color); + break; + default: + break; + } + + return color; +} + +RgbColor get_screen_color_bg() { + RgbColor color = {{255, 130, 0}}; + switch(momentum_settings.vgm_color_mode) { + case VgmColorModeCustom: + color = momentum_settings.vgm_color_bg; + break; + case VgmColorModeRainbow: + color = rainbow_step(&state.bg); + break; + case VgmColorModeRgbBacklight: + rgb_backlight_get_color(0, &color); + break; + default: + break; + } + + return color; +} diff --git a/lib/momentum/rpc_rgb.h b/lib/momentum/rpc_rgb.h new file mode 100644 index 0000000000..c9095fc28e --- /dev/null +++ b/lib/momentum/rpc_rgb.h @@ -0,0 +1,18 @@ +#pragma once + +#include "colors.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void rpc_rgb_init(); + +RgbColor get_screen_color_fg(); +RgbColor get_screen_color_bg(); + +void rpc_rgb_deinit(); + +#ifdef __cplusplus +} +#endif diff --git a/lib/toolbox/colors.c b/lib/toolbox/colors.c index d66380d1d8..399977cd9e 100644 --- a/lib/toolbox/colors.c +++ b/lib/toolbox/colors.c @@ -90,6 +90,31 @@ void rgb2hsv(const RgbColor* rgb, HsvColor* hsv) { } } -inline int rgb565cmp(const Rgb565Color* a, const Rgb565Color* b) { - return memcmp(a, b, sizeof(Rgb565Color)); +RgbColor interpolate_color( + const RgbColor* colorStart, + const RgbColor* colorEnd, + uint8_t step, + uint8_t maxSteps) { + RgbColor color; + + if(step >= maxSteps) { + memcpy(&color, colorEnd, sizeof(RgbColor)); + return color; + } + + color.r = colorStart->r + (colorEnd->r - colorStart->r) * step / maxSteps; + color.g = colorStart->g + (colorEnd->g - colorStart->g) * step / maxSteps; + color.b = colorStart->b + (colorEnd->b - colorStart->b) * step / maxSteps; + + return color; +} + +RgbColor lerp_color(const RgbColor* colorStart, const RgbColor* colorEnd, double t) { + RgbColor color; + + color.r = colorStart->r * ((double)1.0 - t) + colorEnd->r * t; + color.g = colorStart->g * ((double)1.0 - t) + colorEnd->g * t; + color.b = colorStart->b * ((double)1.0 - t) + colorEnd->b * t; + + return color; } diff --git a/lib/toolbox/colors.h b/lib/toolbox/colors.h index 087cdda90a..3c8d0f7557 100644 --- a/lib/toolbox/colors.h +++ b/lib/toolbox/colors.h @@ -31,16 +31,13 @@ int hsvcmp(const HsvColor* a, const HsvColor* b); void hsv2rgb(const HsvColor* hsv, RgbColor* rgb); void rgb2hsv(const RgbColor* rgb, HsvColor* hsv); -typedef union { - uint16_t value; - struct { - uint16_t r : 5; - uint16_t g : 6; - uint16_t b : 5; - } FURI_PACKED; -} Rgb565Color; +RgbColor interpolate_color( + const RgbColor* colorStart, + const RgbColor* colorEnd, + uint8_t step, + uint8_t maxSteps); -int rgb565cmp(const Rgb565Color* a, const Rgb565Color* b); +RgbColor lerp_color(const RgbColor* colorStart, const RgbColor* colorEnd, double t); #ifdef __cplusplus } diff --git a/targets/f7/api_symbols.csv b/targets/f7/api_symbols.csv index 6a738babe2..80502a2f13 100644 --- a/targets/f7/api_symbols.csv +++ b/targets/f7/api_symbols.csv @@ -2028,6 +2028,7 @@ Function,+,infrared_worker_tx_stop,void,InfraredWorker* Function,-,initstate,char*,"unsigned, char*, size_t" Function,+,input_get_key_name,const char*,InputKey Function,+,input_get_type_name,const char*,InputType +Function,+,interpolate_color,RgbColor,"const RgbColor*, const RgbColor*, uint8_t, uint8_t" Function,-,iprintf,int,"const char*, ..." Function,-,isalnum,int,int Function,-,isalnum_l,int,"int, locale_t" @@ -2188,6 +2189,7 @@ Function,-,ldexp,double,"double, int" Function,-,ldexpf,float,"float, int" Function,-,ldexpl,long double,"long double, int" Function,-,ldiv,ldiv_t,"long, long" +Function,+,lerp_color,RgbColor,"const RgbColor*, const RgbColor*, double" Function,+,lfrfid_dict_file_load,ProtocolId,"ProtocolDict*, const char*" Function,+,lfrfid_dict_file_save,_Bool,"ProtocolDict*, ProtocolId, const char*" Function,+,lfrfid_raw_file_alloc,LFRFIDRawFile*,Storage* @@ -2958,7 +2960,6 @@ Function,-,rename,int,"const char*, const char*" Function,-,renameat,int,"int, const char*, int, const char*" Function,-,rewind,void,FILE* Function,+,rgb2hsv,void,"const RgbColor*, HsvColor*" -Function,+,rgb565cmp,int,"const Rgb565Color*, const Rgb565Color*" Function,+,rgb_backlight_get_color,void,"uint8_t, RgbColor*" Function,+,rgb_backlight_get_rainbow_interval,uint32_t, Function,+,rgb_backlight_get_rainbow_mode,RGBBacklightRainbowMode,