Skip to content

Commit

Permalink
RGB Matrix eeprom write limiting (#13238)
Browse files Browse the repository at this point in the history
  • Loading branch information
XScorpion2 committed Jun 20, 2021
1 parent 3c79012 commit 5b7cf9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
30 changes: 16 additions & 14 deletions quantum/led_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ const led_point_t k_led_matrix_center = {112, 32};
const led_point_t k_led_matrix_center = LED_MATRIX_CENTER;
#endif

// clang-format off
#ifndef LED_MATRIX_IMMEDIATE_EEPROM
# define led_eeconfig_update(v) led_update_eeprom |= v
#else
# define led_eeconfig_update(v) if (v) eeconfig_update_led_matrix()
#endif
// clang-format on

// Generic effect runners
#include "led_matrix_runners/effect_runner_dx_dy_dist.h"
#include "led_matrix_runners/effect_runner_dx_dy.h"
Expand Down Expand Up @@ -104,6 +112,7 @@ last_hit_t g_last_hit_tracker;

// internals
static bool suspend_state = false;
static bool led_update_eeprom = false;
static uint8_t led_last_enable = UINT8_MAX;
static uint8_t led_last_effect = UINT8_MAX;
static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false};
Expand Down Expand Up @@ -276,6 +285,7 @@ static void led_task_timers(void) {

static void led_task_sync(void) {
// next task
if (led_update_eeprom) eeconfig_update_led_matrix();
if (sync_timer_elapsed32(g_led_timer) >= LED_MATRIX_LED_FLUSH_LIMIT) led_task_state = STARTING;
}

Expand Down Expand Up @@ -465,17 +475,15 @@ bool led_matrix_get_suspend_state(void) { return suspend_state; }
void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
led_matrix_eeconfig.enable ^= 1;
led_task_state = STARTING;
if (write_to_eeprom) {
eeconfig_update_led_matrix();
}
led_eeconfig_update(write_to_eeprom);
dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable);
}
void led_matrix_toggle_noeeprom(void) { led_matrix_toggle_eeprom_helper(false); }
void led_matrix_toggle(void) { led_matrix_toggle_eeprom_helper(true); }

void led_matrix_enable(void) {
led_matrix_enable_noeeprom();
eeconfig_update_led_matrix();
led_eeconfig_update(true);
}

void led_matrix_enable_noeeprom(void) {
Expand All @@ -485,7 +493,7 @@ void led_matrix_enable_noeeprom(void) {

void led_matrix_disable(void) {
led_matrix_disable_noeeprom();
eeconfig_update_led_matrix();
led_eeconfig_update(true);
}

void led_matrix_disable_noeeprom(void) {
Expand All @@ -507,9 +515,7 @@ void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
led_matrix_eeconfig.mode = mode;
}
led_task_state = STARTING;
if (write_to_eeprom) {
eeconfig_update_led_matrix();
}
led_eeconfig_update(write_to_eeprom);
dprintf("led matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.mode);
}
void led_matrix_mode_noeeprom(uint8_t mode) { led_matrix_mode_eeprom_helper(mode, false); }
Expand All @@ -536,9 +542,7 @@ void led_matrix_set_val_eeprom_helper(uint8_t val, bool write_to_eeprom) {
return;
}
led_matrix_eeconfig.val = (val > LED_MATRIX_MAXIMUM_BRIGHTNESS) ? LED_MATRIX_MAXIMUM_BRIGHTNESS : val;
if (write_to_eeprom) {
eeconfig_update_led_matrix();
}
led_eeconfig_update(write_to_eeprom);
dprintf("led matrix set val [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.val);
}
void led_matrix_set_val_noeeprom(uint8_t val) { led_matrix_set_val_eeprom_helper(val, false); }
Expand All @@ -556,9 +560,7 @@ void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); }

void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
led_matrix_eeconfig.speed = speed;
if (write_to_eeprom) {
eeconfig_update_led_matrix();
}
led_eeconfig_update(write_to_eeprom);
dprintf("led matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.speed);
}
void led_matrix_set_speed_noeeprom(uint8_t speed) { led_matrix_set_speed_eeprom_helper(speed, false); }
Expand Down
30 changes: 16 additions & 14 deletions quantum/rgb_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ const led_point_t k_rgb_matrix_center = {112, 32};
const led_point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
#endif

// clang-format off
#ifndef RGB_MATRIX_IMMEDIATE_EEPROM
# define rgb_eeconfig_update(v) rgb_update_eeprom |= v
#else
# define rgb_eeconfig_update(v) if (v) eeconfig_update_rgb_matrix()
#endif
// clang-format on

__attribute__((weak)) RGB rgb_matrix_hsv_to_rgb(HSV hsv) { return hsv_to_rgb(hsv); }

// Generic effect runners
Expand Down Expand Up @@ -125,6 +133,7 @@ last_hit_t g_last_hit_tracker;

// internals
static bool suspend_state = false;
static bool rgb_update_eeprom = false;
static uint8_t rgb_last_enable = UINT8_MAX;
static uint8_t rgb_last_effect = UINT8_MAX;
static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false};
Expand Down Expand Up @@ -311,6 +320,7 @@ static void rgb_task_timers(void) {

static void rgb_task_sync(void) {
// next task
if (rgb_update_eeprom) eeconfig_update_rgb_matrix();
if (sync_timer_elapsed32(g_rgb_timer) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
}

Expand Down Expand Up @@ -507,17 +517,15 @@ bool rgb_matrix_get_suspend_state(void) { return suspend_state; }
void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) {
rgb_matrix_config.enable ^= 1;
rgb_task_state = STARTING;
if (write_to_eeprom) {
eeconfig_update_rgb_matrix();
}
rgb_eeconfig_update(write_to_eeprom);
dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable);
}
void rgb_matrix_toggle_noeeprom(void) { rgb_matrix_toggle_eeprom_helper(false); }
void rgb_matrix_toggle(void) { rgb_matrix_toggle_eeprom_helper(true); }

void rgb_matrix_enable(void) {
rgb_matrix_enable_noeeprom();
eeconfig_update_rgb_matrix();
rgb_eeconfig_update(true);
}

void rgb_matrix_enable_noeeprom(void) {
Expand All @@ -527,7 +535,7 @@ void rgb_matrix_enable_noeeprom(void) {

void rgb_matrix_disable(void) {
rgb_matrix_disable_noeeprom();
eeconfig_update_rgb_matrix();
rgb_eeconfig_update(true);
}

void rgb_matrix_disable_noeeprom(void) {
Expand All @@ -549,9 +557,7 @@ void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
rgb_matrix_config.mode = mode;
}
rgb_task_state = STARTING;
if (write_to_eeprom) {
eeconfig_update_rgb_matrix();
}
rgb_eeconfig_update(write_to_eeprom);
dprintf("rgb matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.mode);
}
void rgb_matrix_mode_noeeprom(uint8_t mode) { rgb_matrix_mode_eeprom_helper(mode, false); }
Expand Down Expand Up @@ -580,9 +586,7 @@ void rgb_matrix_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, boo
rgb_matrix_config.hsv.h = hue;
rgb_matrix_config.hsv.s = sat;
rgb_matrix_config.hsv.v = (val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) ? RGB_MATRIX_MAXIMUM_BRIGHTNESS : val;
if (write_to_eeprom) {
eeconfig_update_rgb_matrix();
}
rgb_eeconfig_update(write_to_eeprom);
dprintf("rgb matrix set hsv [%s]: %u,%u,%u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v);
}
void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { rgb_matrix_sethsv_eeprom_helper(hue, sat, val, false); }
Expand Down Expand Up @@ -619,9 +623,7 @@ void rgb_matrix_decrease_val(void) { rgb_matrix_decrease_val_helper(true); }

void rgb_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) {
rgb_matrix_config.speed = speed;
if (write_to_eeprom) {
eeconfig_update_rgb_matrix();
}
rgb_eeconfig_update(write_to_eeprom);
dprintf("rgb matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.speed);
}
void rgb_matrix_set_speed_noeeprom(uint8_t speed) { rgb_matrix_set_speed_eeprom_helper(speed, false); }
Expand Down

0 comments on commit 5b7cf9f

Please sign in to comment.