Skip to content

Commit

Permalink
adding rbg support
Browse files Browse the repository at this point in the history
  • Loading branch information
DXVVAY committed Oct 29, 2023
1 parent 1472301 commit da6d0e0
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 52 deletions.
5 changes: 1 addition & 4 deletions applications/services/cli/cli_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ void cli_command_help(Cli* cli, FuriString* args, void* context) {
}
}




void cli_command_uptime(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
UNUSED(args);
Expand Down Expand Up @@ -149,7 +146,7 @@ void cli_command_src(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
UNUSED(args);
UNUSED(context);

printf("Xvirus firmware is a fork of the unleashed firmware");
printf("https://github.com/Xvirus-Team/xvirus-firmware/");
}

Expand Down
21 changes: 10 additions & 11 deletions applications/services/notification/notification_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "notification.h"
#include "notification_messages.h"
#include "notification_app.h"
#include "applications/settings/notification_settings/rgb_backlight.h"

#define TAG "NotificationSrv"

Expand Down Expand Up @@ -228,7 +229,7 @@ static void notification_process_notification_message(
}
break;
case NotificationMessageTypeLedDisplayBacklightEnforceOn:
furi_check(app->display_led_lock < UINT8_MAX);
furi_assert(app->display_led_lock < UINT8_MAX);
app->display_led_lock++;
if(app->display_led_lock == 1) {
notification_apply_internal_led_layer(
Expand All @@ -237,15 +238,12 @@ static void notification_process_notification_message(
}
break;
case NotificationMessageTypeLedDisplayBacklightEnforceAuto:
if(app->display_led_lock > 0) {
app->display_led_lock--;
if(app->display_led_lock == 0) {
notification_apply_internal_led_layer(
&app->display,
notification_message->data.led.value * display_brightness_setting);
}
} else {
FURI_LOG_E(TAG, "Incorrect BacklightEnforce use");
furi_assert(app->display_led_lock > 0);
app->display_led_lock--;
if(app->display_led_lock == 0) {
notification_apply_internal_led_layer(
&app->display,
notification_message->data.led.value * display_brightness_setting);
}
break;
case NotificationMessageTypeLedRed:
Expand Down Expand Up @@ -589,6 +587,7 @@ int32_t notification_srv(void* p) {
break;
case SaveSettingsMessage:
notification_save_settings(app);
rgb_backlight_save_settings();
break;
}

Expand All @@ -598,4 +597,4 @@ int32_t notification_srv(void* p) {
}

return 0;
};
};
26 changes: 6 additions & 20 deletions applications/settings/about/about.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ static DialogMessageButton compliance_screen(DialogsApp* dialogs, DialogMessage*
return result;
}

static DialogMessageButton unleashed_info_screen(DialogsApp* dialogs, DialogMessage* message) {
static DialogMessageButton xvirus_info_screen(DialogsApp* dialogs, DialogMessage* message) {
DialogMessageButton result;

const char* screen_header = "Unleashed Firmware\n";
const char* screen_header = "Xvirus Firmware\n";

const char* screen_text = "Play with caution.\n"
"Not for illegal use!";
const char* screen_text = "Forked With Love <3\n"
"For More Info Join discord\n"
"Discord.gg/xvirustool\n";

dialog_message_set_header(message, screen_header, 0, 0, AlignLeft, AlignTop);
dialog_message_set_text(message, screen_text, 0, 26, AlignLeft, AlignTop);
Expand All @@ -86,20 +87,6 @@ static DialogMessageButton unleashed_info_screen(DialogsApp* dialogs, DialogMess
return result;
}

static DialogMessageButton unleashed_info_screen2(DialogsApp* dialogs, DialogMessage* message) {
DialogMessageButton result;

const char* screen_text = "Custom plugins included\n"
"For updates & info visit\n"
"github.com/DarkFlippers";

dialog_message_set_text(message, screen_text, 0, 0, AlignLeft, AlignTop);
result = dialog_message_show(dialogs, message);
dialog_message_set_text(message, NULL, 0, 0, AlignLeft, AlignTop);

return result;
}

static DialogMessageButton icon1_screen(DialogsApp* dialogs, DialogMessage* message) {
DialogMessageButton result;

Expand Down Expand Up @@ -195,8 +182,7 @@ static DialogMessageButton fw_version_screen(DialogsApp* dialogs, DialogMessage*
}

const AboutDialogScreen about_screens[] = {
unleashed_info_screen,
unleashed_info_screen2,
xvirus_info_screen,
product_screen,
compliance_screen,
address_screen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gui/modules/variable_item_list.h>
#include <gui/view_dispatcher.h>
#include <lib/toolbox/value_index.h>
#include <applications/settings/notification_settings/rgb_backlight.h>

#define MAX_NOTIFICATION_SETTINGS 4

Expand Down Expand Up @@ -156,6 +157,14 @@ static void vibro_changed(VariableItem* item) {
notification_message(app->notification, &sequence_single_vibro);
}

static void color_changed(VariableItem* item) {
NotificationAppSettings* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
rgb_backlight_set_color(index);
variable_item_set_current_value_text(item, rgb_backlight_get_color_text(index));
notification_message(app->notification, &sequence_display_backlight_on);
}

static uint32_t notification_app_settings_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
Expand All @@ -181,7 +190,13 @@ static NotificationAppSettings* alloc_settings() {
variable_item_set_current_value_text(item, contrast_text[value_index]);

item = variable_item_list_add(
app->variable_item_list, "LCD Backlight", BACKLIGHT_COUNT, backlight_changed, app);
app->variable_item_list, "LCD Color", rgb_backlight_get_color_count(), color_changed, app);
value_index = rgb_backlight_get_settings()->display_color_index;
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, rgb_backlight_get_color_text(value_index));

item = variable_item_list_add(
app->variable_item_list, "LCD Brightness", BACKLIGHT_COUNT, backlight_changed, app);
value_index = value_index_float(
app->notification->settings.display_brightness, backlight_value, BACKLIGHT_COUNT);
variable_item_set_current_value_index(item, value_index);
Expand Down Expand Up @@ -255,4 +270,4 @@ int32_t notification_settings_app(void* p) {
notification_message_save_settings(app->notification);
free_settings(app);
return 0;
}
}
182 changes: 182 additions & 0 deletions applications/settings/notification_settings/rgb_backlight.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
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 "rgb_backlight.h"
#include <furi_hal.h>
#include <storage/storage.h>

#define RGB_BACKLIGHT_SETTINGS_VERSION 5
#define RGB_BACKLIGHT_SETTINGS_FILE_NAME ".rgb_backlight.settings"
#define RGB_BACKLIGHT_SETTINGS_PATH EXT_PATH(RGB_BACKLIGHT_SETTINGS_FILE_NAME)

#define COLOR_COUNT (sizeof(colors) / sizeof(RGBBacklightColor))

#define TAG "RGB Backlight"

static RGBBacklightSettings rgb_settings = {
.version = RGB_BACKLIGHT_SETTINGS_VERSION,
.display_color_index = 0,
.settings_is_loaded = false
};

static const RGBBacklightColor colors[] = {
{"Orange", 255, 79, 0},
{"Yellow", 255, 170, 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", 140, 140, 140},
{"Violet", 138, 43, 226},
{"Teal", 0, 128, 128},
{"Gold", 255, 215, 0},
{"Silver", 192, 192, 192},
{"Maroon", 128, 0, 0},
{"Olive", 128, 128, 0},
{"Indigo", 75, 0, 130},
{"Turquoise", 64, 224, 208}
};


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;
}

void rgb_backlight_load_settings(void) {
// Do not load data from internal memory in DFU mode
FuriHalRtcBootMode bm = furi_hal_rtc_get_boot_mode();
if(bm == FuriHalRtcBootModeDfu) {
rgb_settings.settings_is_loaded = true;
return;
}

RGBBacklightSettings settings;
File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
const size_t settings_size = sizeof(RGBBacklightSettings);

FURI_LOG_I(TAG, "loading settings from \"%s\"", RGB_BACKLIGHT_SETTINGS_PATH);
bool fs_result =
storage_file_open(file, RGB_BACKLIGHT_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);

if(fs_result) {
uint16_t bytes_count = storage_file_read(file, &settings, settings_size);

if(bytes_count != settings_size) {
fs_result = false;
}
}

if(fs_result) {
FURI_LOG_I(TAG, "load success");
if(settings.version != RGB_BACKLIGHT_SETTINGS_VERSION) {
FURI_LOG_E(
TAG,
"version(%d != %d) mismatch",
settings.version,
RGB_BACKLIGHT_SETTINGS_VERSION
);
} else {
memcpy(&rgb_settings, &settings, settings_size);
}
} else {
FURI_LOG_E(TAG, "load failed, %s", storage_file_get_error_desc(file));
}

storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
rgb_settings.settings_is_loaded = true;
}

void rgb_backlight_save_settings(void) {
RGBBacklightSettings settings;
File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
const size_t settings_size = sizeof(RGBBacklightSettings);

FURI_LOG_I(TAG, "saving settings to \"%s\"", RGB_BACKLIGHT_SETTINGS_PATH);

memcpy(&settings, &rgb_settings, settings_size);

bool fs_result =
storage_file_open(file, RGB_BACKLIGHT_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);

if(fs_result) {
uint16_t bytes_count = storage_file_write(file, &settings, settings_size);

if(bytes_count != settings_size) {
fs_result = false;
}
}

if(fs_result) {
FURI_LOG_I(TAG, "save success");
} else {
FURI_LOG_E(TAG, "save failed, %s", storage_file_get_error_desc(file));
}

storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}

RGBBacklightSettings* rgb_backlight_get_settings(void) {
if(!rgb_settings.settings_is_loaded) {
rgb_backlight_load_settings();
}
return &rgb_settings;
}

void rgb_backlight_set_color(uint8_t color_index) {
if(color_index > (rgb_backlight_get_color_count() - 1)) color_index = 0;
rgb_settings.display_color_index = color_index;
}

void rgb_backlight_update(uint8_t brightness) {
if(!rgb_settings.settings_is_loaded) {
rgb_backlight_load_settings();
}

static uint8_t last_color_index = 255;
static uint8_t last_brightness = 123;
static uint32_t last_update_time = 0;

if(last_brightness == brightness && last_color_index == rgb_settings.display_color_index &&
furi_get_tick() - last_update_time < 1000)
return;

last_brightness = brightness;
last_color_index = rgb_settings.display_color_index;
last_update_time = furi_get_tick();

for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
uint8_t r = colors[rgb_settings.display_color_index].red * (brightness / 255.0f);
uint8_t g = colors[rgb_settings.display_color_index].green * (brightness / 255.0f);
uint8_t b = colors[rgb_settings.display_color_index].blue * (brightness / 255.0f);

SK6805_set_led_color(i, r, g, b);
}

SK6805_update();
}
Loading

0 comments on commit da6d0e0

Please sign in to comment.