1
mirror of https://github.com/DarkFlippers/unleashed-firmware.git synced 2025-12-12 04:34:43 +04:00

Working on moving RGB routines from Notification to RGB service

This commit is contained in:
Dmitry422
2025-03-12 02:19:17 +07:00
parent 9e6593c09e
commit 4045628ac6
11 changed files with 324 additions and 32 deletions

View File

@@ -30,7 +30,6 @@ void input_settings_load(InputSettings* settings) {
sizeof(InputSettings),
INPUT_SETTINGS_MAGIC,
INPUT_SETTINGS_VER);
// if config previous version - load it and inicialize new settings
}
// in case of another config version we exit from useless cycle to next step
} while(false);

View File

@@ -7,5 +7,4 @@ App(
stack_size=1 * 1024,
order=99,
sdk_headers=["rgb_backlight.h"],
provides=["rgb_backlight_settings"],
)

View File

@@ -1,15 +1,156 @@
/*
RGB BackLight Service based on
RGB backlight FlipperZero driver
Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdio.h>
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include "rgb_backlight.h"
#define STATIC_COLOR_COUNT (sizeof(colors) / sizeof(RGBBacklightStaticColor))
#define TAG "RGB_BACKLIGHT_SRV"
int32_t rgb_backlight_srv (void* p){
UNUSED (p);
while (1){
FURI_LOG_I (TAG,"working");
furi_delay_ms (2000);
static const RGBBacklightStaticColor colors[] = {
{"Orange", 255, 60, 0},
{"Yellow", 255, 144, 0},
{"Spring", 167, 255, 0},
{"Lime", 0, 255, 0},
{"Aqua", 0, 255, 127},
{"Cyan", 0, 210, 210},
{"Azure", 0, 127, 255},
{"Blue", 0, 0, 255},
{"Purple", 127, 0, 255},
{"Magenta", 210, 0, 210},
{"Pink", 255, 0, 127},
{"Red", 255, 0, 0},
{"White", 254, 210, 200},
{"Custom", 0, 0, 0},
};
void rgb_backlight_set_custom_color(uint8_t red, uint8_t green, uint8_t blue, float brightness) {
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
uint8_t r = red * (brightness);
uint8_t g = green * (brightness);
uint8_t b = blue * (brightness);
SK6805_set_led_color(i, r, g, b);
}
SK6805_update();
}
return 0;
static void rainbow_timer_callback(void* context) {
furi_assert(context);
RGBBacklightApp* app = context;
// if rgb_mode_rainbow_mode is rainbow do rainbow effect
if(app->settings->rainbow_mode == 1) {
switch(app->rainbow_stage) {
// from red to yellow
case 1:
app->rainbow_green += app->settings->rainbow_step;
if(app->rainbow_green >= 255) {
app->rainbow_green = 255;
app->rainbow_stage++;
}
break;
// yellow red to green
case 2:
app->rainbow_red -= app->settings->rainbow_step;
if(app->rainbow_red <= 0) {
app->rainbow_red = 0;
app->rainbow_stage++;
}
break;
// from green to light blue
case 3:
app->rainbow_blue += app->settings->rainbow_step;
if(app->rainbow_blue >= 255) {
app->rainbow_blue = 255;
app->rainbow_stage++;
}
break;
//from light blue to blue
case 4:
app->rainbow_green -= app->settings->rainbow_step;
if(app->rainbow_green <= 0) {
app->rainbow_green = 0;
app->rainbow_stage++;
}
break;
//from blue to violet
case 5:
app->rainbow_red += app->settings->rainbow_step;
if(app->rainbow_red >= 255) {
app->rainbow_red = 255;
app->rainbow_stage++;
}
break;
//from violet to red
case 6:
app->rainbow_blue -= app->settings->rainbow_step;
if(app->rainbow_blue <= 0) {
app->rainbow_blue = 0;
app->rainbow_stage = 1;
}
break;
default:
break;
}
rgb_backlight_set_custom_color(
app->rainbow_red,
app->rainbow_green,
app->rainbow_blue,
app->settings->brightness);
}
// if rgb_mode_rainbow_mode is ..... do another effect
// if(app->settings.rainbow_mode == 2) {
// }
}
void rgb_backlight_settings_apply(RGBBacklightSettings* settings) {
UNUSED (settings);
//запуск таймера если все включено
// применить сохраненые настройки цвета к дисплею статику или кастом если индекс=13
}
int32_t rgb_backlight_srv (void* p){
// Define object app (full app with settings and running variables),
// allocate memory and create record for access to app structure from outside
RGBBacklightApp* app = malloc(sizeof(RGBBacklightApp));
furi_record_create(RECORD_RGB_BACKLIGHT, app);
//define rainbow_timer and they callback
app->rainbow_timer =
furi_timer_alloc(rainbow_timer_callback, FuriTimerTypePeriodic, app);
// load or init new settings and apply it
rgb_backlight_settings_load (app->settings);
rgb_backlight_settings_apply (app->settings);
UNUSED(p);
while(1) {
FURI_LOG_I(TAG, "working");
furi_delay_ms(2000);
}
return 0;
}

View File

@@ -0,0 +1,44 @@
/*
RGB BackLight Service based on
RGB backlight FlipperZero driver
Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <furi.h>
#include "rgb_backlight_settings.h"
#include "SK6805.h"
typedef struct {
char* name;
uint8_t red;
uint8_t green;
uint8_t blue;
} RGBBacklightStaticColor;
typedef struct {
FuriTimer* rainbow_timer;
int16_t rainbow_red;
int16_t rainbow_green;
int16_t rainbow_blue;
uint8_t rainbow_stage;
RGBBacklightSettings* settings;
} RGBBacklightApp;
#define RECORD_RGB_BACKLIGHT "rgb_backlight"

View File

@@ -0,0 +1,95 @@
#include "rgb_backlight_settings.h"
#include <saved_struct.h>
#include <storage/storage.h>
#define TAG "RGBBackligthSettings"
#define RGB_BACKLIGHT_SETTINGS_FILE_NAME ".rgb_backlight.settings"
#define RGB_BACKLIGHT_SETTINGS_PATH INT_PATH(RGB_BACKLIGHT_SETTINGS_FILE_NAME)
#define RGB_BACKLIGHT_SETTINGS_MAGIC (0x30)
#define RGB_BACKLIGHT_SETTINGS_VER_PREV (0) // Previous version number
#define RGB_BACKLIGHT_SETTINGS_VER (1) // New version number
//pervious settings must be copyed from previous rgb_backlight_settings.h file
typedef struct {
uint8_t version;
bool rgb_mod_installed;
uint8_t display_static_color_index;
uint8_t custom_r;
uint8_t custom_g;
uint8_t custom_b;
uint32_t rainbow_mode;
uint32_t rainbow_speed_ms;
uint16_t rainbow_step;
} RGBBacklightSettingsPrevious;
void rgb_backlight_settings_load(RGBBacklightSettings* settings) {
furi_assert(settings);
bool success = false;
//a useless cycle do-while, may will be used in future with anoter condition
do {
// take version from settings file metadata, if cant then break and fill settings with 0 and save to settings file;
uint8_t version;
if(!saved_struct_get_metadata(RGB_BACKLIGHT_SETTINGS_PATH, NULL, &version, NULL)) break;
// if config actual version - load it directly
if(version == RGB_BACKLIGHT_SETTINGS_VER) {
success = saved_struct_load(
RGB_BACKLIGHT_SETTINGS_PATH,
settings,
sizeof(RGBBacklightSettings),
RGB_BACKLIGHT_SETTINGS_MAGIC,
RGB_BACKLIGHT_SETTINGS_VER);
// if config previous version - load it and inicialize new settings
} else if(version == RGB_BACKLIGHT_SETTINGS_VER_PREV) {
RGBBacklightSettingsPrevious* settings_previous = malloc(sizeof(RGBBacklightSettingsPrevious));
success = saved_struct_load(
RGB_BACKLIGHT_SETTINGS_PATH,
settings_previous,
sizeof(RGBBacklightSettingsPrevious),
RGB_BACKLIGHT_SETTINGS_MAGIC,
RGB_BACKLIGHT_SETTINGS_VER_PREV);
// new settings initialization
if(success) {
// copy loaded old settings as part of new
uint32_t size = sizeof(settings);
memcpy(settings, settings_previous, size);
// set new options to initial value
// settings.something = something;
}
free(settings_previous);
}
// in case of another config version we exit from useless cycle to next step
} while(false);
// fill settings with 0 and save to settings file;
// Orange color (index=0) will be default
if(!success) {
FURI_LOG_W(TAG, "Failed to load file, using defaults 0");
memset(settings, 0, sizeof(RGBBacklightSettings));
rgb_backlight_settings_save(settings);
}
}
void rgb_backlight_settings_save(const RGBBacklightSettings* settings) {
furi_assert(settings);
const bool success = saved_struct_save(
RGB_BACKLIGHT_SETTINGS_PATH,
settings,
sizeof(RGBBacklightSettings),
RGB_BACKLIGHT_SETTINGS_MAGIC,
RGB_BACKLIGHT_SETTINGS_VER);
if(!success) {
FURI_LOG_E(TAG, "Failed to save rgb_backlight_settings file");
}
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
typedef struct {
uint8_t version;
bool rgb_mod_installed;
uint8_t display_static_color_index;
uint8_t custom_r;
uint8_t custom_g;
uint8_t custom_b;
float brightness;
uint32_t rainbow_mode;
uint32_t rainbow_speed_ms;
uint16_t rainbow_step;
} RGBBacklightSettings;
#ifdef __cplusplus
extern "C" {
#endif
void rgb_backlight_settings_load(RGBBacklightSettings* settings);
void rgb_backlight_settings_save(const RGBBacklightSettings* settings);
#ifdef __cplusplus
}
#endif