Skip to content

Commit

Permalink
sexify rgb code
Browse files Browse the repository at this point in the history
  • Loading branch information
DXVVAY committed Oct 29, 2023
1 parent bbcb133 commit 124ef42
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ static const NotificationSequence sequence_note_c = {
NULL,
};

#define CONTRAST_COUNT 11
#define CONTRAST_COUNT 21
const char* const contrast_text[CONTRAST_COUNT] = {
"-10",
"-9",
"-8",
"-7",
"-6",
"-5",
"-4",
"-3",
Expand All @@ -34,8 +39,18 @@ const char* const contrast_text[CONTRAST_COUNT] = {
"+3",
"+4",
"+5",
"+6",
"+7",
"+8",
"+9",
"+10",
};
const int32_t contrast_value[CONTRAST_COUNT] = {
-10,
-9,
-8,
-7,
-6,
-5,
-4,
-3,
Expand All @@ -47,6 +62,11 @@ const int32_t contrast_value[CONTRAST_COUNT] = {
3,
4,
5,
6,
7,
8,
9,
10,
};

#define BACKLIGHT_COUNT 21
Expand Down Expand Up @@ -181,6 +201,12 @@ static NotificationAppSettings* alloc_settings() {

VariableItem* item;
uint8_t value_index;

item = variable_item_list_add(
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 Contrast", CONTRAST_COUNT, contrast_changed, app);
Expand All @@ -189,12 +215,6 @@ static NotificationAppSettings* alloc_settings() {
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, contrast_text[value_index]);

item = variable_item_list_add(
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(
Expand Down
74 changes: 42 additions & 32 deletions applications/settings/notification_settings/rgb_backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

//Code modified by @dexv
#include "rgb_backlight.h"
#include <furi_hal.h>
#include <storage/storage.h>
Expand Down Expand Up @@ -67,7 +67,7 @@ const char* rgb_backlight_get_color_text(uint8_t index) {
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) {
if (bm == FuriHalRtcBootModeDfu) {
rgb_settings.settings_is_loaded = true;
return;
}
Expand All @@ -76,21 +76,29 @@ void rgb_backlight_load_settings(void) {
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);
bool fs_result = false;

if (file != NULL) {
FURI_LOG_I(TAG, "loading settings from \"%s\"", RGB_BACKLIGHT_SETTINGS_PATH);
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 (fs_result) {
uint16_t bytes_count = storage_file_read(file, &settings, settings_size);

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

storage_file_close(file);
storage_file_free(file);
}

if(fs_result) {
furi_record_close(RECORD_STORAGE);

if (fs_result) {
FURI_LOG_I(TAG, "load success");
if(settings.version != RGB_BACKLIGHT_SETTINGS_VERSION) {
if (settings.version != RGB_BACKLIGHT_SETTINGS_VERSION) {
FURI_LOG_E(
TAG,
"version(%d != %d) mismatch",
Expand All @@ -104,9 +112,6 @@ void rgb_backlight_load_settings(void) {
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;
}

Expand All @@ -119,28 +124,33 @@ void rgb_backlight_save_settings(void) {

memcpy(&settings, &rgb_settings, settings_size);

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

if (file != NULL) {
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 (fs_result) {
uint16_t bytes_count = storage_file_write(file, &settings, settings_size);

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

storage_file_close(file);
storage_file_free(file);
}

if(fs_result) {
furi_record_close(RECORD_STORAGE);

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();
Expand All @@ -154,27 +164,27 @@ void rgb_backlight_set_color(uint8_t color_index) {
}

void rgb_backlight_update(uint8_t brightness) {
if(!rgb_settings.settings_is_loaded) {
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)
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);
uint8_t r = colors[rgb_settings.display_color_index].red * ((float)brightness / 255.0f);
uint8_t g = colors[rgb_settings.display_color_index].green * ((float)brightness / 255.0f);
uint8_t b = colors[rgb_settings.display_color_index].blue * ((float)brightness / 255.0f);

for (uint8_t i = 0; i < SK6805_get_led_count(); i++) {
SK6805_set_led_color(i, r, g, b);
}

Expand Down

0 comments on commit 124ef42

Please sign in to comment.