From f2936581473a86c3b72bd744808d57c50ee9bb32 Mon Sep 17 00:00:00 2001 From: Dasky <32983009+daskygit@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:06:51 +0100 Subject: [PATCH] RGB/LED matrix use limits size optimisation (#22325) --- quantum/led_matrix/led_matrix.c | 39 +++++++++++++++++++++++------ quantum/led_matrix/led_matrix.h | 44 +++++++++++---------------------- quantum/rgb_matrix/rgb_matrix.c | 30 ++++++++++++++++++++++ quantum/rgb_matrix/rgb_matrix.h | 42 ++++++++++--------------------- 4 files changed, 89 insertions(+), 66 deletions(-) diff --git a/quantum/led_matrix/led_matrix.c b/quantum/led_matrix/led_matrix.c index bbc94bca7a..4d67a295df 100644 --- a/quantum/led_matrix/led_matrix.c +++ b/quantum/led_matrix/led_matrix.c @@ -375,14 +375,7 @@ void led_matrix_indicators_advanced(effect_params_t *params) { * and not sure which would be better. Otherwise, this should be called from * led_task_render, right before the iter++ line. */ -#if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT - uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1); - uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; - if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT; -#else - uint8_t min = 0; - uint8_t max = LED_MATRIX_LED_COUNT; -#endif + LED_MATRIX_USE_LIMITS_ITER(min, max, params->iter - 1); led_matrix_indicators_advanced_kb(min, max); } @@ -394,6 +387,36 @@ __attribute__((weak)) bool led_matrix_indicators_advanced_user(uint8_t led_min, return true; } +struct led_matrix_limits_t led_matrix_get_limits(uint8_t iter) { + struct led_matrix_limits_t limits = {0}; +#if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT +# if defined(LED_MATRIX_SPLIT) + limits.led_min_index = LED_MATRIX_LED_PROCESS_LIMIT * (iter); + limits.led_max_index = limits.led_min_index + LED_MATRIX_LED_PROCESS_LIMIT; + if (limits.led_max_index > LED_MATRIX_LED_COUNT) limits.led_max_index = LED_MATRIX_LED_COUNT; + uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; + if (is_keyboard_left() && (limits.led_max_index > k_led_matrix_split[0])) limits.led_max_index = k_led_matrix_split[0]; + if (!(is_keyboard_left()) && (limits.led_min_index < k_led_matrix_split[0])) limits.led_min_index = k_led_matrix_split[0]; +# else + limits.led_min_index = LED_MATRIX_LED_PROCESS_LIMIT * (iter); + limits.led_max_index = limits.led_min_index + LED_MATRIX_LED_PROCESS_LIMIT; + if (limits.led_max_index > LED_MATRIX_LED_COUNT) limits.led_max_index = LED_MATRIX_LED_COUNT; +# endif +#else +# if defined(LED_MATRIX_SPLIT) + limits.led_min_index = 0; + limits.led_max_index = LED_MATRIX_LED_COUNT; + const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; + if (is_keyboard_left() && (limits.led_max_index > k_led_matrix_split[0])) limits.led_max_index = k_led_matrix_split[0]; + if (!(is_keyboard_left()) && (limits.led_min_index < k_led_matrix_split[0])) limits.led_min_index = k_led_matrix_split[0]; +# else + limits.led_min_index = 0; + limits.led_max_index = LED_MATRIX_LED_COUNT; +# endif +#endif + return limits; +} + void led_matrix_init(void) { led_matrix_driver.init(); diff --git a/quantum/led_matrix/led_matrix.h b/quantum/led_matrix/led_matrix.h index ab5ca32308..c903a230f4 100644 --- a/quantum/led_matrix/led_matrix.h +++ b/quantum/led_matrix/led_matrix.h @@ -89,35 +89,21 @@ # define LED_MATRIX_LED_PROCESS_LIMIT ((LED_MATRIX_LED_COUNT + 4) / 5) #endif -#if defined(LED_MATRIX_LED_PROCESS_LIMIT) && LED_MATRIX_LED_PROCESS_LIMIT > 0 && LED_MATRIX_LED_PROCESS_LIMIT < LED_MATRIX_LED_COUNT -# if defined(LED_MATRIX_SPLIT) -# define LED_MATRIX_USE_LIMITS(min, max) \ - uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * params->iter; \ - uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; \ - if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT; \ - uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; \ - if (is_keyboard_left() && (max > k_led_matrix_split[0])) max = k_led_matrix_split[0]; \ - if (!(is_keyboard_left()) && (min < k_led_matrix_split[0])) min = k_led_matrix_split[0]; -# else -# define LED_MATRIX_USE_LIMITS(min, max) \ - uint8_t min = LED_MATRIX_LED_PROCESS_LIMIT * params->iter; \ - uint8_t max = min + LED_MATRIX_LED_PROCESS_LIMIT; \ - if (max > LED_MATRIX_LED_COUNT) max = LED_MATRIX_LED_COUNT; -# endif -#else -# if defined(LED_MATRIX_SPLIT) -# define LED_MATRIX_USE_LIMITS(min, max) \ - uint8_t min = 0; \ - uint8_t max = LED_MATRIX_LED_COUNT; \ - const uint8_t k_led_matrix_split[2] = LED_MATRIX_SPLIT; \ - if (is_keyboard_left() && (max > k_led_matrix_split[0])) max = k_led_matrix_split[0]; \ - if (!(is_keyboard_left()) && (min < k_led_matrix_split[0])) min = k_led_matrix_split[0]; -# else -# define LED_MATRIX_USE_LIMITS(min, max) \ - uint8_t min = 0; \ - uint8_t max = LED_MATRIX_LED_COUNT; -# endif -#endif +struct led_matrix_limits_t { + uint8_t led_min_index; + uint8_t led_max_index; +}; + +struct led_matrix_limits_t led_matrix_get_limits(uint8_t iter); + +#define LED_MATRIX_USE_LIMITS_ITER(min, max, iter) \ + struct led_matrix_limits_t limits = led_matrix_get_limits(iter); \ + uint8_t min = limits.led_min_index; \ + uint8_t max = limits.led_max_index; \ + (void)min; \ + (void)max; + +#define LED_MATRIX_USE_LIMITS(min, max) LED_MATRIX_USE_LIMITS_ITER(min, max, params->iter) #define LED_MATRIX_TEST_LED_FLAGS() \ if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c index e56802f164..d93d189827 100644 --- a/quantum/rgb_matrix/rgb_matrix.c +++ b/quantum/rgb_matrix/rgb_matrix.c @@ -410,6 +410,36 @@ __attribute__((weak)) bool rgb_matrix_indicators_user(void) { return true; } +struct rgb_matrix_limits_t rgb_matrix_get_limits(uint8_t iter) { + struct rgb_matrix_limits_t limits = {0}; +#if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT +# if defined(RGB_MATRIX_SPLIT) + limits.led_min_index = RGB_MATRIX_LED_PROCESS_LIMIT * (iter); + limits.led_max_index = limits.led_min_index + RGB_MATRIX_LED_PROCESS_LIMIT; + if (limits.led_max_index > RGB_MATRIX_LED_COUNT) limits.led_max_index = RGB_MATRIX_LED_COUNT; + uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; + if (is_keyboard_left() && (limits.led_max_index > k_rgb_matrix_split[0])) limits.led_max_index = k_rgb_matrix_split[0]; + if (!(is_keyboard_left()) && (limits.led_min_index < k_rgb_matrix_split[0])) limits.led_min_index = k_rgb_matrix_split[0]; +# else + limits.led_min_index = RGB_MATRIX_LED_PROCESS_LIMIT * (iter); + limits.led_max_index = limits.led_min_index + RGB_MATRIX_LED_PROCESS_LIMIT; + if (limits.led_max_index > RGB_MATRIX_LED_COUNT) limits.led_max_index = RGB_MATRIX_LED_COUNT; +# endif +#else +# if defined(RGB_MATRIX_SPLIT) + limits.led_min_index = 0; + limits.led_max_index = RGB_MATRIX_LED_COUNT; + const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; + if (is_keyboard_left() && (limits.led_max_index > k_rgb_matrix_split[0])) limits.led_max_index = k_rgb_matrix_split[0]; + if (!(is_keyboard_left()) && (limits.led_min_index < k_rgb_matrix_split[0])) limits.led_min_index = k_rgb_matrix_split[0]; +# else + limits.led_min_index = 0; + limits.led_max_index = RGB_MATRIX_LED_COUNT; +# endif +#endif + return limits; +} + void rgb_matrix_indicators_advanced(effect_params_t *params) { /* special handling is needed for "params->iter", since it's already been incremented. * Could move the invocations to rgb_task_render, but then it's missing a few checks diff --git a/quantum/rgb_matrix/rgb_matrix.h b/quantum/rgb_matrix/rgb_matrix.h index bba07af79d..9a3ffb8ea3 100644 --- a/quantum/rgb_matrix/rgb_matrix.h +++ b/quantum/rgb_matrix/rgb_matrix.h @@ -107,35 +107,19 @@ # define RGB_MATRIX_LED_PROCESS_LIMIT ((RGB_MATRIX_LED_COUNT + 4) / 5) #endif -#if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < RGB_MATRIX_LED_COUNT -# if defined(RGB_MATRIX_SPLIT) -# define RGB_MATRIX_USE_LIMITS_ITER(min, max, iter) \ - uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * (iter); \ - uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT; \ - if (max > RGB_MATRIX_LED_COUNT) max = RGB_MATRIX_LED_COUNT; \ - uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; \ - if (is_keyboard_left() && (max > k_rgb_matrix_split[0])) max = k_rgb_matrix_split[0]; \ - if (!(is_keyboard_left()) && (min < k_rgb_matrix_split[0])) min = k_rgb_matrix_split[0]; -# else -# define RGB_MATRIX_USE_LIMITS_ITER(min, max, iter) \ - uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * (iter); \ - uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT; \ - if (max > RGB_MATRIX_LED_COUNT) max = RGB_MATRIX_LED_COUNT; -# endif -#else -# if defined(RGB_MATRIX_SPLIT) -# define RGB_MATRIX_USE_LIMITS_ITER(min, max, iter) \ - uint8_t min = 0; \ - uint8_t max = RGB_MATRIX_LED_COUNT; \ - const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; \ - if (is_keyboard_left() && (max > k_rgb_matrix_split[0])) max = k_rgb_matrix_split[0]; \ - if (!(is_keyboard_left()) && (min < k_rgb_matrix_split[0])) min = k_rgb_matrix_split[0]; -# else -# define RGB_MATRIX_USE_LIMITS_ITER(min, max, iter) \ - uint8_t min = 0; \ - uint8_t max = RGB_MATRIX_LED_COUNT; -# endif -#endif +struct rgb_matrix_limits_t { + uint8_t led_min_index; + uint8_t led_max_index; +}; + +struct rgb_matrix_limits_t rgb_matrix_get_limits(uint8_t iter); + +#define RGB_MATRIX_USE_LIMITS_ITER(min, max, iter) \ + struct rgb_matrix_limits_t limits = rgb_matrix_get_limits(iter); \ + uint8_t min = limits.led_min_index; \ + uint8_t max = limits.led_max_index; \ + (void)min; \ + (void)max; #define RGB_MATRIX_USE_LIMITS(min, max) RGB_MATRIX_USE_LIMITS_ITER(min, max, params->iter)