diff --git a/keyboards/planck/light/keyboard.json b/keyboards/planck/light/keyboard.json index f445cca931d..907dd0c6d5e 100644 --- a/keyboards/planck/light/keyboard.json +++ b/keyboards/planck/light/keyboard.json @@ -98,7 +98,7 @@ {"matrix": [3, 3], "x": 61, "y": 63, "flags": 1}, {"matrix": [3, 4], "x": 81, "y": 63, "flags": 1}, {"matrix": [3, 5], "x": 101, "y": 63, "flags": 4}, - {"x": 111, "y": 63, "flags": 4}, + {"matrix": [3, 5], "x": 111, "y": 63, "flags": 4}, {"matrix": [3, 6], "x": 122, "y": 63, "flags": 4}, {"matrix": [3, 7], "x": 142, "y": 63, "flags": 1}, {"matrix": [3, 8], "x": 162, "y": 63, "flags": 1}, diff --git a/keyboards/planck/light/light.c b/keyboards/planck/light/light.c index 97ea06c467c..b7dabcd6ced 100644 --- a/keyboards/planck/light/light.c +++ b/keyboards/planck/light/light.c @@ -86,16 +86,6 @@ void matrix_init_kb(void) { matrix_init_user(); } -uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { - // Spacebar has 2 leds 41 & 42, so add 42 to the array here, and 41 will be added - // by the default lookup code that runs after this - if (row == 3 && column == 5) { - led_i[0] = 42; - return 1; - } - return 0; -} - #ifdef SWAP_HANDS_ENABLE __attribute__ ((weak)) const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = { diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py index 1978de4a22c..f1df375dfb4 100755 --- a/lib/python/qmk/cli/generate/keyboard_c.py +++ b/lib/python/qmk/cli/generate/keyboard_c.py @@ -17,10 +17,22 @@ def _gen_led_configs(info_data): lines = [] if 'layout' in info_data.get('rgb_matrix', {}): + lines.append('#ifdef RGB_MATRIX_ENABLE') + lines.append('#include "rgb_matrix.h"') + lines.append('') lines.extend(_gen_led_config(info_data, 'rgb_matrix')) + lines.extend(_gen_led_duplicate_config(info_data, 'rgb_matrix')) + lines.append('#endif') + lines.append('') if 'layout' in info_data.get('led_matrix', {}): + lines.append('#ifdef LED_MATRIX_ENABLE') + lines.append('#include "led_matrix.h"') + lines.append('') lines.extend(_gen_led_config(info_data, 'led_matrix')) + lines.extend(_gen_led_duplicate_config(info_data, 'led_matrix')) + lines.append('#endif') + lines.append('') return lines @@ -45,13 +57,6 @@ def _gen_led_config(info_data, config_type): pos.append(f'{{{led_data.get("x", 0)}, {led_data.get("y", 0)}}}') flags.append(str(led_data.get('flags', 0))) - if config_type == 'rgb_matrix': - lines.append('#ifdef RGB_MATRIX_ENABLE') - lines.append('#include "rgb_matrix.h"') - elif config_type == 'led_matrix': - lines.append('#ifdef LED_MATRIX_ENABLE') - lines.append('#include "led_matrix.h"') - lines.append('__attribute__ ((weak)) led_config_t g_led_config = {') lines.append(' {') for line in matrix: @@ -60,7 +65,51 @@ def _gen_led_config(info_data, config_type): lines.append(f' {{ {", ".join(pos)} }},') lines.append(f' {{ {", ".join(flags)} }},') lines.append('};') - lines.append('#endif') + lines.append('') + + return lines + + +def _gen_led_duplicate_config(info_data, config_type): + """Convert duplicate led mappings to map_row_column_to_led_kb implementations""" + + # precompute our map of matrix[r,c] -> [ids] + duplicates = False + led_map = {} + led_layout = info_data[config_type]['layout'] + for led_index, led_data in enumerate(led_layout): + if 'matrix' in led_data: + matrix = tuple(led_data['matrix']) + led_map[matrix] = [*led_map.get(matrix, []), led_index] + duplicates = True + + if not duplicates: + return [] + + lines = [] + lines.append(f'uint8_t {config_type}_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {{') + + for matrix, indexes in led_map.items(): + if len(indexes) > 1: + # Match last one wins behavior of _gen_led_config w.r.t. g_led_config matrix generation + last_index = indexes[-1] + other_indexes = indexes[:-1] + + lines.append(f'#if LED_HITS_TO_REMEMBER < {len(indexes)}') + lines.append(f'# pragma message("LED_HITS_TO_REMEMBER is not large enough to handle matrix{list(matrix)} indexes{indexes}")') + lines.append('#else') + lines.append(f' if (row == {matrix[0]} && column == {matrix[1]}) {{') + lines.append(f' // {last_index} will be added by the default lookup code that runs after this') + + for index, led_index in enumerate(other_indexes): + lines.append(f' led_i[{index}] = {led_index};') + + lines.append(f' return {len(other_indexes)};') + lines.append(' }') + lines.append('#endif') + + lines.append(' return 0;') + lines.append('}') lines.append('') return lines