diff options
Diffstat (limited to 'lib/python/qmk/cli')
| -rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/keyboard_c.py | 38 | ||||
| -rw-r--r-- | lib/python/qmk/cli/generate/keymap_h.py | 51 | 
3 files changed, 89 insertions, 1 deletions
| diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 6d05a5fc21..b656909f85 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -58,6 +58,7 @@ subcommands = [      'qmk.cli.generate.keyboard_h',      'qmk.cli.generate.keycodes',      'qmk.cli.generate.keycodes_tests', +    'qmk.cli.generate.keymap_h',      'qmk.cli.generate.make_dependencies',      'qmk.cli.generate.rgb_breathe_table',      'qmk.cli.generate.rules_mk', diff --git a/lib/python/qmk/cli/generate/keyboard_c.py b/lib/python/qmk/cli/generate/keyboard_c.py index 5a6c967486..228b320942 100755 --- a/lib/python/qmk/cli/generate/keyboard_c.py +++ b/lib/python/qmk/cli/generate/keyboard_c.py @@ -6,7 +6,7 @@ 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 +from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, JOYSTICK_AXES  def _gen_led_configs(info_data): @@ -91,6 +91,41 @@ def _gen_matrix_mask(info_data):      return lines +def _gen_joystick_axes(info_data): +    """Convert info.json content to joystick_axes +    """ +    if 'axes' not in info_data.get('joystick', {}): +        return [] + +    axes = info_data['joystick']['axes'] +    axes_keys = list(axes.keys()) + +    lines = [] +    lines.append('#ifdef JOYSTICK_ENABLE') +    lines.append('joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = {') + +    # loop over all available axes - injecting virtual axis for those not specified +    for index, cur in enumerate(JOYSTICK_AXES): +        # bail out if we have generated all requested axis +        if len(axes_keys) == 0: +            break + +        axis = 'virtual' +        if cur in axes: +            axis = axes[cur] +            axes_keys.remove(cur) + +        if axis == 'virtual': +            lines.append(f"    [{index}] = JOYSTICK_AXIS_VIRTUAL,") +        else: +            lines.append(f"    [{index}] = JOYSTICK_AXIS_IN({axis['input_pin']}, {axis['low']}, {axis['rest']}, {axis['high']}),") + +    lines.append('};') +    lines.append('#endif') + +    return lines + +  @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')  @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")  @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.c for.') @@ -105,6 +140,7 @@ def generate_keyboard_c(cli):      keyboard_h_lines.extend(_gen_led_configs(kb_info_json))      keyboard_h_lines.extend(_gen_matrix_mask(kb_info_json)) +    keyboard_h_lines.extend(_gen_joystick_axes(kb_info_json))      # Show the results      dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet) diff --git a/lib/python/qmk/cli/generate/keymap_h.py b/lib/python/qmk/cli/generate/keymap_h.py new file mode 100644 index 0000000000..a3aaa405c0 --- /dev/null +++ b/lib/python/qmk/cli/generate/keymap_h.py @@ -0,0 +1,51 @@ +from argcomplete.completers import FilesCompleter + +from milc import cli + +import qmk.path +from qmk.commands import dump_lines +from qmk.commands import parse_configurator_json +from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE + + +def _generate_keycodes_function(keymap_json): +    """Generates keymap level keycodes. +    """ +    lines = [] +    lines.append('enum keymap_keycodes {') + +    for index, item in enumerate(keymap_json.get('keycodes', [])): +        key = item["key"] +        if index == 0: +            lines.append(f'  {key} = QK_USER_0,') +        else: +            lines.append(f'  {key},') + +    lines.append('};') + +    for item in keymap_json.get('keycodes', []): +        key = item["key"] +        for alias in item.get("aliases", []): +            lines.append(f'#define {alias} {key}') + +    return lines + + [email protected]('-o', '--output', arg_only=True, type=qmk.path.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]('filename', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') [email protected]('Creates a keymap.h from a QMK Configurator export.') +def generate_keymap_h(cli): +    """Creates a keymap.h from a QMK Configurator export +    """ +    if cli.args.output and cli.args.output.name == '-': +        cli.args.output = None + +    keymap_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '// clang-format off'] + +    keymap_json = parse_configurator_json(cli.args.filename) + +    if 'keycodes' in keymap_json and keymap_json['keycodes'] is not None: +        keymap_h_lines += _generate_keycodes_function(keymap_json) + +    dump_lines(cli.args.output, keymap_h_lines, cli.args.quiet) | 
