diff options
| author | Nick Brassel <[email protected]> | 2022-05-29 07:57:11 +1000 | 
|---|---|---|
| committer | Nick Brassel <[email protected]> | 2022-05-29 07:57:11 +1000 | 
| commit | b835171008eaeaa992a1b8e390af8bce6f5f0b8f (patch) | |
| tree | de22c239cc47556f8be7538f95f48ad75b86d110 /lib/python/qmk/cli/generate/keyboard_c.py | |
| parent | f5d091a9d58c8349437e9d52de87294258cbd256 (diff) | |
| parent | 0c8f78020d01ee5c45481d7d93b9b0d9f7b95103 (diff) | |
| download | qmk_firmware-b835171008eaeaa992a1b8e390af8bce6f5f0b8f.tar.gz qmk_firmware-b835171008eaeaa992a1b8e390af8bce6f5f0b8f.zip | |
Merge branch 'develop' -- breaking changes 2022-05-28.0.17.0
Diffstat (limited to 'lib/python/qmk/cli/generate/keyboard_c.py')
| -rwxr-xr-x | lib/python/qmk/cli/generate/keyboard_c.py | 75 | 
1 files changed, 75 insertions, 0 deletions
| diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py new file mode 100755 index 0000000000..a9b742f323 --- /dev/null +++ b/lib/python/qmk/cli/generate/keyboard_c.py @@ -0,0 +1,75 @@ +"""Used by the make system to generate keyboard.c from info.json. +""" +from milc import cli + +from qmk.info import info_json +from qmk.commands import dump_lines +from qmk.keyboard import keyboard_completer, keyboard_folder +from qmk.path import normpath +from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE + + +def _gen_led_config(info_data): +    """Convert info.json content to g_led_config +    """ +    cols = info_data['matrix_size']['cols'] +    rows = info_data['matrix_size']['rows'] + +    config_type = None +    if 'layout' in info_data.get('rgb_matrix', {}): +        config_type = 'rgb_matrix' +    elif 'layout' in info_data.get('led_matrix', {}): +        config_type = 'led_matrix' + +    lines = [] +    if not config_type: +        return lines + +    matrix = [['NO_LED'] * cols for i in range(rows)] +    pos = [] +    flags = [] + +    led_config = info_data[config_type]['layout'] +    for index, item in enumerate(led_config, start=0): +        if 'matrix' in item: +            (x, y) = item['matrix'] +            matrix[x][y] = str(index) +        pos.append(f'{{ {item.get("x", 0)},{item.get("y", 0)} }}') +        flags.append(str(item.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: +        lines.append(f'    {{ {",".join(line)} }},') +    lines.append('  },') +    lines.append(f'  {{ {",".join(pos)} }},') +    lines.append(f'  {{ {",".join(flags)} }},') +    lines.append('};') +    lines.append('#endif') + +    return lines + + [email protected]('-o', '--output', arg_only=True, type=normpath, help='File to write to') [email protected]('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") [email protected]('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.c for.') [email protected]('Used by the make system to generate keyboard.c from info.json', hidden=True) +def generate_keyboard_c(cli): +    """Generates the keyboard.h file. +    """ +    kb_info_json = info_json(cli.args.keyboard) + +    # Build the layouts.h file. +    keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', ''] + +    keyboard_h_lines.extend(_gen_led_config(kb_info_json)) + +    # Show the results +    dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet) | 
