Rainbow mode in progress

This commit is contained in:
Dmitry422
2025-03-19 00:46:00 +07:00
parent a4d0c467f9
commit a629118aaa
5 changed files with 348 additions and 249 deletions

View File

@@ -23,6 +23,7 @@
#include <furi_hal.h>
#include <storage/storage.h>
#include "rgb_backlight.h"
#include <math.h>
#define COLOR_COUNT (sizeof(colors) / sizeof(RGBBacklightColor))
@@ -87,14 +88,22 @@ void rgb_backlight_set_led_static_color(uint8_t led, uint8_t index) {
// furi_record_close(RECORD_RGB_BACKLIGHT);
}
// use RECORD for acces to rgb service instance and update current colors by custom
// void rgb_backlight_set_custom_color(uint8_t red, uint8_t green, uint8_t blue) {
// RGBBacklightApp* app = furi_record_open(RECORD_RGB_BACKLIGHT);
// app->current_red = red;
// app->current_green = green;
// app->current_blue = blue;
// furi_record_close(RECORD_RGB_BACKLIGHT);
// }
// use RECORD for acces to rgb service instance and update current colors by custom value
void rgb_backlight_set_led_custom_color(uint8_t led, uint8_t red, uint8_t green, uint8_t blue) {
// RGBBacklightApp* app = furi_record_open(RECORD_RGB_BACKLIGHT);
// float brightness = app->settings->brightness;
if(led < SK6805_get_led_count()) {
current_led[led].red = red;
current_led[led].green = green;
current_led[led].blue = blue;
SK6805_set_led_color(led, red, green, blue);
}
// furi_record_close(RECORD_RGB_BACKLIGHT);
}
// use RECORD for acces to rgb service instance, use current_* colors and update backlight
void rgb_backlight_update(float brightness) {
@@ -132,19 +141,77 @@ void rainbow_timer_starter(RGBBacklightApp* app) {
}
}
}
// HSV to RGB based on
// https://www.radiokot.ru/forum/viewtopic.php?p=3000181&ysclid=m88wvoz34w244644702
// https://radiolaba.ru/microcotrollers/tsvetnaya-lampa.html#comment-1790
// https://alexgyver.ru/lessons/arduino-rgb/?ysclid=m88voflppa24464916
void hsv_to_rgb(uint8_t red, uint8_t green, uint8_t blue, uint16_t hue ,uint8_t sat ,uint8_t val) {
float r = 1.0f;
float g = 1.0f;
float b = 1.0f;
float H = hue / 255.0f;
float S = sat / 255.0f;
float V = val / 255.0f;
uint8_t i = trunc(H * 6);
float f = H * 6 - i;
float p = V * (1 - S);
float q = V * (1 - f * S);
float t = V * (1 - (1 - f) * S);
switch(i) {
case 0:
r = V, g = t, b = p;
break;
case 1:
r = q, g = V, b = p;
break;
case 2:
r = p, g = V, b = t;
break;
case 3:
r = p, g = q, b = V;
break;
case 4:
r = t, g = p, b = V;
break;
case 5:
r = V, g = p, b = q;
break;
}
red = r * 255;
green = g * 255;
blue = b * 255;
}
static void rainbow_timer_callback(void* context) {
furi_assert(context);
RGBBacklightApp* app = context;
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
if (app->settings->rgb_backlight_installed) {
if(app->settings->rgb_backlight_installed) {
switch(app->settings->rainbow_mode) {
case 1:
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
hsv_to_rgb(r,g,b,app->rainbow_hue, app->settings->rainbow_saturation,app->settings->brightness*255);
FURI_LOG_D(
TAG, "rgb %d,%d,%d", r, g, b);
//rgb_backlight_set_led_custom_color (i,*r,*g,*b);
//SK6805_update();
}
break;
case 2:
break;
default:
break;
}
app->rainbow_hue++;
rgb_backlight_update(app->settings->brightness);
}
@@ -168,6 +235,8 @@ int32_t rgb_backlight_srv(void* p) {
app->settings = malloc(sizeof(RGBBacklightSettings));
rgb_backlight_settings_load(app->settings);
app->rainbow_hue = 1;
furi_record_create(RECORD_RGB_BACKLIGHT, app);
//if rgb_backlight_installed then start rainbow or set leds colors from saved settings (default index = 0)

View File

@@ -28,10 +28,10 @@ extern "C" {
typedef struct {
FuriTimer* rainbow_timer;
// int16_t current_red;
// int16_t current_green;
// int16_t current_blue;
uint8_t rainbow_hue;
uint8_t rainbow_red;
uint8_t rainbow_green;
uint8_t rainbow_blue;
RGBBacklightSettings* settings;

View File

@@ -18,16 +18,17 @@ typedef struct {
uint8_t version;
uint8_t rgb_backlight_installed;
float brightness;
// static gradient mode settings
uint8_t led_2_color_index;
uint8_t led_1_color_index;
uint8_t led_0_color_index;
// rainbow mode setings
uint32_t rainbow_mode;
uint32_t rainbow_speed_ms;
uint16_t rainbow_step;
uint8_t rainbow_saturation;
} RGBBacklightSettingsPrevious;
void rgb_backlight_settings_load(RGBBacklightSettings* settings) {
@@ -82,6 +83,7 @@ void rgb_backlight_settings_load(RGBBacklightSettings* settings) {
settings->brightness = 1.0f;
settings->rainbow_speed_ms = 100;
settings->rainbow_step = 1;
settings->rainbow_saturation = 255;
rgb_backlight_settings_save(settings);
}
}

View File

@@ -18,6 +18,7 @@ typedef struct {
uint32_t rainbow_mode;
uint32_t rainbow_speed_ms;
uint16_t rainbow_step;
uint8_t rainbow_saturation;
} RGBBacklightSettings;