Skip to content

Commit

Permalink
RGB/LED matrix use limits size optimisation (qmk#22325)
Browse files Browse the repository at this point in the history
  • Loading branch information
daskygit authored and zgagnon committed Dec 15, 2023
1 parent eb8046f commit 94260bc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 66 deletions.
39 changes: 31 additions & 8 deletions quantum/led_matrix/led_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();

Expand Down
44 changes: 15 additions & 29 deletions quantum/led_matrix/led_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions quantum/rgb_matrix/rgb_matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 13 additions & 29 deletions quantum/rgb_matrix/rgb_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 94260bc

Please sign in to comment.