From ef946cbe9a82298d2911d6def995896de9c582d0 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 01:04:44 -0700 Subject: [PATCH 01/19] initial working on python cmd --- json-data.json | 215 ++++++++++++++++++++++++++++++++++ lib/python/qmk/cli/compile.py | 18 ++- lib/python/qmk/cli/flash.py | 7 ++ lib/python/qmk/commands.py | 23 ++++ lib/python/qmk/keymap.py | 109 +++++++++++++++++ 5 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 json-data.json diff --git a/json-data.json b/json-data.json new file mode 100644 index 000000000000..a7d1255adaa2 --- /dev/null +++ b/json-data.json @@ -0,0 +1,215 @@ +{ + "keyboard": "1upkeyboards/1up60hse", + "keymap": "newone", + "layout": "LAYOUT_60_ansi", + "layers": [ + [ + "KC_ESC", + "KC_1", + "KC_2", + "KC_3", + "KC_4", + "KC_5", + "KC_6", + "KC_7", + "KC_8", + "KC_9", + "KC_0", + "KC_MINS", + "KC_EQL", + "KC_BSPC", + "KC_TAB", + "KC_Q", + "KC_W", + "KC_E", + "KC_R", + "KC_T", + "KC_Y", + "KC_U", + "KC_I", + "KC_O", + "KC_P", + "KC_LBRC", + "KC_RBRC", + "KC_BSLS", + "KC_CAPS", + "KC_A", + "KC_S", + "KC_D", + "KC_F", + "KC_G", + "KC_H", + "KC_J", + "KC_K", + "KC_L", + "KC_SCLN", + "KC_QUOT", + "KC_ENT", + "KC_LSFT", + "KC_Z", + "KC_X", + "KC_C", + "KC_V", + "KC_B", + "KC_N", + "KC_M", + "KC_COMM", + "KC_DOT", + "KC_SLSH", + "KC_RSFT", + "KC_LCTL", + "KC_LGUI", + "KC_LALT", + "KC_SPC", + "KC_RALT", + "KC_RGUI", + "MO(1)", + "KC_RCTL" + ], + [ + "KC_GRV", + "KC_F1", + "KC_F2", + "KC_F3", + "KC_F4", + "KC_F5", + "KC_F6", + "KC_F7", + "KC_F8", + "KC_F9", + "KC_F10", + "KC_F11", + "KC_F12", + "KC_DEL", + "KC_TRNS", + "KC_TRNS", + "KC_UP", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_PSCR", + "KC_SLCK", + "KC_PAUS", + "KC_TRNS", + "KC_TRNS", + "KC_LEFT", + "KC_DOWN", + "KC_RGHT", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_INS", + "KC_HOME", + "KC_PGUP", + "KC_TRNS", + "KC_TRNS", + "KC_VOLU", + "KC_VOLD", + "KC_MUTE", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_END", + "KC_PGDN", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "MO(2)", + "KC_TRNS", + "KC_TRNS" + ], + [ + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "RESET", + "BL_TOGG", + "BL_INC", + "BL_DEC", + "BL_STEP", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "RGB_TOG", + "RGB_MOD", + "RGB_HUI", + "RGB_SAI", + "RGB_VAI", + "RGB_SPI", + "RGB_M_P", + "RGB_M_B", + "RGB_M_R", + "RGB_M_SW", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "RGB_RMOD", + "RGB_HUD", + "RGB_SAD", + "RGB_VAD", + "RGB_SPD", + "RGB_M_SN", + "RGB_M_K", + "RGB_M_X", + "RGB_M_G", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS", + "KC_TRNS" + ] + ], + "author": "", + "notes": "", + "encoders": [ + { + "index": 0, + "clockwise": "KC_MS_WH_UP", + "counter": "KC_MS_WH_DOWN" + }, + { + "index": 1, + "clockwise": "KC_VOLU", + "counter": "KC_VOLD" + }, + { + "index": 2, + "clockwise": "KC_GRV", + "counter": "KC_DOWN" + } + ] +} \ No newline at end of file diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 8e2d0cdbf4fb..439427dd5f90 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -9,14 +9,16 @@ from qmk.commands import create_make_command from qmk.commands import parse_configurator_json from qmk.commands import compile_configurator_json +from qmk.commands import compile_configurator_json_encoders import qmk.keymap import qmk.path -@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') +@cli.argument('filename', nargs='?', type=FileType('r'), help='The configurator export to compile') @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') +@cli.argument('-x', '--experimental', action='store_true', help='True enables encoder support for configurator export files. Only enabled when a configurator export is supplied.') @cli.subcommand('Compile a QMK Firmware.') def compile(cli): """Compile a QMK Firmware. @@ -28,6 +30,18 @@ def compile(cli): If --keyboard and --keymap are provided this command will build a firmware based on that. """ + experimental_mode = False + + if cli.args.filename and cli.args.experimental: + + # Warn the user + cli.log.info("{fg_yellow}Compiling in experimental mode!") + cont = input('Are you sure? Y/N ') + + experimental_mode = False + if (cont.lower() == 'y' or cont.lower() == 'yes'): + experimental_mode = True + if cli.args.filename: # Parse the configurator json user_keymap = parse_configurator_json(cli.args.filename) @@ -38,6 +52,8 @@ def compile(cli): # Compile the keymap command = compile_configurator_json(user_keymap) + if experimental_mode: + command = compile_configurator_json_encoders(user_keymap) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index e897174a2025..4a154ac42725 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py @@ -33,6 +33,7 @@ def print_bootloader_help(): @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') +@cli.argument('-x', '--experimental', help='Compiles configurator json with encoder support. Only supported for use with the `filename` parameter') @cli.subcommand('QMK Flash.') def flash(cli): """Compile and or flash QMK Firmware or keyboard/layout @@ -63,10 +64,16 @@ def flash(cli): user_keymap = parse_configurator_json(cli.args.filename) keymap_path = qmk.path.keymap(user_keymap['keyboard']) + use_encoders = False + if cli.args.experimental: + use_encoders = True + cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) # Convert the JSON into a C file and write it to disk. command = compile_configurator_json(user_keymap, cli.args.bootloader) + if cli.args.experimental: + command = compile_configurator_json_encoders(users_keymap, cli.args.bootloader) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 6067d49ae7c8..b49c4a8151dc 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -46,9 +46,32 @@ def compile_configurator_json(user_keymap, bootloader=None): A command to run to compile and flash the C file. """ # Write the keymap C file + qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) # Return a command that can be run to make the keymap and flash if given if bootloader is None: return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) + +def compile_configurator_json_encoders(user_keymap, bootloader=None): + """Convert a configurator export JSON file into a C file + + Args: + configurator_filename + The configurator JSON export file + + bootloader + A bootloader to flash + + Returns: + A command to run to compile and flash the C file. + """ + # Write the keymap C file + + qmk.keymap.write_with_encoders(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], user_keymap['encoders']) + + # Return a command that can be run to make the keymap and flash if given + if bootloader is None: + return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) + return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index f4a2893f3855..7fb72b11133f 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -18,6 +18,31 @@ }; """ +DEFAULT_KEYMAP_ENCODERS_C = """#include QMK_KEYBOARD_H + +/* THIS FILE WAS GENERATED! + * + * This file was generated by qmk-compile-json. You may or may not want to + * edit it directly. + */ + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +__KEYMAP_GOES_HERE__ +}; + +void encoder_update_user(uint8_t index, bool clockwise) { +__ENCODERS_GO_HERE__ +}; +""" + +ENCODER_IF = """\t_ELIF_ (index == _INDEX_) { +\t\tif (clockwise) { +\t\t\ttap_code(_CLOCKWISE_); +\t\t} else { +\t\t\ttap_code(_COUNTER_); +\t\t} +\t} +""" def template(keyboard): """Returns the `keymap.c` template for a keyboard. @@ -38,6 +63,55 @@ def template(keyboard): return DEFAULT_KEYMAP_C +def generate_with_encoders(keyboard, layout, layers, encoders): + """Returns a keymap.c for the specified keyboard, layout, and layers. + + Args: + keyboard + The name of the keyboard + + layout + The LAYOUT macro this keymap uses. + + layers + An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. + + encoders + A json object describing the encoder positions and counter- and clockwise turn values. + """ + + # First, replace the keymap + layer_txt = [] + for layer_num, layer in enumerate(layers): + if layer_num != 0: + layer_txt[-1] = layer_txt[-1] + ',' + layer_keys = ', '.join(layer) + layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) + + keymap = '\n'.join(layer_txt) + keymap_c = DEFAULT_KEYMAP_ENCODERS_C # literally only two keyboards have templates so... + + keymap_c = keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) + + # Second, replace the encoders + encoders_txt = [] + for encoder_set in encoders: + elif_str = "else if" + if encoder_set['index'] == 0: + elif_str = "if" + + curr_encoder = ENCODER_IF.replace('_ELIF_', elif_str) + curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set['index'])) + curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) + curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) + encoders_txt.append(curr_encoder) + + encoder_function = '\n'.join(encoders_txt) + keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_function) + + return keymap_c + + def generate(keyboard, layout, layers): """Returns a keymap.c for the specified keyboard, layout, and layers. @@ -94,3 +168,38 @@ def write(keyboard, keymap, layout, layers): keymap_fd.write(keymap_c) return keymap_file + + +def write_with_encoders(keyboard, keymap, layout, layers, encoders): + """Generate the `keymap.c` and write it to disk. + + Returns the filename written to. + + Args: + keyboard + The name of the keyboard + + keymap + The name of the keymap + + layout + The LAYOUT macro this keymap uses. + + layers + An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. + + encoders + A json object describing the encoder positions and their counter- and clockwise turn values. + """ + keymap_c = generate_with_encoders(keyboard, layout, layers, encoders) + keymap_path = qmk.path.keymap(keyboard) + keymap_dir = os.path.join(keymap_path, keymap) + keymap_file = os.path.join(keymap_dir, 'keymap.c') + + if not os.path.exists(keymap_dir): + os.makedirs(keymap_dir) + + with open(keymap_file, 'w') as keymap_fd: + keymap_fd.write(keymap_c) + + return keymap_file \ No newline at end of file From 9e01eba3e273a489912fbf206a13bcd6abc82d89 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 15:57:15 -0700 Subject: [PATCH 02/19] merging functions using experimental flag --- lib/python/qmk/cli/compile.py | 19 ++---- lib/python/qmk/cli/flash.py | 9 +-- lib/python/qmk/commands.py | 31 ++------- lib/python/qmk/keymap.py | 119 +++++++++++----------------------- 4 files changed, 50 insertions(+), 128 deletions(-) diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 439427dd5f90..1fdf94d9fb59 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -30,19 +30,12 @@ def compile(cli): If --keyboard and --keymap are provided this command will build a firmware based on that. """ - experimental_mode = False - - if cli.args.filename and cli.args.experimental: - - # Warn the user - cli.log.info("{fg_yellow}Compiling in experimental mode!") - cont = input('Are you sure? Y/N ') + if cli.args.filename: - experimental_mode = False - if (cont.lower() == 'y' or cont.lower() == 'yes'): - experimental_mode = True + # Warn the user if they are using experimental mode + if cli.args.experimental: + cli.log.info("{fg_yellow}Compiling in experimental mode! I hope you know what you're doing...") - if cli.args.filename: # Parse the configurator json user_keymap = parse_configurator_json(cli.args.filename) @@ -51,9 +44,7 @@ def compile(cli): cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) # Compile the keymap - command = compile_configurator_json(user_keymap) - if experimental_mode: - command = compile_configurator_json_encoders(user_keymap) + command = compile_configurator_json(user_keymap, experimental=cli.args.experimental) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index 4a154ac42725..1b535155c9a0 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py @@ -33,7 +33,6 @@ def print_bootloader_help(): @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') -@cli.argument('-x', '--experimental', help='Compiles configurator json with encoder support. Only supported for use with the `filename` parameter') @cli.subcommand('QMK Flash.') def flash(cli): """Compile and or flash QMK Firmware or keyboard/layout @@ -64,16 +63,10 @@ def flash(cli): user_keymap = parse_configurator_json(cli.args.filename) keymap_path = qmk.path.keymap(user_keymap['keyboard']) - use_encoders = False - if cli.args.experimental: - use_encoders = True - cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path) # Convert the JSON into a C file and write it to disk. - command = compile_configurator_json(user_keymap, cli.args.bootloader) - if cli.args.experimental: - command = compile_configurator_json_encoders(users_keymap, cli.args.bootloader) + command = compile_configurator_json(user_keymap, bootloader=cli.args.bootloader) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index b49c4a8151dc..48e87659262d 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -32,7 +32,7 @@ def parse_configurator_json(configurator_file): return user_keymap -def compile_configurator_json(user_keymap, bootloader=None): +def compile_configurator_json(user_keymap, bootloader=None, experimental=False): """Convert a configurator export JSON file into a C file Args: @@ -42,34 +42,17 @@ def compile_configurator_json(user_keymap, bootloader=None): bootloader A bootloader to flash - Returns: - A command to run to compile and flash the C file. - """ - # Write the keymap C file - - qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers']) - - # Return a command that can be run to make the keymap and flash if given - if bootloader is None: - return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) - return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) - -def compile_configurator_json_encoders(user_keymap, bootloader=None): - """Convert a configurator export JSON file into a C file - - Args: - configurator_filename - The configurator JSON export file - - bootloader - A bootloader to flash + experimental + True if the user wants to attempt compilation with encoder support. Returns: A command to run to compile and flash the C file. """ - # Write the keymap C file + # Find the encoders, if any + encoders = None if 'encoders' not in user_keymap.keys() else user_keymap['encoders'] - qmk.keymap.write_with_encoders(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], user_keymap['encoders']) + # Write the keymap C file + qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], encoders, experimental) # Return a command that can be run to make the keymap and flash if given if bootloader is None: diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 7fb72b11133f..d475721ca811 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -20,7 +20,7 @@ DEFAULT_KEYMAP_ENCODERS_C = """#include QMK_KEYBOARD_H -/* THIS FILE WAS GENERATED! +/* THIS FILE WAS GENERATED AND IS EXPERIMENTAL! * * This file was generated by qmk-compile-json. You may or may not want to * edit it directly. @@ -44,7 +44,7 @@ \t} """ -def template(keyboard): +def template(keyboard, experimental=False): """Returns the `keymap.c` template for a keyboard. If a template exists in `keyboards//templates/keymap.c` that @@ -53,6 +53,9 @@ def template(keyboard): Args: keyboard The keyboard to return a template for. + + experimental + True if the user wants to attempt compilation with encoder support """ template_name = 'keyboards/%s/templates/keymap.c' % keyboard @@ -60,10 +63,12 @@ def template(keyboard): with open(template_name, 'r') as fd: return fd.read() + if experimental: + return DEFAULT_KEYMAP_ENCODERS_C return DEFAULT_KEYMAP_C -def generate_with_encoders(keyboard, layout, layers, encoders): +def generate(keyboard, layout, layers, encoders=None, experimental=False): """Returns a keymap.c for the specified keyboard, layout, and layers. Args: @@ -77,10 +82,12 @@ def generate_with_encoders(keyboard, layout, layers, encoders): An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. encoders - A json object describing the encoder positions and counter- and clockwise turn values. - """ + EXPERIMENTAL + An array of encoders on the keyboard. Each item includes the index and the counter- and clockwise key codes. - # First, replace the keymap + experimental + True if the user wants to attempt compilation with encoder support + """ layer_txt = [] for layer_num, layer in enumerate(layers): if layer_num != 0: @@ -89,88 +96,32 @@ def generate_with_encoders(keyboard, layout, layers, encoders): layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) keymap = '\n'.join(layer_txt) - keymap_c = DEFAULT_KEYMAP_ENCODERS_C # literally only two keyboards have templates so... + keymap_c = template(keyboard, experimental) keymap_c = keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) - # Second, replace the encoders - encoders_txt = [] - for encoder_set in encoders: - elif_str = "else if" - if encoder_set['index'] == 0: - elif_str = "if" + if experimental: + encoder_function = '' + if encoders is not None: + encoders_txt = [] + for encoder_set in encoders: + elif_str = "else if" + if encoder_set['index'] == 0: + elif_str = "if" - curr_encoder = ENCODER_IF.replace('_ELIF_', elif_str) - curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set['index'])) - curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) - curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) - encoders_txt.append(curr_encoder) + curr_encoder = ENCODER_IF.replace('_ELIF_', elif_str) + curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set['index'])) + curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) + curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) + encoders_txt.append(curr_encoder) + encoder_function = '\n'.join(encoders_txt) - encoder_function = '\n'.join(encoders_txt) - keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_function) + keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_function) return keymap_c -def generate(keyboard, layout, layers): - """Returns a keymap.c for the specified keyboard, layout, and layers. - - Args: - keyboard - The name of the keyboard - - layout - The LAYOUT macro this keymap uses. - - layers - An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. - """ - layer_txt = [] - for layer_num, layer in enumerate(layers): - if layer_num != 0: - layer_txt[-1] = layer_txt[-1] + ',' - layer_keys = ', '.join(layer) - layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) - - keymap = '\n'.join(layer_txt) - keymap_c = template(keyboard) - - return keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) - - -def write(keyboard, keymap, layout, layers): - """Generate the `keymap.c` and write it to disk. - - Returns the filename written to. - - Args: - keyboard - The name of the keyboard - - keymap - The name of the keymap - - layout - The LAYOUT macro this keymap uses. - - layers - An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. - """ - keymap_c = generate(keyboard, layout, layers) - keymap_path = qmk.path.keymap(keyboard) - keymap_dir = os.path.join(keymap_path, keymap) - keymap_file = os.path.join(keymap_dir, 'keymap.c') - - if not os.path.exists(keymap_dir): - os.makedirs(keymap_dir) - - with open(keymap_file, 'w') as keymap_fd: - keymap_fd.write(keymap_c) - - return keymap_file - - -def write_with_encoders(keyboard, keymap, layout, layers, encoders): +def write(keyboard, keymap, layout, layers, encoders=None, experimental=False): """Generate the `keymap.c` and write it to disk. Returns the filename written to. @@ -189,9 +140,13 @@ def write_with_encoders(keyboard, keymap, layout, layers, encoders): An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. encoders - A json object describing the encoder positions and their counter- and clockwise turn values. + EXPERIMENTAL + An array of encoders on the keyboard. Each item includes the index and the counter- and clockwise key codes. + + experimental + True if the user wants to attempt compilation with encoder support """ - keymap_c = generate_with_encoders(keyboard, layout, layers, encoders) + keymap_c = generate(keyboard, layout, layers, encoders, experimental) keymap_path = qmk.path.keymap(keyboard) keymap_dir = os.path.join(keymap_path, keymap) keymap_file = os.path.join(keymap_dir, 'keymap.c') @@ -202,4 +157,4 @@ def write_with_encoders(keyboard, keymap, layout, layers, encoders): with open(keymap_file, 'w') as keymap_fd: keymap_fd.write(keymap_c) - return keymap_file \ No newline at end of file + return keymap_file From 642041bcb2bae8ecc174e58ce26c63c5946e5f13 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 17:26:41 -0700 Subject: [PATCH 03/19] Adding UTs --- keyboards/handwired/onekey/pytest/keymap.json | 19 +++++++++++++++++++ lib/python/qmk/cli/compile.py | 3 +-- lib/python/qmk/keymap.py | 1 + lib/python/qmk/tests/test_cli_commands.py | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 keyboards/handwired/onekey/pytest/keymap.json diff --git a/keyboards/handwired/onekey/pytest/keymap.json b/keyboards/handwired/onekey/pytest/keymap.json new file mode 100644 index 000000000000..e2cdca374d40 --- /dev/null +++ b/keyboards/handwired/onekey/pytest/keymap.json @@ -0,0 +1,19 @@ +{ + "keyboard": "handwired/onekey/pytest", + "keymap": "pytest", + "layout": "LAYOUT", + "layers": [ + [ + "KC_ENTER" + ] + ], + "author": "", + "notes": "", + "encoders": [ + { + "index": 0, + "clockwise": "KC_UP", + "counter": "KC_DOWN" + } + ] +} \ No newline at end of file diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 1fdf94d9fb59..dad168b4547f 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -9,7 +9,6 @@ from qmk.commands import create_make_command from qmk.commands import parse_configurator_json from qmk.commands import compile_configurator_json -from qmk.commands import compile_configurator_json_encoders import qmk.keymap import qmk.path @@ -34,7 +33,7 @@ def compile(cli): # Warn the user if they are using experimental mode if cli.args.experimental: - cli.log.info("{fg_yellow}Compiling in experimental mode! I hope you know what you're doing...") + cli.log.info("{fg_yellow}Compiling in experimental mode! I hope you know what you're doing...") # Parse the configurator json user_keymap = parse_configurator_json(cli.args.filename) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index d475721ca811..90df0f5f1465 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -44,6 +44,7 @@ \t} """ + def template(keyboard, experimental=False): """Returns the `keymap.c` template for a keyboard. diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 3f75cef3e1d2..b4955eab4582 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -12,6 +12,8 @@ def test_cformat(): def test_compile(): assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 + assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest/keymap.json').returncode == 0 + assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest/keymap.json', '-x').returncode == 0 def test_flash(): From a37f578805ec17a2e55f0769847c08de9f587e86 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 17:34:42 -0700 Subject: [PATCH 04/19] Adding some cleanup --- lib/python/qmk/tests/test_cli_commands.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index b4955eab4582..69bd80463a4a 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -1,4 +1,5 @@ import subprocess +import os def check_subcommand(command, *args): @@ -15,6 +16,11 @@ def test_compile(): assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest/keymap.json').returncode == 0 assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest/keymap.json', '-x').returncode == 0 + # Cleanup + test_output = 'keyboards/handwired/onekey/keymaps/pytest/keymap.c' + if os.path.exists(test_output): + os.remove(test_output) + def test_flash(): assert check_subcommand('flash', '-b').returncode == 1 From 47841804b25cb9baf44e9a8166124dcae322ba5f Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 17:50:00 -0700 Subject: [PATCH 05/19] Deleting test files --- json-data.json | 215 ------------------------------------------------- 1 file changed, 215 deletions(-) delete mode 100644 json-data.json diff --git a/json-data.json b/json-data.json deleted file mode 100644 index a7d1255adaa2..000000000000 --- a/json-data.json +++ /dev/null @@ -1,215 +0,0 @@ -{ - "keyboard": "1upkeyboards/1up60hse", - "keymap": "newone", - "layout": "LAYOUT_60_ansi", - "layers": [ - [ - "KC_ESC", - "KC_1", - "KC_2", - "KC_3", - "KC_4", - "KC_5", - "KC_6", - "KC_7", - "KC_8", - "KC_9", - "KC_0", - "KC_MINS", - "KC_EQL", - "KC_BSPC", - "KC_TAB", - "KC_Q", - "KC_W", - "KC_E", - "KC_R", - "KC_T", - "KC_Y", - "KC_U", - "KC_I", - "KC_O", - "KC_P", - "KC_LBRC", - "KC_RBRC", - "KC_BSLS", - "KC_CAPS", - "KC_A", - "KC_S", - "KC_D", - "KC_F", - "KC_G", - "KC_H", - "KC_J", - "KC_K", - "KC_L", - "KC_SCLN", - "KC_QUOT", - "KC_ENT", - "KC_LSFT", - "KC_Z", - "KC_X", - "KC_C", - "KC_V", - "KC_B", - "KC_N", - "KC_M", - "KC_COMM", - "KC_DOT", - "KC_SLSH", - "KC_RSFT", - "KC_LCTL", - "KC_LGUI", - "KC_LALT", - "KC_SPC", - "KC_RALT", - "KC_RGUI", - "MO(1)", - "KC_RCTL" - ], - [ - "KC_GRV", - "KC_F1", - "KC_F2", - "KC_F3", - "KC_F4", - "KC_F5", - "KC_F6", - "KC_F7", - "KC_F8", - "KC_F9", - "KC_F10", - "KC_F11", - "KC_F12", - "KC_DEL", - "KC_TRNS", - "KC_TRNS", - "KC_UP", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_PSCR", - "KC_SLCK", - "KC_PAUS", - "KC_TRNS", - "KC_TRNS", - "KC_LEFT", - "KC_DOWN", - "KC_RGHT", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_INS", - "KC_HOME", - "KC_PGUP", - "KC_TRNS", - "KC_TRNS", - "KC_VOLU", - "KC_VOLD", - "KC_MUTE", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_END", - "KC_PGDN", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "MO(2)", - "KC_TRNS", - "KC_TRNS" - ], - [ - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "RESET", - "BL_TOGG", - "BL_INC", - "BL_DEC", - "BL_STEP", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "RGB_TOG", - "RGB_MOD", - "RGB_HUI", - "RGB_SAI", - "RGB_VAI", - "RGB_SPI", - "RGB_M_P", - "RGB_M_B", - "RGB_M_R", - "RGB_M_SW", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "RGB_RMOD", - "RGB_HUD", - "RGB_SAD", - "RGB_VAD", - "RGB_SPD", - "RGB_M_SN", - "RGB_M_K", - "RGB_M_X", - "RGB_M_G", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS", - "KC_TRNS" - ] - ], - "author": "", - "notes": "", - "encoders": [ - { - "index": 0, - "clockwise": "KC_MS_WH_UP", - "counter": "KC_MS_WH_DOWN" - }, - { - "index": 1, - "clockwise": "KC_VOLU", - "counter": "KC_VOLD" - }, - { - "index": 2, - "clockwise": "KC_GRV", - "counter": "KC_DOWN" - } - ] -} \ No newline at end of file From 635a790bc4c045c48a67cd19a968dedf6aa94399 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 17:56:15 -0700 Subject: [PATCH 06/19] Docs update. Loc needed --- docs/cli.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/cli.md b/docs/cli.md index 1c095272212c..06a6602f4f07 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -89,6 +89,11 @@ This command allows you to compile firmware from any directory. You can compile qmk compile ``` +**Experimental Usage for Configurator Exports**: +``` +qmk compile -x +``` + **Usage for Keymaps**: ``` @@ -172,7 +177,7 @@ qmk kle2json [-f] **Examples**: ``` -$ qmk kle2json kle.txt +$ qmk kle2json kle.txt ☒ File info.json already exists, use -f or --force to overwrite. ``` From 412364644661b53ced3f539ad8d0404ceebe8a14 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Sun, 15 Mar 2020 17:57:44 -0700 Subject: [PATCH 07/19] full switch --- docs/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cli.md b/docs/cli.md index 06a6602f4f07..796cfa67d4ac 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -91,7 +91,7 @@ qmk compile **Experimental Usage for Configurator Exports**: ``` -qmk compile -x +qmk compile --experimental ``` **Usage for Keymaps**: From f68caf75dff9be31986b2dbf3cf0e19bd6ae6c48 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Mon, 16 Mar 2020 22:15:15 -0700 Subject: [PATCH 08/19] Changing dirs so the test works correctly --- keyboards/handwired/onekey/{pytest => pytest2}/keymap.json | 0 lib/python/qmk/tests/test_cli_commands.py | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename keyboards/handwired/onekey/{pytest => pytest2}/keymap.json (100%) diff --git a/keyboards/handwired/onekey/pytest/keymap.json b/keyboards/handwired/onekey/pytest2/keymap.json similarity index 100% rename from keyboards/handwired/onekey/pytest/keymap.json rename to keyboards/handwired/onekey/pytest2/keymap.json diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index dd9b3fd9a442..3c80fcad0484 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -14,11 +14,11 @@ def test_cformat(): def test_compile(): assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 - assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest/keymap.json').returncode == 0 - assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest/keymap.json', '-x').returncode == 0 + assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest2/keymap.json').returncode == 0 + assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest2/keymap.json', '-x').returncode == 0 # Cleanup - test_output = 'keyboards/handwired/onekey/keymaps/pytest/keymap.c' + test_output = 'keyboards/handwired/onekey/keymaps/pytest2/keymap.c' if os.path.exists(test_output): os.remove(test_output) From 0d5d1d4752d919903d696f051f586ad04e1c8214 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Mon, 16 Mar 2020 22:25:28 -0700 Subject: [PATCH 09/19] Fixing the json --- keyboards/handwired/onekey/pytest2/keymap.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/keyboards/handwired/onekey/pytest2/keymap.json b/keyboards/handwired/onekey/pytest2/keymap.json index e2cdca374d40..b5e774d4d089 100644 --- a/keyboards/handwired/onekey/pytest2/keymap.json +++ b/keyboards/handwired/onekey/pytest2/keymap.json @@ -1,6 +1,6 @@ { - "keyboard": "handwired/onekey/pytest", - "keymap": "pytest", + "keyboard": "handwired/onekey", + "keymap": "pytest2", "layout": "LAYOUT", "layers": [ [ From 824f38213576af0f110a8cea7eff116d123795ee Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Wed, 18 Mar 2020 19:41:54 -0700 Subject: [PATCH 10/19] PR comments and some optimizations --- keyboards/handwired/onekey/pytest2/config.h | 23 +++++++ .../pytest2/{keymap.json => encoder.json} | 2 +- .../handwired/onekey/pytest2/no_encoder.json | 12 ++++ keyboards/handwired/onekey/pytest2/rules.mk | 2 + lib/python/qmk/cli/compile.py | 9 +-- lib/python/qmk/commands.py | 14 ++-- lib/python/qmk/keymap.py | 65 +++++-------------- lib/python/qmk/tests/test_cli_commands.py | 12 ++-- 8 files changed, 66 insertions(+), 73 deletions(-) create mode 100644 keyboards/handwired/onekey/pytest2/config.h rename keyboards/handwired/onekey/pytest2/{keymap.json => encoder.json} (91%) create mode 100644 keyboards/handwired/onekey/pytest2/no_encoder.json create mode 100644 keyboards/handwired/onekey/pytest2/rules.mk diff --git a/keyboards/handwired/onekey/pytest2/config.h b/keyboards/handwired/onekey/pytest2/config.h new file mode 100644 index 000000000000..f6bedcfe6481 --- /dev/null +++ b/keyboards/handwired/onekey/pytest2/config.h @@ -0,0 +1,23 @@ +/* Copyright 2019 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "config_common.h" + +#define MATRIX_COL_PINS { A3 } +#define MATRIX_ROW_PINS { A2 } +#define UNUSED_PINS diff --git a/keyboards/handwired/onekey/pytest2/keymap.json b/keyboards/handwired/onekey/pytest2/encoder.json similarity index 91% rename from keyboards/handwired/onekey/pytest2/keymap.json rename to keyboards/handwired/onekey/pytest2/encoder.json index b5e774d4d089..aa45f841d003 100644 --- a/keyboards/handwired/onekey/pytest2/keymap.json +++ b/keyboards/handwired/onekey/pytest2/encoder.json @@ -1,6 +1,6 @@ { "keyboard": "handwired/onekey", - "keymap": "pytest2", + "keymap": "encoder", "layout": "LAYOUT", "layers": [ [ diff --git a/keyboards/handwired/onekey/pytest2/no_encoder.json b/keyboards/handwired/onekey/pytest2/no_encoder.json new file mode 100644 index 000000000000..11519b5cadda --- /dev/null +++ b/keyboards/handwired/onekey/pytest2/no_encoder.json @@ -0,0 +1,12 @@ +{ + "keyboard": "handwired/onekey", + "keymap": "no_encoder", + "layout": "LAYOUT", + "layers": [ + [ + "KC_ENTER" + ] + ], + "author": "", + "notes": "" +} \ No newline at end of file diff --git a/keyboards/handwired/onekey/pytest2/rules.mk b/keyboards/handwired/onekey/pytest2/rules.mk new file mode 100644 index 000000000000..b17a3d0316b5 --- /dev/null +++ b/keyboards/handwired/onekey/pytest2/rules.mk @@ -0,0 +1,2 @@ +# MCU name +MCU = STM32F303 diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 9885855ecf7d..8d330174a566 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -12,10 +12,9 @@ from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json -@cli.argument('filename', nargs='?', type=FileType('r'), help='The configurator export to compile') +@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') -@cli.argument('-x', '--experimental', action='store_true', help='True enables encoder support for configurator export files. Only enabled when a configurator export is supplied.') @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") @cli.subcommand('Compile a QMK Firmware.') @automagic_keyboard @@ -31,10 +30,6 @@ def compile(cli): if cli.args.filename: - # Warn the user if they are using experimental mode - if cli.args.experimental: - cli.log.info("{fg_yellow}Compiling in experimental mode! I hope you know what you're doing...") - # If a configurator JSON was provided generate a keymap and compile it # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. @@ -43,7 +38,7 @@ def compile(cli): keymap_path = qmk.path.keymap(user_keymap['keyboard']) # Compile the keymap - command = compile_configurator_json(user_keymap, experimental=cli.args.experimental) + command = compile_configurator_json(user_keymap) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 5dfaa17a6854..780f89452b34 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -31,7 +31,7 @@ def create_make_command(keyboard, keymap, target=None): return ['make', ':'.join(make_args)] -def compile_configurator_json(user_keymap, bootloader=None, experimental=False): +def compile_configurator_json(user_keymap, bootloader=None): """Convert a configurator export JSON file into a C file Args: @@ -42,23 +42,17 @@ def compile_configurator_json(user_keymap, bootloader=None, experimental=False): bootloader A bootloader to flash - experimental - True if the user wants to attempt compilation with encoder support. - Returns: A command to run to compile and flash the C file. """ - # Find the encoders, if any - encoders = None if 'encoders' not in user_keymap.keys() else user_keymap['encoders'] - # Write the keymap C file - qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], encoders, experimental) + qmk.keymap.write(user_keymap.get('keyboard'), user_keymap.get('keymap'), user_keymap.get('layout'), user_keymap.get('layers'), user_keymap.get('encoders')) # Return a command that can be run to make the keymap and flash if given if bootloader is None: - return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) - return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) + return create_make_command(user_keymap.get('keyboard'), user_keymap.get('keymap')) + return create_make_command(user_keymap.get('keyboard'), user_keymap.get('keymap'), bootloader) def parse_configurator_json(configurator_file): diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 2da6ae9512d5..e23a3a4bb536 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -15,19 +15,6 @@ * edit it directly. */ -const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { -__KEYMAP_GOES_HERE__ -}; -""" - -DEFAULT_KEYMAP_ENCODERS_C = """#include QMK_KEYBOARD_H - -/* THIS FILE WAS GENERATED AND IS EXPERIMENTAL! - * - * This file was generated by qmk-compile-json. You may or may not want to - * edit it directly. - */ - const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { __KEYMAP_GOES_HERE__ }; @@ -47,7 +34,7 @@ """ -def template(keyboard, experimental=False): +def template(keyboard): """Returns the `keymap.c` template for a keyboard. If a template exists in `keyboards//templates/keymap.c` that @@ -56,21 +43,16 @@ def template(keyboard, experimental=False): Args: keyboard The keyboard to return a template for. - - experimental - True if the user wants to attempt compilation with encoder support """ template_file = Path('keyboards/%s/templates/keymap.c' % keyboard) if template_file.exists(): return template_file.read_text() - if experimental: - return DEFAULT_KEYMAP_ENCODERS_C return DEFAULT_KEYMAP_C -def generate(keyboard, layout, layers, encoders=None, experimental=False): +def generate(keyboard, layout, layers, encoders=None): """Returns a keymap.c for the specified keyboard, layout, and layers. Args: @@ -84,11 +66,7 @@ def generate(keyboard, layout, layers, encoders=None, experimental=False): An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. encoders - EXPERIMENTAL - An array of encoders on the keyboard. Each item includes the index and the counter- and clockwise key codes. - - experimental - True if the user wants to attempt compilation with encoder support + An array of encoders on the keyboard, if any. Each item includes the index and the counter- and clockwise key codes. """ layer_txt = [] for layer_num, layer in enumerate(layers): @@ -98,32 +76,27 @@ def generate(keyboard, layout, layers, encoders=None, experimental=False): layer_txt.append('\t[%s] = %s(%s)' % (layer_num, layout, layer_keys)) keymap = '\n'.join(layer_txt) - keymap_c = template(keyboard, experimental) + keymap_c = template(keyboard) keymap_c = keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) - if experimental: - encoder_function = '' - if encoders is not None: - encoders_txt = [] - for encoder_set in encoders: - elif_str = "else if" - if encoder_set['index'] == 0: - elif_str = "if" + encoder_func = '' + if encoders: + encoders_txt = [] + for encoder_set in encoders: + curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_set['index'] == 0 else 'else if') + curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set['index'])) + curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) + curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) + encoders_txt.append(curr_encoder) + encoder_func = '\n'.join(encoders_txt) - curr_encoder = ENCODER_IF.replace('_ELIF_', elif_str) - curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set['index'])) - curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) - curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) - encoders_txt.append(curr_encoder) - encoder_function = '\n'.join(encoders_txt) - - keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_function) + keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_func) return keymap_c -def write(keyboard, keymap, layout, layers, encoders=None, experimental=False): +def write(keyboard, keymap, layout, layers, encoders=None): """Generate the `keymap.c` and write it to disk. Returns the filename written to. @@ -142,13 +115,9 @@ def write(keyboard, keymap, layout, layers, encoders=None, experimental=False): An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode. encoders - EXPERIMENTAL An array of encoders on the keyboard. Each item includes the index and the counter- and clockwise key codes. - - experimental - True if the user wants to attempt compilation with encoder support """ - keymap_c = generate(keyboard, layout, layers, encoders, experimental) + keymap_c = generate(keyboard, layout, layers, encoders) keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c' keymap_file.parent.mkdir(parents=True, exist_ok=True) diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 3c80fcad0484..60735c78de7f 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -1,5 +1,4 @@ import subprocess -import os def check_subcommand(command, *args): @@ -13,14 +12,13 @@ def test_cformat(): def test_compile(): + # keyboard compile assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 - assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest2/keymap.json').returncode == 0 - assert check_subcommand('compile', 'keyboards/handwired/onekey/pytest2/keymap.json', '-x').returncode == 0 - # Cleanup - test_output = 'keyboards/handwired/onekey/keymaps/pytest2/keymap.c' - if os.path.exists(test_output): - os.remove(test_output) + # json compile + test_json = ['encoder', 'no_encoder'] + for j in test_json: + assert check_subcommand('compile', f'keyboards/handwired/onekey/pytest2/{j}.json').returncode == 0 # with encoder def test_flash(): From 1ffd65acadfc9a6e93f2d51d5f74436891600d20 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Thu, 19 Mar 2020 11:17:17 -0700 Subject: [PATCH 11/19] Trying to fix the weird compilation issues on the build server --- keyboards/handwired/onekey/pytest2/config.h | 23 --------------------- keyboards/handwired/onekey/pytest2/rules.mk | 2 -- 2 files changed, 25 deletions(-) delete mode 100644 keyboards/handwired/onekey/pytest2/config.h delete mode 100644 keyboards/handwired/onekey/pytest2/rules.mk diff --git a/keyboards/handwired/onekey/pytest2/config.h b/keyboards/handwired/onekey/pytest2/config.h deleted file mode 100644 index f6bedcfe6481..000000000000 --- a/keyboards/handwired/onekey/pytest2/config.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright 2019 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include "config_common.h" - -#define MATRIX_COL_PINS { A3 } -#define MATRIX_ROW_PINS { A2 } -#define UNUSED_PINS diff --git a/keyboards/handwired/onekey/pytest2/rules.mk b/keyboards/handwired/onekey/pytest2/rules.mk deleted file mode 100644 index b17a3d0316b5..000000000000 --- a/keyboards/handwired/onekey/pytest2/rules.mk +++ /dev/null @@ -1,2 +0,0 @@ -# MCU name -MCU = STM32F303 From cb091b60bbfb12bfccb4c928846a27874193ac60 Mon Sep 17 00:00:00 2001 From: xplusplus Date: Sat, 21 Mar 2020 17:26:56 -0700 Subject: [PATCH 12/19] Removing extra newline --- lib/python/qmk/keymap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index e23a3a4bb536..56c62c342e3c 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -89,7 +89,7 @@ def generate(keyboard, layout, layers, encoders=None): curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) encoders_txt.append(curr_encoder) - encoder_func = '\n'.join(encoders_txt) + encoder_func = '\n'.join(encoders_txt)[:-1] keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_func) From c3787c950077f5effedfe079a8baf657129c36ef Mon Sep 17 00:00:00 2001 From: xplusplus Date: Sun, 22 Mar 2020 14:03:25 -0700 Subject: [PATCH 13/19] Updating tap_code to tap_code16 to accomadate modifiers per fauxpark suggestion --- lib/python/qmk/keymap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 56c62c342e3c..31f5c9a635bb 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -26,9 +26,9 @@ ENCODER_IF = """\t_ELIF_ (index == _INDEX_) { \t\tif (clockwise) { -\t\t\ttap_code(_CLOCKWISE_); +\t\t\ttap_code16(_CLOCKWISE_); \t\t} else { -\t\t\ttap_code(_COUNTER_); +\t\t\ttap_code16(_COUNTER_); \t\t} \t} """ From b611fffbad4349105749b78a5432db47ba3f429f Mon Sep 17 00:00:00 2001 From: xplusplus Date: Sun, 22 Mar 2020 15:54:29 -0700 Subject: [PATCH 14/19] Enforcing basic keycode usage for encoder support per fauxpark --- lib/python/qmk/keymap.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 31f5c9a635bb..1a752016ad8b 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -84,18 +84,33 @@ def generate(keyboard, layout, layers, encoders=None): if encoders: encoders_txt = [] for encoder_set in encoders: - curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_set['index'] == 0 else 'else if') - curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set['index'])) - curr_encoder = curr_encoder.replace('_CLOCKWISE_', encoder_set['clockwise']) - curr_encoder = curr_encoder.replace('_COUNTER_', encoder_set['counter']) + curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_set.get('index') == 0 else 'else if') + curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set.get('index'))) + + clockwise = parse_basic_code(encoder_set.get('clockwise')) + counter = parse_basic_code(encoder_set.get('counter')) + + curr_encoder = curr_encoder.replace('_CLOCKWISE_', clockwise) + curr_encoder = curr_encoder.replace('_COUNTER_', counter) + encoders_txt.append(curr_encoder) + encoder_func = '\n'.join(encoders_txt)[:-1] keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_func) - return keymap_c +def parse_basic_code(keycode): + """Performs a simplistic check for a 'basic' keycode. Otherwise it returns 'KC_NO' + + Args: + keycode + The keycode the user is trying to apply + """ + return keycode if keycode[:2].lower() == 'kc' else 'KC_NO' + + def write(keyboard, keymap, layout, layers, encoders=None): """Generate the `keymap.c` and write it to disk. From 190b9d11f9c463efac480361bdb3ed5272dd5077 Mon Sep 17 00:00:00 2001 From: xplusplus Date: Sun, 22 Mar 2020 16:00:39 -0700 Subject: [PATCH 15/19] pyformat bullshirt --- lib/python/qmk/keymap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 1a752016ad8b..2e56e2bede93 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -103,7 +103,7 @@ def generate(keyboard, layout, layers, encoders=None): def parse_basic_code(keycode): """Performs a simplistic check for a 'basic' keycode. Otherwise it returns 'KC_NO' - + Args: keycode The keycode the user is trying to apply From 42bbe061129bbecc663a776348528dacbbac8d6d Mon Sep 17 00:00:00 2001 From: xplusplus Date: Sat, 4 Apr 2020 22:50:25 -0700 Subject: [PATCH 16/19] Changing test folder name --- .../handwired/onekey/{pytest2 => encodertest}/encoder.json | 0 .../handwired/onekey/{pytest2 => encodertest}/no_encoder.json | 0 lib/python/qmk/tests/test_cli_commands.py | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename keyboards/handwired/onekey/{pytest2 => encodertest}/encoder.json (100%) rename keyboards/handwired/onekey/{pytest2 => encodertest}/no_encoder.json (100%) diff --git a/keyboards/handwired/onekey/pytest2/encoder.json b/keyboards/handwired/onekey/encodertest/encoder.json similarity index 100% rename from keyboards/handwired/onekey/pytest2/encoder.json rename to keyboards/handwired/onekey/encodertest/encoder.json diff --git a/keyboards/handwired/onekey/pytest2/no_encoder.json b/keyboards/handwired/onekey/encodertest/no_encoder.json similarity index 100% rename from keyboards/handwired/onekey/pytest2/no_encoder.json rename to keyboards/handwired/onekey/encodertest/no_encoder.json diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 0cebda42e87e..c53a501dcf42 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -19,7 +19,7 @@ def test_compile(): # json compile test_json = ['encoder', 'no_encoder'] for j in test_json: - assert check_subcommand('compile', f'keyboards/handwired/onekey/pytest2/{j}.json').returncode == 0 # with encoder + assert check_subcommand('compile', f'keyboards/handwired/onekey/encodertest/{j}.json').returncode == 0 # with encoder def test_flash(): From c9e379e3d4cdfadc2dd3d95a7ffe3a5c335a43bb Mon Sep 17 00:00:00 2001 From: xplusplus Date: Sun, 19 Apr 2020 20:12:40 -0700 Subject: [PATCH 17/19] Erovia's Latest Comments --- .../default_json/keymap.json} | 4 ++-- .../encodertest/keymap.json} | 5 ++--- lib/python/qmk/cli/compile.py | 3 --- lib/python/qmk/cli/flash.py | 2 -- lib/python/qmk/commands.py | 8 ++++---- lib/python/qmk/keymap.py | 17 ++++++++--------- lib/python/qmk/tests/test_cli_commands.py | 13 ++++++++----- 7 files changed, 24 insertions(+), 28 deletions(-) rename keyboards/handwired/onekey/{encodertest/no_encoder.json => keymaps/default_json/keymap.json} (59%) rename keyboards/handwired/onekey/{encodertest/encoder.json => keymaps/encodertest/keymap.json} (71%) diff --git a/keyboards/handwired/onekey/encodertest/no_encoder.json b/keyboards/handwired/onekey/keymaps/default_json/keymap.json similarity index 59% rename from keyboards/handwired/onekey/encodertest/no_encoder.json rename to keyboards/handwired/onekey/keymaps/default_json/keymap.json index 11519b5cadda..c13d0139f59c 100644 --- a/keyboards/handwired/onekey/encodertest/no_encoder.json +++ b/keyboards/handwired/onekey/keymaps/default_json/keymap.json @@ -1,6 +1,6 @@ { - "keyboard": "handwired/onekey", - "keymap": "no_encoder", + "keyboard": "handwired/onekey/pytest", + "keymap": "default_json", "layout": "LAYOUT", "layers": [ [ diff --git a/keyboards/handwired/onekey/encodertest/encoder.json b/keyboards/handwired/onekey/keymaps/encodertest/keymap.json similarity index 71% rename from keyboards/handwired/onekey/encodertest/encoder.json rename to keyboards/handwired/onekey/keymaps/encodertest/keymap.json index aa45f841d003..6fa4719b3818 100644 --- a/keyboards/handwired/onekey/encodertest/encoder.json +++ b/keyboards/handwired/onekey/keymaps/encodertest/keymap.json @@ -1,6 +1,6 @@ { - "keyboard": "handwired/onekey", - "keymap": "encoder", + "keyboard": "handwired/onekey/pytest", + "keymap": "encodertest", "layout": "LAYOUT", "layers": [ [ @@ -11,7 +11,6 @@ "notes": "", "encoders": [ { - "index": 0, "clockwise": "KC_UP", "counter": "KC_DOWN" } diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index 8d330174a566..edb85bece08d 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -32,12 +32,9 @@ def compile(cli): # If a configurator JSON was provided generate a keymap and compile it # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. - - # Parse the configurator json user_keymap = parse_configurator_json(cli.args.filename) keymap_path = qmk.path.keymap(user_keymap['keyboard']) - # Compile the keymap command = compile_configurator_json(user_keymap) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py index c4c0bd360d51..fc32dfa17563 100644 --- a/lib/python/qmk/cli/flash.py +++ b/lib/python/qmk/cli/flash.py @@ -61,8 +61,6 @@ def flash(cli): # Handle compiling a configurator JSON user_keymap = parse_configurator_json(cli.args.filename) keymap_path = qmk.path.keymap(user_keymap['keyboard']) - - # Convert the JSON into a C file and write it to disk. command = compile_configurator_json(user_keymap, bootloader=cli.args.bootloader) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index 1d653f5a5db2..d041935bc9cc 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -53,12 +53,12 @@ def compile_configurator_json(user_keymap, bootloader=None): A command to run to compile and flash the C file. """ # Write the keymap C file - qmk.keymap.write(user_keymap.get('keyboard'), user_keymap.get('keymap'), user_keymap.get('layout'), user_keymap.get('layers'), user_keymap.get('encoders')) + qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], user_keymap.get('encoders')) - # Return a command that can be run to make the keymap and flash if given + # Return a command that can be run to make the keymap and flashf if given if bootloader is None: - return create_make_command(user_keymap.get('keyboard'), user_keymap.get('keymap')) - return create_make_command(user_keymap.get('keyboard'), user_keymap.get('keymap'), bootloader) + return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) + return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) def parse_configurator_json(configurator_file): diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 22ac84c272d6..c5e26aee74df 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -17,8 +17,9 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { __KEYMAP_GOES_HERE__ }; +""" -void encoder_update_user(uint8_t index, bool clockwise) { +ENCODER_SUPPORT = """void encoder_update_user(uint8_t index, bool clockwise) { __ENCODERS_GO_HERE__ }; """ @@ -29,8 +30,7 @@ \t\t} else { \t\t\ttap_code16(_COUNTER_); \t\t} -\t} -""" +\t}""" def template(keyboard): @@ -91,12 +91,12 @@ def generate(keyboard, layout, layers, encoders=None): keymap_c = keymap_c.replace('__KEYMAP_GOES_HERE__', keymap) - encoder_func = '' if encoders: + keymap_c += ENCODER_SUPPORT encoders_txt = [] - for encoder_set in encoders: - curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_set.get('index') == 0 else 'else if') - curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_set.get('index'))) + for encoder_num, encoder_set in enumerate(encoders): + curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_num == 0 else 'else if') + curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_num)) clockwise = parse_basic_code(encoder_set.get('clockwise')) counter = parse_basic_code(encoder_set.get('counter')) @@ -106,9 +106,8 @@ def generate(keyboard, layout, layers, encoders=None): encoders_txt.append(curr_encoder) - encoder_func = '\n'.join(encoders_txt)[:-1] + keymap_c = keymap_c.replace('ENCODERS_GO_HERE__', '\n'.join(encoders_txt)) - keymap_c = keymap_c.replace('__ENCODERS_GO_HERE__', encoder_func) return keymap_c diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index c53a501dcf42..05d2325298af 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py @@ -12,15 +12,18 @@ def test_cformat(): assert result.returncode == 0 +def test_compile_json(): + assert check_subcommand('compile', 'keyboards/handwired/onekey/keymaps/default_json/keymap.json').returncode == 0 + + +def test_compile_json_encoder(): + assert check_subcommand('compile', 'keyboards/handwired/onekey/keymaps/encodertest/keymap.json').returncode == 0 + + def test_compile(): # keyboard compile assert check_subcommand('compile', '-kb', 'handwired/onekey/pytest', '-km', 'default').returncode == 0 - # json compile - test_json = ['encoder', 'no_encoder'] - for j in test_json: - assert check_subcommand('compile', f'keyboards/handwired/onekey/encodertest/{j}.json').returncode == 0 # with encoder - def test_flash(): assert check_subcommand('flash', '-b').returncode == 1 From 946b042689c2e501da815d23f451bda7e1e2fe78 Mon Sep 17 00:00:00 2001 From: Lauren Harris Date: Mon, 20 Apr 2020 17:07:00 -0700 Subject: [PATCH 18/19] Removing unnecessary changes --- lib/python/qmk/cli/compile.py | 2 -- lib/python/qmk/commands.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py index edb85bece08d..6480d624b043 100755 --- a/lib/python/qmk/cli/compile.py +++ b/lib/python/qmk/cli/compile.py @@ -29,12 +29,10 @@ def compile(cli): command = None if cli.args.filename: - # If a configurator JSON was provided generate a keymap and compile it # FIXME(skullydazed): add code to check and warn if the keymap already exists when compiling a json keymap. user_keymap = parse_configurator_json(cli.args.filename) keymap_path = qmk.path.keymap(user_keymap['keyboard']) - command = compile_configurator_json(user_keymap) cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap']) diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py index d041935bc9cc..e486e9b38546 100644 --- a/lib/python/qmk/commands.py +++ b/lib/python/qmk/commands.py @@ -55,7 +55,7 @@ def compile_configurator_json(user_keymap, bootloader=None): # Write the keymap C file qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'], user_keymap.get('encoders')) - # Return a command that can be run to make the keymap and flashf if given + # Return a command that can be run to make the keymap and flash if given if bootloader is None: return create_make_command(user_keymap['keyboard'], user_keymap['keymap']) return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader) From 2c6cd2a358e077647c809326fa5289dc9063d39d Mon Sep 17 00:00:00 2001 From: xplusplus Date: Tue, 15 Dec 2020 19:40:16 -0800 Subject: [PATCH 19/19] Some updates --- .../onekey/keymaps/encodertest/keymap.json | 17 +++++--------- lib/python/qmk/keymap.py | 22 +++++++++++++++++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/keyboards/handwired/onekey/keymaps/encodertest/keymap.json b/keyboards/handwired/onekey/keymaps/encodertest/keymap.json index 6fa4719b3818..48f36a40940c 100644 --- a/keyboards/handwired/onekey/keymaps/encodertest/keymap.json +++ b/keyboards/handwired/onekey/keymaps/encodertest/keymap.json @@ -2,17 +2,12 @@ "keyboard": "handwired/onekey/pytest", "keymap": "encodertest", "layout": "LAYOUT", - "layers": [ - [ - "KC_ENTER" - ] - ], - "author": "", - "notes": "", - "encoders": [ - { + "layers": [["KC_ENTER"]], + "author": "qmk", + "notes": "This file is a keymap.json file for handwired/onekey/pytest. It contains encoders", + "version": 1, + "encoders": [{ "clockwise": "KC_UP", "counter": "KC_DOWN" - } - ] + }] } \ No newline at end of file diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index bcfaa816cde3..7f549df28cc8 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -144,7 +144,7 @@ def generate_json(keymap, keyboard, layout, layers): return new_keymap -def generate_c(keyboard, layout, layers): +def generate_c(keyboard, layout, layers, encoders=None): """Returns a `keymap.c` or `keymap.json` for the specified keyboard, layout, and layers. Args: @@ -172,6 +172,24 @@ def generate_c(keyboard, layout, layers): keymap = '\n'.join(layer_txt) new_keymap = new_keymap.replace('__KEYMAP_GOES_HERE__', keymap) + if encoders: + new_keymap += ENCODER_SUPPORT + + encoders_txt = [] + for encoder_num, encoder_set in enumerate(encoders): + curr_encoder = ENCODER_IF.replace('_ELIF_', 'if' if encoder_num == 0 else 'else if') + curr_encoder = curr_encoder.replace('_INDEX_', str(encoder_num)) + + clockwise = parse_basic_code(encoder_set.get('clockwise')) + counter = parse_basic_code(encoder_set.get('counter')) + + curr_encoder = curr_encoder.replace('_CLOCKWISE_', clockwise) + curr_encoder = curr_encoder.replace('_COUNTER_', counter) + + encoders_txt.append(curr_encoder) + + new_keymap = new_keymap.replace('__ENCODERS_GO_HERE__', '\n'.join(encoders_txt)) + return new_keymap @@ -244,7 +262,7 @@ def write(keyboard, keymap, layout, layers, encoders=None): encoders An array of encoders on the keyboard. Each item includes the index and the counter- and clockwise key codes. """ - keymap_content = generate_c(keyboard, layout, layers) + keymap_content = generate_c(keyboard, layout, layers, encoders) keymap_file = qmk.path.keymap(keyboard) / keymap / 'keymap.c' return write_file(keymap_file, keymap_content)