mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-19 04:44:47 -07:00
Merge pull request #890 from Dmitry422/dev
Moving RGB backlight back to Notification service
This commit is contained in:
@@ -12,6 +12,5 @@ App(
|
||||
"loader",
|
||||
"power",
|
||||
"namechanger_srv",
|
||||
"rgb_backlight",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ App(
|
||||
apptype=FlipperAppType.SERVICE,
|
||||
entry_point="notification_srv",
|
||||
cdefines=["SRV_NOTIFICATION"],
|
||||
requires=["input","rgb_backlight"],
|
||||
requires=["input"],
|
||||
provides=["notification_settings"],
|
||||
stack_size=int(1.5 * 1024),
|
||||
order=100,
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
#include "notification.h"
|
||||
#include "notification_messages.h"
|
||||
#include "notification_app.h"
|
||||
#include "applications/services/rgb_backlight/rgb_backlight.h"
|
||||
|
||||
#define TAG "NotificationSrv"
|
||||
#define TAG "NotificationSrv"
|
||||
#define COLOR_COUNT (sizeof(colors) / sizeof(RGBBacklightColor))
|
||||
|
||||
static const uint8_t minimal_delay = 100;
|
||||
static const uint8_t led_off_values[NOTIFICATION_LED_COUNT] = {0x00, 0x00, 0x00};
|
||||
@@ -33,6 +33,210 @@ static uint8_t notification_settings_get_display_brightness(NotificationApp* app
|
||||
static uint8_t notification_settings_get_rgb_led_brightness(NotificationApp* app, uint8_t value);
|
||||
static uint32_t notification_settings_display_off_delay_ticks(NotificationApp* app);
|
||||
|
||||
// --- RGB BACKLIGHT ---
|
||||
|
||||
// local variable for local use
|
||||
uint8_t rgb_backlight_installed_variable = 0;
|
||||
|
||||
typedef struct {
|
||||
char* name;
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
uint8_t blue;
|
||||
} RGBBacklightColor;
|
||||
|
||||
// use one type RGBBacklightColor for current_leds_settings and for static colors definition
|
||||
static RGBBacklightColor current_led[] = {
|
||||
{"LED0", 0, 0, 0},
|
||||
{"LED1", 0, 0, 0},
|
||||
{"LED2", 0, 0, 0},
|
||||
};
|
||||
|
||||
static const RGBBacklightColor 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},
|
||||
{"OFF", 0, 0, 0},
|
||||
};
|
||||
|
||||
uint8_t rgb_backlight_get_color_count(void) {
|
||||
return COLOR_COUNT;
|
||||
}
|
||||
|
||||
const char* rgb_backlight_get_color_text(uint8_t index) {
|
||||
return colors[index].name;
|
||||
}
|
||||
|
||||
// function for changind local variable from outside;
|
||||
void set_rgb_backlight_installed_variable(uint8_t var) {
|
||||
rgb_backlight_installed_variable = var;
|
||||
}
|
||||
|
||||
// update led current colors by static
|
||||
void rgb_backlight_set_led_static_color(uint8_t led, uint8_t index) {
|
||||
if(led < SK6805_get_led_count()) {
|
||||
uint8_t r = colors[index].red;
|
||||
uint8_t g = colors[index].green;
|
||||
uint8_t b = colors[index].blue;
|
||||
|
||||
current_led[led].red = r;
|
||||
current_led[led].green = g;
|
||||
current_led[led].blue = b;
|
||||
|
||||
SK6805_set_led_color(led, r, g, b);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// led number (0-2), hue (0..255), sat (0..255), val (0...1)
|
||||
void rgb_backlight_set_led_custom_hsv_color(uint8_t led, uint16_t hue, uint8_t sat, float V) {
|
||||
// init value
|
||||
float r = 1.0f;
|
||||
float g = 1.0f;
|
||||
float b = 1.0f;
|
||||
|
||||
// from (0..255) to (0..1)
|
||||
float H = hue / 255.0f;
|
||||
float S = sat / 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;
|
||||
}
|
||||
|
||||
// from (0..1) to (0..255)
|
||||
current_led[led].red = r * 255;
|
||||
current_led[led].green = g * 255;
|
||||
current_led[led].blue = b * 255;
|
||||
}
|
||||
|
||||
// set current_* colors to led and update backlight
|
||||
void rgb_backlight_update(float brightness) {
|
||||
if(rgb_backlight_installed_variable > 0) {
|
||||
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
|
||||
uint8_t r = current_led[i].red * brightness * 1.0f;
|
||||
uint8_t g = current_led[i].green * brightness * 1.0f;
|
||||
uint8_t b = current_led[i].blue * brightness * 1.0f;
|
||||
SK6805_set_led_color(i, r, g, b);
|
||||
}
|
||||
SK6805_update();
|
||||
}
|
||||
}
|
||||
|
||||
// start furi timer for rainbow
|
||||
void rainbow_timer_start(NotificationApp* app) {
|
||||
if(furi_timer_is_running(app->rainbow_timer)) {
|
||||
furi_timer_stop(app->rainbow_timer);
|
||||
}
|
||||
furi_timer_start(app->rainbow_timer, furi_ms_to_ticks(app->settings.rgb.rainbow_speed_ms));
|
||||
}
|
||||
|
||||
// stop furi timer for rainbow
|
||||
void rainbow_timer_stop(NotificationApp* app) {
|
||||
if(furi_timer_is_running(app->rainbow_timer)) {
|
||||
furi_timer_stop(app->rainbow_timer);
|
||||
}
|
||||
}
|
||||
|
||||
// if rgb_backlight_installed then apply rainbow colors to backlight and start/restart/stop rainbow_timer
|
||||
void rainbow_timer_starter(NotificationApp* app) {
|
||||
if((app->settings.rgb.rainbow_mode > 0) && (app->settings.rgb.rgb_backlight_installed)) {
|
||||
rainbow_timer_start(app);
|
||||
}
|
||||
}
|
||||
|
||||
static void rainbow_timer_callback(void* context) {
|
||||
furi_assert(context);
|
||||
NotificationApp* app = context;
|
||||
|
||||
if(app->settings.rgb.rgb_backlight_installed) {
|
||||
app->rainbow_hue += app->settings.rgb.rainbow_step;
|
||||
if(app->rainbow_hue > 254) {
|
||||
app->rainbow_hue = 0;
|
||||
}
|
||||
|
||||
uint8_t wide = app->settings.rgb.rainbow_wide;
|
||||
|
||||
switch(app->settings.rgb.rainbow_mode) {
|
||||
//rainbow mode
|
||||
case 1:
|
||||
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
i,
|
||||
app->rainbow_hue,
|
||||
app->settings.rgb.rainbow_saturation,
|
||||
app->settings.display_brightness);
|
||||
}
|
||||
break;
|
||||
|
||||
//wave mode
|
||||
case 2:
|
||||
uint16_t j = app->rainbow_hue + wide;
|
||||
uint16_t k = app->rainbow_hue + wide * 2;
|
||||
|
||||
if(app->rainbow_hue > (254 - wide)) {
|
||||
j = j - 255;
|
||||
}
|
||||
if(app->rainbow_hue > (254 - wide * 2)) {
|
||||
k = k - 255;
|
||||
}
|
||||
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
0,
|
||||
app->rainbow_hue,
|
||||
app->settings.rgb.rainbow_saturation,
|
||||
app->settings.display_brightness);
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
1, j, app->settings.rgb.rainbow_saturation, app->settings.display_brightness);
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
2, k, app->settings.rgb.rainbow_saturation, app->settings.display_brightness);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
rgb_backlight_update(app->settings.led_brightness * app->current_night_shift);
|
||||
}
|
||||
}
|
||||
|
||||
// --- RGB BACKLIGHT END---
|
||||
|
||||
// --- NIGHT SHIFT ---
|
||||
|
||||
void night_shift_timer_start(NotificationApp* app) {
|
||||
@@ -64,10 +268,8 @@ void night_shift_timer_callback(void* context) {
|
||||
// set values to stock and rgb backlights
|
||||
if((time > app->settings.night_shift_end) && (time < app->settings.night_shift_start)) {
|
||||
app->current_night_shift = 1.0f;
|
||||
app->rgb_srv->current_night_shift = 1.0f;
|
||||
} else {
|
||||
app->current_night_shift = app->settings.night_shift;
|
||||
app->rgb_srv->current_night_shift = app->settings.night_shift;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +469,7 @@ static void notification_process_notification_message(
|
||||
reset_mask |= reset_display_mask;
|
||||
|
||||
//start rgb_mod_rainbow_timer when display backlight is ON and all corresponding settings is ON too
|
||||
rainbow_timer_starter(app->rgb_srv);
|
||||
rainbow_timer_starter(app);
|
||||
// --- NIGHT SHIFT END ---
|
||||
} else {
|
||||
reset_mask &= ~reset_display_mask;
|
||||
@@ -276,8 +478,8 @@ static void notification_process_notification_message(
|
||||
furi_timer_stop(app->display_timer);
|
||||
}
|
||||
//stop rgb_mod_rainbow_timer when display backlight is OFF
|
||||
if(furi_timer_is_running(app->rgb_srv->rainbow_timer)) {
|
||||
rainbow_timer_stop(app->rgb_srv);
|
||||
if(furi_timer_is_running(app->rainbow_timer)) {
|
||||
rainbow_timer_stop(app);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -610,8 +812,7 @@ static NotificationApp* notification_app_alloc(void) {
|
||||
notification_message(app, &sequence_display_backlight_on);
|
||||
|
||||
// --- NIGHT SHIFT ---
|
||||
app->rgb_srv = furi_record_open(RECORD_RGB_BACKLIGHT);
|
||||
app->rgb_srv->current_night_shift = 1.0f;
|
||||
app->current_night_shift = 1.0f;
|
||||
app->current_night_shift = 1.0f;
|
||||
app->settings.night_shift = 1.0f;
|
||||
app->settings.night_shift_start = 1020;
|
||||
@@ -620,6 +821,21 @@ static NotificationApp* notification_app_alloc(void) {
|
||||
furi_timer_alloc(night_shift_timer_callback, FuriTimerTypePeriodic, app);
|
||||
// --- NIGHT SHIFT END ---
|
||||
|
||||
// init working variables
|
||||
app->rainbow_hue = 1;
|
||||
app->current_night_shift = 1.0f;
|
||||
|
||||
// init rgb.segings values
|
||||
app->settings.rgb.rgb_backlight_installed = 0;
|
||||
app->settings.rgb.led_2_color_index = 0;
|
||||
app->settings.rgb.led_1_color_index = 0;
|
||||
app->settings.rgb.led_0_color_index = 0;
|
||||
app->settings.rgb.rainbow_speed_ms = 100;
|
||||
app->settings.rgb.rainbow_step = 1;
|
||||
app->settings.rgb.rainbow_saturation = 255;
|
||||
app->settings.rgb.rainbow_wide = 50;
|
||||
|
||||
// use RECORD for setup init values to canvas lcd_inverted
|
||||
Gui* tmp_gui = furi_record_open(RECORD_GUI);
|
||||
Canvas* tmp_canvas = gui_direct_draw_acquire(tmp_gui);
|
||||
canvas_set_inverted_lcd(tmp_canvas, false);
|
||||
@@ -651,7 +867,7 @@ static void notification_apply_settings(NotificationApp* app) {
|
||||
notification_apply_lcd_contrast(app);
|
||||
|
||||
// --- NIGHT SHIFT ---
|
||||
// if night_shift_enabled start timer for controlling current_night_shift multiplicator value depent from current time
|
||||
// if night_shift enabled then start timer for controlling current_night_shift multiplicator value depent from current time
|
||||
if(app->settings.night_shift != 1) {
|
||||
night_shift_timer_start(app);
|
||||
}
|
||||
@@ -693,6 +909,34 @@ int32_t notification_srv(void* p) {
|
||||
|
||||
furi_record_create(RECORD_NOTIFICATION, app);
|
||||
|
||||
// --- RGB BACKLIGHT SECTION ---
|
||||
|
||||
//setup local variable
|
||||
set_rgb_backlight_installed_variable(app->settings.rgb.rgb_backlight_installed);
|
||||
|
||||
// define rainbow_timer and they callback
|
||||
app->rainbow_timer = furi_timer_alloc(rainbow_timer_callback, FuriTimerTypePeriodic, app);
|
||||
|
||||
// if rgb_backlight_installed then start rainbow or set leds colors from saved settings (default index = 0)
|
||||
if(app->settings.rgb.rgb_backlight_installed) {
|
||||
if(app->settings.rgb.rainbow_mode > 0) {
|
||||
rainbow_timer_start(app);
|
||||
} else {
|
||||
rgb_backlight_set_led_static_color(2, app->settings.rgb.led_2_color_index);
|
||||
rgb_backlight_set_led_static_color(1, app->settings.rgb.led_1_color_index);
|
||||
rgb_backlight_set_led_static_color(0, app->settings.rgb.led_0_color_index);
|
||||
rgb_backlight_update(app->settings.display_brightness * app->current_night_shift);
|
||||
}
|
||||
// if rgb_backlight not installed then set default static orange color(index=0) to all leds (0-2) and force light on
|
||||
} else {
|
||||
rgb_backlight_set_led_static_color(2, 0);
|
||||
rgb_backlight_set_led_static_color(1, 0);
|
||||
rgb_backlight_set_led_static_color(0, 0);
|
||||
SK6805_update();
|
||||
}
|
||||
|
||||
// --- RGB BACKLIGHT SECTION END ---
|
||||
|
||||
NotificationAppMessage message;
|
||||
while(1) {
|
||||
furi_check(furi_message_queue_get(app->queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
#include "notification.h"
|
||||
#include "notification_messages.h"
|
||||
#include "notification_settings_filename.h"
|
||||
#include "applications/services/rgb_backlight/rgb_backlight.h"
|
||||
#include <SK6805.h>
|
||||
#include <math.h>
|
||||
|
||||
#define NOTIFICATION_LED_COUNT 3
|
||||
#define NOTIFICATION_EVENT_COMPLETE 0x00000001U
|
||||
@@ -34,9 +35,26 @@ typedef struct {
|
||||
Light light;
|
||||
} NotificationLedLayer;
|
||||
|
||||
#define NOTIFICATION_SETTINGS_VERSION 0x04
|
||||
#define NOTIFICATION_SETTINGS_VERSION 0x05
|
||||
#define NOTIFICATION_SETTINGS_PATH INT_PATH(NOTIFICATION_SETTINGS_FILE_NAME)
|
||||
|
||||
typedef struct {
|
||||
//Common settings
|
||||
uint8_t rgb_backlight_installed;
|
||||
|
||||
// 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;
|
||||
uint8_t rainbow_wide;
|
||||
} RGBBacklightSettings;
|
||||
|
||||
typedef struct {
|
||||
uint8_t version;
|
||||
float display_brightness;
|
||||
@@ -49,6 +67,7 @@ typedef struct {
|
||||
uint32_t night_shift_start;
|
||||
uint32_t night_shift_end;
|
||||
bool lcd_inversion;
|
||||
RGBBacklightSettings rgb;
|
||||
} NotificationSettings;
|
||||
|
||||
struct NotificationApp {
|
||||
@@ -61,12 +80,25 @@ struct NotificationApp {
|
||||
uint8_t display_led_lock;
|
||||
|
||||
NotificationSettings settings;
|
||||
RGBBacklightApp* rgb_srv;
|
||||
|
||||
FuriTimer* night_shift_timer;
|
||||
float current_night_shift;
|
||||
|
||||
FuriTimer* rainbow_timer;
|
||||
uint16_t rainbow_hue;
|
||||
uint8_t rainbow_red;
|
||||
uint8_t rainbow_green;
|
||||
uint8_t rainbow_blue;
|
||||
};
|
||||
|
||||
void notification_message_save_settings(NotificationApp* app);
|
||||
void night_shift_timer_start(NotificationApp* app);
|
||||
void night_shift_timer_stop(NotificationApp* app);
|
||||
void rgb_backlight_update(float brightness);
|
||||
void rgb_backlight_set_led_static_color(uint8_t led, uint8_t index);
|
||||
void rainbow_timer_start(NotificationApp* app);
|
||||
void rainbow_timer_stop(NotificationApp* app);
|
||||
void rainbow_timer_starter(NotificationApp* app);
|
||||
const char* rgb_backlight_get_color_text(uint8_t index);
|
||||
uint8_t rgb_backlight_get_color_count(void);
|
||||
void set_rgb_backlight_installed_variable(uint8_t var);
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
App(
|
||||
appid="rgb_backlight",
|
||||
name="RgbBackLightSrv",
|
||||
apptype=FlipperAppType.SERVICE,
|
||||
entry_point="rgb_backlight_srv",
|
||||
cdefines=["SRV_RGB_BACKLIGHT"],
|
||||
stack_size=1 * 1024,
|
||||
order=1290,
|
||||
sdk_headers=["rgb_backlight.h"],
|
||||
)
|
||||
@@ -1,270 +0,0 @@
|
||||
/*
|
||||
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"
|
||||
#include <math.h>
|
||||
|
||||
#define COLOR_COUNT (sizeof(colors) / sizeof(RGBBacklightColor))
|
||||
|
||||
#define TAG "RGB_BACKLIGHT_SRV"
|
||||
|
||||
typedef struct {
|
||||
char* name;
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
uint8_t blue;
|
||||
} RGBBacklightColor;
|
||||
|
||||
// use one type RGBBacklightColor for current_leds_settings and for static colors definition
|
||||
static RGBBacklightColor current_led[] = {
|
||||
{"LED0", 0, 0, 0},
|
||||
{"LED1", 0, 0, 0},
|
||||
{"LED2", 0, 0, 0},
|
||||
};
|
||||
|
||||
static const RGBBacklightColor 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},
|
||||
{"OFF", 0, 0, 0},
|
||||
};
|
||||
|
||||
uint8_t rgb_backlight_get_color_count(void) {
|
||||
return COLOR_COUNT;
|
||||
}
|
||||
|
||||
const char* rgb_backlight_get_color_text(uint8_t index) {
|
||||
return colors[index].name;
|
||||
}
|
||||
|
||||
// use RECORD for acces to rgb service instance and update current colors by static
|
||||
void rgb_backlight_set_led_static_color(uint8_t led, uint8_t index) {
|
||||
if(led < SK6805_get_led_count()) {
|
||||
uint8_t r = colors[index].red;
|
||||
uint8_t g = colors[index].green;
|
||||
uint8_t b = colors[index].blue;
|
||||
|
||||
current_led[led].red = r;
|
||||
current_led[led].green = g;
|
||||
current_led[led].blue = b;
|
||||
|
||||
SK6805_set_led_color(led, r, g, b);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// led number (0-2), hue (0..255), sat (0..255), val (0...1)
|
||||
void rgb_backlight_set_led_custom_hsv_color(uint8_t led, uint16_t hue, uint8_t sat, float V) {
|
||||
// init value
|
||||
float r = 1.0f;
|
||||
float g = 1.0f;
|
||||
float b = 1.0f;
|
||||
|
||||
// from (0..255) to (0..1)
|
||||
float H = hue / 255.0f;
|
||||
float S = sat / 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;
|
||||
}
|
||||
|
||||
// from (0..1) to (0..255)
|
||||
current_led[led].red = r * 255;
|
||||
current_led[led].green = g * 255;
|
||||
current_led[led].blue = b * 255;
|
||||
}
|
||||
|
||||
// use RECORD for acces to rgb service instance, set current_* colors to led and update backlight
|
||||
void rgb_backlight_update(float brightness) {
|
||||
RGBBacklightApp* app = furi_record_open(RECORD_RGB_BACKLIGHT);
|
||||
|
||||
if(app->settings->rgb_backlight_installed) {
|
||||
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
|
||||
uint8_t r = current_led[i].red * brightness * 1.0f;
|
||||
uint8_t g = current_led[i].green * brightness * 1.0f;
|
||||
uint8_t b = current_led[i].blue * brightness * 1.0f;
|
||||
SK6805_set_led_color(i, r, g, b);
|
||||
}
|
||||
SK6805_update();
|
||||
}
|
||||
furi_record_close(RECORD_RGB_BACKLIGHT);
|
||||
}
|
||||
|
||||
// start furi timer for rainbow
|
||||
void rainbow_timer_start(RGBBacklightApp* app) {
|
||||
if(furi_timer_is_running(app->rainbow_timer)) {
|
||||
furi_timer_stop(app->rainbow_timer);
|
||||
}
|
||||
furi_timer_start(app->rainbow_timer, furi_ms_to_ticks(app->settings->rainbow_speed_ms));
|
||||
}
|
||||
|
||||
// stop furi timer for rainbow
|
||||
void rainbow_timer_stop(RGBBacklightApp* app) {
|
||||
if(furi_timer_is_running(app->rainbow_timer)) {
|
||||
furi_timer_stop(app->rainbow_timer);
|
||||
}
|
||||
}
|
||||
|
||||
// if rgb_backlight_installed then apply rainbow colors to backlight and start/restart/stop rainbow_timer
|
||||
void rainbow_timer_starter(RGBBacklightApp* app) {
|
||||
if((app->settings->rainbow_mode > 0) && (app->settings->rgb_backlight_installed)) {
|
||||
rainbow_timer_start(app);
|
||||
}
|
||||
}
|
||||
|
||||
static void rainbow_timer_callback(void* context) {
|
||||
furi_assert(context);
|
||||
RGBBacklightApp* app = context;
|
||||
|
||||
if(app->settings->rgb_backlight_installed) {
|
||||
app->rainbow_hue += app->settings->rainbow_step;
|
||||
if(app->rainbow_hue > 254) {
|
||||
app->rainbow_hue = 0;
|
||||
}
|
||||
|
||||
uint8_t wide = app->settings->rainbow_wide;
|
||||
|
||||
switch(app->settings->rainbow_mode) {
|
||||
//rainbow mode
|
||||
case 1:
|
||||
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
i,
|
||||
app->rainbow_hue,
|
||||
app->settings->rainbow_saturation,
|
||||
app->settings->brightness);
|
||||
}
|
||||
break;
|
||||
|
||||
//wave mode
|
||||
case 2:
|
||||
uint16_t j = app->rainbow_hue + wide;
|
||||
uint16_t k = app->rainbow_hue + wide * 2;
|
||||
|
||||
if(app->rainbow_hue > (254 - wide)) {
|
||||
j = j - 255;
|
||||
}
|
||||
if(app->rainbow_hue > (254 - wide * 2)) {
|
||||
k = k - 255;
|
||||
}
|
||||
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
0, app->rainbow_hue, app->settings->rainbow_saturation, app->settings->brightness);
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
1, j, app->settings->rainbow_saturation, app->settings->brightness);
|
||||
rgb_backlight_set_led_custom_hsv_color(
|
||||
2, k, app->settings->rainbow_saturation, app->settings->brightness);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
rgb_backlight_update(app->settings->brightness * app->current_night_shift);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t rgb_backlight_srv(void* p) {
|
||||
UNUSED(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));
|
||||
|
||||
// define rainbow_timer and they callback
|
||||
app->rainbow_timer = furi_timer_alloc(rainbow_timer_callback, FuriTimerTypePeriodic, app);
|
||||
|
||||
// settings load or create default
|
||||
app->settings = malloc(sizeof(RGBBacklightSettings));
|
||||
rgb_backlight_settings_load(app->settings);
|
||||
|
||||
app->rainbow_hue = 1;
|
||||
app->current_night_shift = 1.0f;
|
||||
|
||||
furi_record_create(RECORD_RGB_BACKLIGHT, app);
|
||||
|
||||
// if rgb_backlight_installed then start rainbow or set leds colors from saved settings (default index = 0)
|
||||
if(app->settings->rgb_backlight_installed) {
|
||||
if(app->settings->rainbow_mode > 0) {
|
||||
rainbow_timer_start(app);
|
||||
} else {
|
||||
rgb_backlight_set_led_static_color(2, app->settings->led_2_color_index);
|
||||
rgb_backlight_set_led_static_color(1, app->settings->led_1_color_index);
|
||||
rgb_backlight_set_led_static_color(0, app->settings->led_0_color_index);
|
||||
rgb_backlight_update(app->settings->brightness * app->current_night_shift);
|
||||
}
|
||||
// if rgb_backlight not installed then set default static orange color(index=0) to all leds (0-2) and force light on
|
||||
} else {
|
||||
rgb_backlight_set_led_static_color(2, 0);
|
||||
rgb_backlight_set_led_static_color(1, 0);
|
||||
rgb_backlight_set_led_static_color(0, 0);
|
||||
SK6805_update();
|
||||
}
|
||||
|
||||
while(1) {
|
||||
furi_delay_ms(5000);
|
||||
if(app->settings->rgb_backlight_installed) {
|
||||
FURI_LOG_D(TAG, "RGB backlight enabled - serivce is running");
|
||||
} else {
|
||||
FURI_LOG_D(TAG, "RGB backlight DISABLED - serivce is running");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <furi.h>
|
||||
#include "rgb_backlight_settings.h"
|
||||
#include "SK6805.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
FuriTimer* rainbow_timer;
|
||||
uint16_t rainbow_hue;
|
||||
uint8_t rainbow_red;
|
||||
uint8_t rainbow_green;
|
||||
uint8_t rainbow_blue;
|
||||
RGBBacklightSettings* settings;
|
||||
// night_shift multiplicator for leds brightnes coming from Notificatoin app.
|
||||
float current_night_shift;
|
||||
|
||||
} RGBBacklightApp;
|
||||
|
||||
#define RECORD_RGB_BACKLIGHT "rgb_backlight"
|
||||
|
||||
/** Update leds colors from current_led[i].color and selected bright
|
||||
*
|
||||
* @param brightness - Brightness 0..1
|
||||
* @return
|
||||
*/
|
||||
void rgb_backlight_update(float brightness);
|
||||
|
||||
/** Set current_led[i].color for one led by static color index
|
||||
*
|
||||
* @param led - Led number (0..2)
|
||||
* @param index - Static color index number
|
||||
* @return
|
||||
*/
|
||||
void rgb_backlight_set_led_static_color(uint8_t led, uint8_t index);
|
||||
|
||||
/** Stop rainbow timer
|
||||
*
|
||||
* @param app - Instance of RGBBacklightApp from FURI RECORD
|
||||
* @return
|
||||
*/
|
||||
void rainbow_timer_stop(RGBBacklightApp* app);
|
||||
|
||||
/** Start rainbow timer
|
||||
*
|
||||
* @param app - Instance of RGBBacklightApp from FURI RECORD
|
||||
* @return
|
||||
*/
|
||||
|
||||
/** Start rainbow timer
|
||||
*
|
||||
* @param app - Instance of RGBBacklightApp from FURI RECORD
|
||||
* @return
|
||||
*/
|
||||
void rainbow_timer_start(RGBBacklightApp* app);
|
||||
|
||||
/** Start rainbow timer only if all conditions meet (rgb_backlight_installed && rainbow ON)
|
||||
*
|
||||
* @param app - Instance of RGBBacklightApp from FURI RECORD
|
||||
* @return
|
||||
*/
|
||||
void rainbow_timer_starter(RGBBacklightApp* app);
|
||||
|
||||
/** Get name of static color by index
|
||||
*
|
||||
* @param index - Static colors index number
|
||||
* @return - color name
|
||||
*/
|
||||
const char* rgb_backlight_get_color_text(uint8_t index);
|
||||
|
||||
/** Get static colors count
|
||||
*
|
||||
* @param
|
||||
* @return - colors count
|
||||
*/
|
||||
uint8_t rgb_backlight_get_color_count(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,109 +0,0 @@
|
||||
#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 (4) // Previous version number
|
||||
#define RGB_BACKLIGHT_SETTINGS_VER (5) // Current version number
|
||||
|
||||
//pervious settings must be copyed from previous rgb_backlight_settings.h file
|
||||
typedef struct {
|
||||
//Common settings
|
||||
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;
|
||||
uint8_t rainbow_wide;
|
||||
|
||||
} 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");
|
||||
memset(settings, 0, sizeof(RGBBacklightSettings));
|
||||
settings->brightness = 1.0f;
|
||||
settings->rainbow_speed_ms = 100;
|
||||
settings->rainbow_step = 1;
|
||||
settings->rainbow_saturation = 255;
|
||||
settings->rainbow_wide = 50;
|
||||
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");
|
||||
} else {
|
||||
FURI_LOG_I(TAG, "Settings saved");
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
//Common settings
|
||||
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;
|
||||
uint8_t rainbow_wide;
|
||||
|
||||
} RGBBacklightSettings;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void rgb_backlight_settings_load(RGBBacklightSettings* settings);
|
||||
void rgb_backlight_settings_save(const RGBBacklightSettings* settings);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user