summaryrefslogtreecommitdiff
path: root/quantum/rgb_matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/rgb_matrix.c')
-rw-r--r--quantum/rgb_matrix.c106
1 files changed, 63 insertions, 43 deletions
diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c
index 91032b6564..f3da0ab0f7 100644
--- a/quantum/rgb_matrix.c
+++ b/quantum/rgb_matrix.c
@@ -57,8 +57,12 @@ const point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
// -----End rgb effect includes macros-------
// ------------------------------------------
-#ifndef RGB_DISABLE_AFTER_TIMEOUT
-# define RGB_DISABLE_AFTER_TIMEOUT 0
+#if defined(RGB_DISABLE_AFTER_TIMEOUT) && !defined(RGB_DISABLE_TIMEOUT)
+# define RGB_DISABLE_TIMEOUT (RGB_DISABLE_AFTER_TIMEOUT * 1200)
+#endif
+
+#ifndef RGB_DISABLE_TIMEOUT
+# define RGB_DISABLE_TIMEOUT 0
#endif
#ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
@@ -111,19 +115,29 @@ const point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
# define RGB_MATRIX_STARTUP_SPD UINT8_MAX / 2
#endif
-bool g_suspend_state = false;
-
-rgb_config_t rgb_matrix_config;
-
-rgb_counters_t g_rgb_counters;
-static uint32_t rgb_counters_buffer;
-
+// globals
+bool g_suspend_state = false;
+rgb_config_t rgb_matrix_config; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr
+uint32_t g_rgb_timer;
#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
-uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
-#endif
+uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
+#endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS
+#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
+last_hit_t g_last_hit_tracker;
+#endif // RGB_MATRIX_KEYREACTIVE_ENABLED
+// internals
+static uint8_t rgb_last_enable = UINT8_MAX;
+static uint8_t rgb_last_effect = UINT8_MAX;
+static effect_params_t rgb_effect_params = {0, 0xFF};
+static rgb_task_states rgb_task_state = SYNCING;
+#if RGB_DISABLE_TIMEOUT > 0
+static uint32_t rgb_anykey_timer;
+#endif // RGB_DISABLE_TIMEOUT > 0
+
+// double buffers
+static uint32_t rgb_timer_buffer;
#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
-last_hit_t g_last_hit_tracker;
static last_hit_t last_hit_buffer;
#endif // RGB_MATRIX_KEYREACTIVE_ENABLED
@@ -169,21 +183,24 @@ void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color_all(red, green, blue); }
bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
+#if RGB_DISABLE_TIMEOUT > 0
+ if (record->event.pressed) {
+ rgb_anykey_timer = 0;
+ }
+#endif // RGB_DISABLE_TIMEOUT > 0
+
#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
uint8_t led[LED_HITS_TO_REMEMBER];
uint8_t led_count = 0;
# if defined(RGB_MATRIX_KEYRELEASES)
- if (!record->event.pressed) {
- led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
- g_rgb_counters.any_key_hit = 0;
- }
+ if (!record->event.pressed)
# elif defined(RGB_MATRIX_KEYPRESSES)
- if (record->event.pressed) {
- led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
- g_rgb_counters.any_key_hit = 0;
- }
+ if (record->event.pressed)
# endif // defined(RGB_MATRIX_KEYRELEASES)
+ {
+ led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
+ }
if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
@@ -216,7 +233,7 @@ void rgb_matrix_test(void) {
// Mask out bits 4 and 5
// Increase the factor to make the test animation slower (and reduce to make it faster)
uint8_t factor = 10;
- switch ((g_rgb_counters.tick & (0b11 << factor)) >> factor) {
+ switch ((g_rgb_timer & (0b11 << factor)) >> factor) {
case 0: {
rgb_matrix_set_color_all(20, 0, 0);
break;
@@ -241,29 +258,26 @@ static bool rgb_matrix_none(effect_params_t *params) {
return false;
}
- RGB_MATRIX_USE_LIMITS(led_min, led_max);
- for (uint8_t i = led_min; i < led_max; i++) {
- rgb_matrix_set_color(i, 0, 0, 0);
- }
- return led_max < DRIVER_LED_TOTAL;
+ rgb_matrix_set_color_all(0, 0, 0);
+ return 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, 0xFF};
-static rgb_task_states rgb_task_state = SYNCING;
-
static void rgb_task_timers(void) {
+#if defined(RGB_MATRIX_KEYREACTIVE_ENABLED) || RGB_DISABLE_TIMEOUT > 0
+ uint32_t deltaTime = timer_elapsed32(rgb_timer_buffer);
+#endif // defined(RGB_MATRIX_KEYREACTIVE_ENABLED) || RGB_DISABLE_TIMEOUT > 0
+ rgb_timer_buffer = timer_read32();
+
// Update double buffer timers
- uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
- rgb_counters_buffer = timer_read32();
- if (g_rgb_counters.any_key_hit < UINT32_MAX) {
- if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
- g_rgb_counters.any_key_hit = UINT32_MAX;
+#if RGB_DISABLE_TIMEOUT > 0
+ if (rgb_anykey_timer < UINT32_MAX) {
+ if (UINT32_MAX - deltaTime < rgb_anykey_timer) {
+ rgb_anykey_timer = UINT32_MAX;
} else {
- g_rgb_counters.any_key_hit += deltaTime;
+ rgb_anykey_timer += deltaTime;
}
}
+#endif // RGB_DISABLE_TIMEOUT > 0
// Update double buffer last hit timers
#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
@@ -280,7 +294,7 @@ static void rgb_task_timers(void) {
static void rgb_task_sync(void) {
// next task
- if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
+ if (timer_elapsed32(g_rgb_timer) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
}
static void rgb_task_start(void) {
@@ -288,7 +302,7 @@ static void rgb_task_start(void) {
rgb_effect_params.iter = 0;
// update double buffers
- g_rgb_counters.tick = rgb_counters_buffer;
+ g_rgb_timer = rgb_timer_buffer;
#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
g_last_hit_tracker = last_hit_buffer;
#endif // RGB_MATRIX_KEYREACTIVE_ENABLED
@@ -370,8 +384,16 @@ void rgb_matrix_task(void) {
// Ideally we would also stop sending zeros to the LED driver PWM buffers
// while suspended and just do a software shutdown. This is a cheap hack for now.
- bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
- uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
+ bool suspend_backlight =
+#if RGB_DISABLE_WHEN_USB_SUSPENDED == true
+ g_suspend_state ||
+#endif // RGB_DISABLE_WHEN_USB_SUSPENDED == true
+#if RGB_DISABLE_TIMEOUT > 0
+ (rgb_anykey_timer > (uint32_t)RGB_DISABLE_TIMEOUT) ||
+#endif // RGB_DISABLE_TIMEOUT > 0
+ false;
+
+ uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
switch (rgb_task_state) {
case STARTING:
@@ -405,8 +427,6 @@ __attribute__((weak)) void rgb_matrix_indicators_user(void) {}
void rgb_matrix_init(void) {
rgb_matrix_driver.init();
- // TODO: put the 1 second startup delay here?
-
#ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
g_last_hit_tracker.count = 0;
for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {