diff options
Diffstat (limited to 'lib/python/qmk/cli/generate')
| -rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 245 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/generate/rules_mk.py | 61 |
2 files changed, 97 insertions, 209 deletions
diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index 1de84de7a..7ddad745d 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py | |||
| @@ -1,62 +1,14 @@ | |||
| 1 | """Used by the make system to generate info_config.h from info.json. | 1 | """Used by the make system to generate info_config.h from info.json. |
| 2 | """ | 2 | """ |
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from dotty_dict import dotty | ||
| 3 | from milc import cli | 6 | from milc import cli |
| 4 | 7 | ||
| 5 | from qmk.constants import LED_INDICATORS | ||
| 6 | from qmk.decorators import automagic_keyboard, automagic_keymap | 8 | from qmk.decorators import automagic_keyboard, automagic_keymap |
| 7 | from qmk.info import info_json, rgblight_animations, rgblight_properties, rgblight_toggles | 9 | from qmk.info import _json_load, info_json |
| 8 | from qmk.path import is_keyboard, normpath | 10 | from qmk.path import is_keyboard, normpath |
| 9 | 11 | ||
| 10 | usb_prop_map = { | ||
| 11 | 'vid': 'VENDOR_ID', | ||
| 12 | 'pid': 'PRODUCT_ID', | ||
| 13 | 'device_ver': 'DEVICE_VER', | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | def debounce(debounce): | ||
| 18 | """Return the config.h lines that set debounce | ||
| 19 | """ | ||
| 20 | return """ | ||
| 21 | #ifndef DEBOUNCE | ||
| 22 | # define DEBOUNCE %s | ||
| 23 | #endif // DEBOUNCE | ||
| 24 | """ % debounce | ||
| 25 | |||
| 26 | |||
| 27 | def diode_direction(diode_direction): | ||
| 28 | """Return the config.h lines that set diode direction | ||
| 29 | """ | ||
| 30 | return """ | ||
| 31 | #ifndef DIODE_DIRECTION | ||
| 32 | # define DIODE_DIRECTION %s | ||
| 33 | #endif // DIODE_DIRECTION | ||
| 34 | """ % diode_direction | ||
| 35 | |||
| 36 | |||
| 37 | def keyboard_name(keyboard_name): | ||
| 38 | """Return the config.h lines that set the keyboard's name. | ||
| 39 | """ | ||
| 40 | return """ | ||
| 41 | #ifndef DESCRIPTION | ||
| 42 | # define DESCRIPTION %s | ||
| 43 | #endif // DESCRIPTION | ||
| 44 | |||
| 45 | #ifndef PRODUCT | ||
| 46 | # define PRODUCT %s | ||
| 47 | #endif // PRODUCT | ||
| 48 | """ % (keyboard_name.replace("'", ""), keyboard_name.replace("'", "")) | ||
| 49 | |||
| 50 | |||
| 51 | def manufacturer(manufacturer): | ||
| 52 | """Return the config.h lines that set the manufacturer. | ||
| 53 | """ | ||
| 54 | return """ | ||
| 55 | #ifndef MANUFACTURER | ||
| 56 | # define MANUFACTURER %s | ||
| 57 | #endif // MANUFACTURER | ||
| 58 | """ % (manufacturer.replace("'", "")) | ||
| 59 | |||
| 60 | 12 | ||
| 61 | def direct_pins(direct_pins): | 13 | def direct_pins(direct_pins): |
| 62 | """Return the config.h lines that set the direct pins. | 14 | """Return the config.h lines that set the direct pins. |
| @@ -72,80 +24,34 @@ def direct_pins(direct_pins): | |||
| 72 | 24 | ||
| 73 | return """ | 25 | return """ |
| 74 | #ifndef MATRIX_COLS | 26 | #ifndef MATRIX_COLS |
| 75 | # define MATRIX_COLS %s | 27 | # define MATRIX_COLS %s |
| 76 | #endif // MATRIX_COLS | 28 | #endif // MATRIX_COLS |
| 77 | 29 | ||
| 78 | #ifndef MATRIX_ROWS | 30 | #ifndef MATRIX_ROWS |
| 79 | # define MATRIX_ROWS %s | 31 | # define MATRIX_ROWS %s |
| 80 | #endif // MATRIX_ROWS | 32 | #endif // MATRIX_ROWS |
| 81 | 33 | ||
| 82 | #ifndef DIRECT_PINS | 34 | #ifndef DIRECT_PINS |
| 83 | # define DIRECT_PINS {%s} | 35 | # define DIRECT_PINS {%s} |
| 84 | #endif // DIRECT_PINS | 36 | #endif // DIRECT_PINS |
| 85 | """ % (col_count, row_count, ','.join(rows)) | 37 | """ % (col_count, row_count, ','.join(rows)) |
| 86 | 38 | ||
| 87 | 39 | ||
| 88 | def col_pins(col_pins): | 40 | def pin_array(define, pins): |
| 89 | """Return the config.h lines that set the column pins. | 41 | """Return the config.h lines that set a pin array. |
| 90 | """ | ||
| 91 | cols = ','.join(map(str, [pin or 'NO_PIN' for pin in col_pins])) | ||
| 92 | col_num = len(col_pins) | ||
| 93 | |||
| 94 | return """ | ||
| 95 | #ifndef MATRIX_COLS | ||
| 96 | # define MATRIX_COLS %s | ||
| 97 | #endif // MATRIX_COLS | ||
| 98 | |||
| 99 | #ifndef MATRIX_COL_PINS | ||
| 100 | # define MATRIX_COL_PINS {%s} | ||
| 101 | #endif // MATRIX_COL_PINS | ||
| 102 | """ % (col_num, cols) | ||
| 103 | |||
| 104 | |||
| 105 | def row_pins(row_pins): | ||
| 106 | """Return the config.h lines that set the row pins. | ||
| 107 | """ | ||
| 108 | rows = ','.join(map(str, [pin or 'NO_PIN' for pin in row_pins])) | ||
| 109 | row_num = len(row_pins) | ||
| 110 | |||
| 111 | return """ | ||
| 112 | #ifndef MATRIX_ROWS | ||
| 113 | # define MATRIX_ROWS %s | ||
| 114 | #endif // MATRIX_ROWS | ||
| 115 | |||
| 116 | #ifndef MATRIX_ROW_PINS | ||
| 117 | # define MATRIX_ROW_PINS {%s} | ||
| 118 | #endif // MATRIX_ROW_PINS | ||
| 119 | """ % (row_num, rows) | ||
| 120 | |||
| 121 | |||
| 122 | def indicators(config): | ||
| 123 | """Return the config.h lines that setup LED indicators. | ||
| 124 | """ | 42 | """ |
| 125 | defines = [] | 43 | pin_num = len(pins) |
| 44 | pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins])) | ||
| 126 | 45 | ||
| 127 | for led, define in LED_INDICATORS.items(): | 46 | return f""" |
| 128 | if led in config: | 47 | #ifndef {define}S |
| 129 | defines.append('') | 48 | # define {define}S {pin_num} |
| 130 | defines.append('#ifndef %s' % (define,)) | 49 | #endif // {define}S |
| 131 | defines.append('# define %s %s' % (define, config[led])) | ||
| 132 | defines.append('#endif // %s' % (define,)) | ||
| 133 | 50 | ||
| 134 | return '\n'.join(defines) | 51 | #ifndef {define}_PINS |
| 135 | 52 | # define {define}_PINS {{ {pin_array} }} | |
| 136 | 53 | #endif // {define}_PINS | |
| 137 | def layout_aliases(layout_aliases): | 54 | """ |
| 138 | """Return the config.h lines that setup layout aliases. | ||
| 139 | """ | ||
| 140 | defines = [] | ||
| 141 | |||
| 142 | for alias, layout in layout_aliases.items(): | ||
| 143 | defines.append('') | ||
| 144 | defines.append('#ifndef %s' % (alias,)) | ||
| 145 | defines.append('# define %s %s' % (alias, layout)) | ||
| 146 | defines.append('#endif // %s' % (alias,)) | ||
| 147 | |||
| 148 | return '\n'.join(defines) | ||
| 149 | 55 | ||
| 150 | 56 | ||
| 151 | def matrix_pins(matrix_pins): | 57 | def matrix_pins(matrix_pins): |
| @@ -157,58 +63,14 @@ def matrix_pins(matrix_pins): | |||
| 157 | pins.append(direct_pins(matrix_pins['direct'])) | 63 | pins.append(direct_pins(matrix_pins['direct'])) |
| 158 | 64 | ||
| 159 | if 'cols' in matrix_pins: | 65 | if 'cols' in matrix_pins: |
| 160 | pins.append(col_pins(matrix_pins['cols'])) | 66 | pins.append(pin_array('MATRIX_COL', matrix_pins['cols'])) |
| 161 | 67 | ||
| 162 | if 'rows' in matrix_pins: | 68 | if 'rows' in matrix_pins: |
| 163 | pins.append(row_pins(matrix_pins['rows'])) | 69 | pins.append(pin_array('MATRIX_ROW', matrix_pins['rows'])) |
| 164 | 70 | ||
| 165 | return '\n'.join(pins) | 71 | return '\n'.join(pins) |
| 166 | 72 | ||
| 167 | 73 | ||
| 168 | def rgblight(config): | ||
| 169 | """Return the config.h lines that setup rgblight. | ||
| 170 | """ | ||
| 171 | rgblight_config = [] | ||
| 172 | |||
| 173 | for json_key, config_key in rgblight_properties.items(): | ||
| 174 | if json_key in config: | ||
| 175 | rgblight_config.append('') | ||
| 176 | rgblight_config.append('#ifndef %s' % (config_key[0],)) | ||
| 177 | rgblight_config.append('# define %s %s' % (config_key[0], config[json_key])) | ||
| 178 | rgblight_config.append('#endif // %s' % (config_key[0],)) | ||
| 179 | |||
| 180 | for json_key, config_key in rgblight_toggles.items(): | ||
| 181 | if config.get(json_key): | ||
| 182 | rgblight_config.append('') | ||
| 183 | rgblight_config.append('#ifndef %s' % (config_key,)) | ||
| 184 | rgblight_config.append('# define %s' % (config_key,)) | ||
| 185 | rgblight_config.append('#endif // %s' % (config_key,)) | ||
| 186 | |||
| 187 | for json_key, config_key in rgblight_animations.items(): | ||
| 188 | if 'animations' in config and config['animations'].get(json_key): | ||
| 189 | rgblight_config.append('') | ||
| 190 | rgblight_config.append('#ifndef %s' % (config_key,)) | ||
| 191 | rgblight_config.append('# define %s' % (config_key,)) | ||
| 192 | rgblight_config.append('#endif // %s' % (config_key,)) | ||
| 193 | |||
| 194 | return '\n'.join(rgblight_config) | ||
| 195 | |||
| 196 | |||
| 197 | def usb_properties(usb_props): | ||
| 198 | """Return the config.h lines that setup USB params. | ||
| 199 | """ | ||
| 200 | usb_lines = [] | ||
| 201 | |||
| 202 | for info_name, config_name in usb_prop_map.items(): | ||
| 203 | if info_name in usb_props: | ||
| 204 | usb_lines.append('') | ||
| 205 | usb_lines.append('#ifndef ' + config_name) | ||
| 206 | usb_lines.append('# define %s %s' % (config_name, usb_props[info_name])) | ||
| 207 | usb_lines.append('#endif // ' + config_name) | ||
| 208 | |||
| 209 | return '\n'.join(usb_lines) | ||
| 210 | |||
| 211 | |||
| 212 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | 74 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') |
| 213 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") | 75 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") |
| 214 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') | 76 | @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.') |
| @@ -228,39 +90,52 @@ def generate_config_h(cli): | |||
| 228 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) | 90 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard) |
| 229 | return False | 91 | return False |
| 230 | 92 | ||
| 231 | # Build the info.json file | ||
| 232 | kb_info_json = info_json(cli.config.generate_config_h.keyboard) | ||
| 233 | |||
| 234 | # Build the info_config.h file. | 93 | # Build the info_config.h file. |
| 235 | config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] | 94 | kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard)) |
| 95 | info_config_map = _json_load(Path('data/mappings/info_config.json')) | ||
| 236 | 96 | ||
| 237 | if 'debounce' in kb_info_json: | 97 | config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once'] |
| 238 | config_h_lines.append(debounce(kb_info_json['debounce'])) | ||
| 239 | |||
| 240 | if 'diode_direction' in kb_info_json: | ||
| 241 | config_h_lines.append(diode_direction(kb_info_json['diode_direction'])) | ||
| 242 | |||
| 243 | if 'indicators' in kb_info_json: | ||
| 244 | config_h_lines.append(indicators(kb_info_json['indicators'])) | ||
| 245 | |||
| 246 | if 'keyboard_name' in kb_info_json: | ||
| 247 | config_h_lines.append(keyboard_name(kb_info_json['keyboard_name'])) | ||
| 248 | |||
| 249 | if 'layout_aliases' in kb_info_json: | ||
| 250 | config_h_lines.append(layout_aliases(kb_info_json['layout_aliases'])) | ||
| 251 | |||
| 252 | if 'manufacturer' in kb_info_json: | ||
| 253 | config_h_lines.append(manufacturer(kb_info_json['manufacturer'])) | ||
| 254 | 98 | ||
| 255 | if 'rgblight' in kb_info_json: | 99 | # Iterate through the info_config map to generate basic things |
| 256 | config_h_lines.append(rgblight(kb_info_json['rgblight'])) | 100 | for config_key, info_dict in info_config_map.items(): |
| 101 | info_key = info_dict['info_key'] | ||
| 102 | key_type = info_dict.get('value_type', 'str') | ||
| 103 | to_config = info_dict.get('to_config', True) | ||
| 104 | |||
| 105 | if not to_config: | ||
| 106 | continue | ||
| 107 | |||
| 108 | try: | ||
| 109 | config_value = kb_info_json[info_key] | ||
| 110 | except KeyError: | ||
| 111 | continue | ||
| 112 | |||
| 113 | if key_type.startswith('array'): | ||
| 114 | config_h_lines.append('') | ||
| 115 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 116 | config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}') | ||
| 117 | config_h_lines.append(f'#endif // {config_key}') | ||
| 118 | elif key_type == 'bool': | ||
| 119 | if config_value: | ||
| 120 | config_h_lines.append('') | ||
| 121 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 122 | config_h_lines.append(f'# define {config_key}') | ||
| 123 | config_h_lines.append(f'#endif // {config_key}') | ||
| 124 | elif key_type == 'mapping': | ||
| 125 | for key, value in config_value.items(): | ||
| 126 | config_h_lines.append('') | ||
| 127 | config_h_lines.append(f'#ifndef {key}') | ||
| 128 | config_h_lines.append(f'# define {key} {value}') | ||
| 129 | config_h_lines.append(f'#endif // {key}') | ||
| 130 | else: | ||
| 131 | config_h_lines.append('') | ||
| 132 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 133 | config_h_lines.append(f'# define {config_key} {config_value}') | ||
| 134 | config_h_lines.append(f'#endif // {config_key}') | ||
| 257 | 135 | ||
| 258 | if 'matrix_pins' in kb_info_json: | 136 | if 'matrix_pins' in kb_info_json: |
| 259 | config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) | 137 | config_h_lines.append(matrix_pins(kb_info_json['matrix_pins'])) |
| 260 | 138 | ||
| 261 | if 'usb' in kb_info_json: | ||
| 262 | config_h_lines.append(usb_properties(kb_info_json['usb'])) | ||
| 263 | |||
| 264 | # Show the results | 139 | # Show the results |
| 265 | config_h = '\n'.join(config_h_lines) | 140 | config_h = '\n'.join(config_h_lines) |
| 266 | 141 | ||
diff --git a/lib/python/qmk/cli/generate/rules_mk.py b/lib/python/qmk/cli/generate/rules_mk.py index b262e3c66..af740f341 100755 --- a/lib/python/qmk/cli/generate/rules_mk.py +++ b/lib/python/qmk/cli/generate/rules_mk.py | |||
| @@ -1,16 +1,37 @@ | |||
| 1 | """Used by the make system to generate a rules.mk | 1 | """Used by the make system to generate a rules.mk |
| 2 | """ | 2 | """ |
| 3 | from pathlib import Path | ||
| 4 | |||
| 5 | from dotty_dict import dotty | ||
| 3 | from milc import cli | 6 | from milc import cli |
| 4 | 7 | ||
| 5 | from qmk.decorators import automagic_keyboard, automagic_keymap | 8 | from qmk.decorators import automagic_keyboard, automagic_keymap |
| 6 | from qmk.info import info_json | 9 | from qmk.info import _json_load, info_json |
| 7 | from qmk.path import is_keyboard, normpath | 10 | from qmk.path import is_keyboard, normpath |
| 8 | 11 | ||
| 9 | info_to_rules = { | 12 | |
| 10 | 'board': 'BOARD', | 13 | def process_mapping_rule(kb_info_json, rules_key, info_dict): |
| 11 | 'bootloader': 'BOOTLOADER', | 14 | """Return the rules.mk line(s) for a mapping rule. |
| 12 | 'processor': 'MCU', | 15 | """ |
| 13 | } | 16 | if not info_dict.get('to_c', True): |
| 17 | return None | ||
| 18 | |||
| 19 | info_key = info_dict['info_key'] | ||
| 20 | key_type = info_dict.get('value_type', 'str') | ||
| 21 | |||
| 22 | try: | ||
| 23 | rules_value = kb_info_json[info_key] | ||
| 24 | except KeyError: | ||
| 25 | return None | ||
| 26 | |||
| 27 | if key_type == 'array': | ||
| 28 | return f'{rules_key} ?= {" ".join(rules_value)}' | ||
| 29 | elif key_type == 'bool': | ||
| 30 | return f'{rules_key} ?= {"on" if rules_value else "off"}' | ||
| 31 | elif key_type == 'mapping': | ||
| 32 | return '\n'.join([f'{key} ?= {value}' for key, value in rules_value.items()]) | ||
| 33 | |||
| 34 | return f'{rules_key} ?= {rules_value}' | ||
| 14 | 35 | ||
| 15 | 36 | ||
| 16 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') | 37 | @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to') |
| @@ -22,7 +43,6 @@ info_to_rules = { | |||
| 22 | def generate_rules_mk(cli): | 43 | def generate_rules_mk(cli): |
| 23 | """Generates a rules.mk file from info.json. | 44 | """Generates a rules.mk file from info.json. |
| 24 | """ | 45 | """ |
| 25 | # Determine our keyboard(s) | ||
| 26 | if not cli.config.generate_rules_mk.keyboard: | 46 | if not cli.config.generate_rules_mk.keyboard: |
| 27 | cli.log.error('Missing paramater: --keyboard') | 47 | cli.log.error('Missing paramater: --keyboard') |
| 28 | cli.subcommands['info'].print_help() | 48 | cli.subcommands['info'].print_help() |
| @@ -32,16 +52,18 @@ def generate_rules_mk(cli): | |||
| 32 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) | 52 | cli.log.error('Invalid keyboard: "%s"', cli.config.generate_rules_mk.keyboard) |
| 33 | return False | 53 | return False |
| 34 | 54 | ||
| 35 | # Build the info.json file | 55 | kb_info_json = dotty(info_json(cli.config.generate_rules_mk.keyboard)) |
| 36 | kb_info_json = info_json(cli.config.generate_rules_mk.keyboard) | 56 | info_rules_map = _json_load(Path('data/mappings/info_rules.json')) |
| 37 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] | 57 | rules_mk_lines = ['# This file was generated by `qmk generate-rules-mk`. Do not edit or copy.', ''] |
| 38 | 58 | ||
| 39 | # Bring in settings | 59 | # Iterate through the info_rules map to generate basic rules |
| 40 | for info_key, rule_key in info_to_rules.items(): | 60 | for rules_key, info_dict in info_rules_map.items(): |
| 41 | if info_key in kb_info_json: | 61 | new_entry = process_mapping_rule(kb_info_json, rules_key, info_dict) |
| 42 | rules_mk_lines.append(f'{rule_key} ?= {kb_info_json[info_key]}') | 62 | |
| 63 | if new_entry: | ||
| 64 | rules_mk_lines.append(new_entry) | ||
| 43 | 65 | ||
| 44 | # Find features that should be enabled | 66 | # Iterate through features to enable/disable them |
| 45 | if 'features' in kb_info_json: | 67 | if 'features' in kb_info_json: |
| 46 | for feature, enabled in kb_info_json['features'].items(): | 68 | for feature, enabled in kb_info_json['features'].items(): |
| 47 | if feature == 'bootmagic_lite' and enabled: | 69 | if feature == 'bootmagic_lite' and enabled: |
| @@ -51,15 +73,6 @@ def generate_rules_mk(cli): | |||
| 51 | enabled = 'yes' if enabled else 'no' | 73 | enabled = 'yes' if enabled else 'no' |
| 52 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') | 74 | rules_mk_lines.append(f'{feature}_ENABLE ?= {enabled}') |
| 53 | 75 | ||
| 54 | # Set the LED driver | ||
| 55 | if 'led_matrix' in kb_info_json and 'driver' in kb_info_json['led_matrix']: | ||
| 56 | driver = kb_info_json['led_matrix']['driver'] | ||
| 57 | rules_mk_lines.append(f'LED_MATRIX_DRIVER ?= {driver}') | ||
| 58 | |||
| 59 | # Add community layouts | ||
| 60 | if 'community_layouts' in kb_info_json: | ||
| 61 | rules_mk_lines.append(f'LAYOUTS ?= {" ".join(kb_info_json["community_layouts"])}') | ||
| 62 | |||
| 63 | # Show the results | 76 | # Show the results |
| 64 | rules_mk = '\n'.join(rules_mk_lines) + '\n' | 77 | rules_mk = '\n'.join(rules_mk_lines) + '\n' |
| 65 | 78 | ||
| @@ -72,7 +85,7 @@ def generate_rules_mk(cli): | |||
| 72 | if cli.args.quiet: | 85 | if cli.args.quiet: |
| 73 | print(cli.args.output) | 86 | print(cli.args.output) |
| 74 | else: | 87 | else: |
| 75 | cli.log.info('Wrote info_config.h to %s.', cli.args.output) | 88 | cli.log.info('Wrote rules.mk to %s.', cli.args.output) |
| 76 | 89 | ||
| 77 | else: | 90 | else: |
| 78 | print(rules_mk) | 91 | print(rules_mk) |
