diff options
| author | Ryan <fauxpark@gmail.com> | 2022-09-22 03:31:57 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-21 18:31:57 +0100 |
| commit | 2f48d300f4fdce10d4183279f68cf4fe355cf605 (patch) | |
| tree | f71b8ddec7e68a9255f783226b4a159438753b2b /lib/python/qmk | |
| parent | fc7f3f17e6bda78fb151d09f73faf4d673ec77af (diff) | |
Normalise info_config.h define generation (#18439)
* Normalise info_config.h define generation
* format
* Fix tests
* Update lib/python/qmk/cli/generate/config_h.py
Co-authored-by: Nick Brassel <nick@tzarc.org>
Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'lib/python/qmk')
| -rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 114 | ||||
| -rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 20 |
2 files changed, 41 insertions, 93 deletions
diff --git a/lib/python/qmk/cli/generate/config_h.py b/lib/python/qmk/cli/generate/config_h.py index a2178bf1e9..d6e87c8803 100755 --- a/lib/python/qmk/cli/generate/config_h.py +++ b/lib/python/qmk/cli/generate/config_h.py | |||
| @@ -13,6 +13,14 @@ from qmk.path import normpath | |||
| 13 | from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE | 13 | from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | def generate_define(define, value=None): | ||
| 17 | value = f' {value}' if value is not None else '' | ||
| 18 | return f""" | ||
| 19 | #ifndef {define} | ||
| 20 | # define {define}{value} | ||
| 21 | #endif // {define}""" | ||
| 22 | |||
| 23 | |||
| 16 | def direct_pins(direct_pins, postfix): | 24 | def direct_pins(direct_pins, postfix): |
| 17 | """Return the config.h lines that set the direct pins. | 25 | """Return the config.h lines that set the direct pins. |
| 18 | """ | 26 | """ |
| @@ -22,11 +30,7 @@ def direct_pins(direct_pins, postfix): | |||
| 22 | cols = ','.join(map(str, [col or 'NO_PIN' for col in row])) | 30 | cols = ','.join(map(str, [col or 'NO_PIN' for col in row])) |
| 23 | rows.append('{' + cols + '}') | 31 | rows.append('{' + cols + '}') |
| 24 | 32 | ||
| 25 | return f""" | 33 | return generate_define(f'DIRECT_PINS{postfix}', f'{{ {", ".join(rows)} }}') |
| 26 | #ifndef DIRECT_PINS{postfix} | ||
| 27 | # define DIRECT_PINS{postfix} {{ {", ".join(rows)} }} | ||
| 28 | #endif // DIRECT_PINS{postfix} | ||
| 29 | """ | ||
| 30 | 34 | ||
| 31 | 35 | ||
| 32 | def pin_array(define, pins, postfix): | 36 | def pin_array(define, pins, postfix): |
| @@ -34,11 +38,7 @@ def pin_array(define, pins, postfix): | |||
| 34 | """ | 38 | """ |
| 35 | pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins])) | 39 | pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins])) |
| 36 | 40 | ||
| 37 | return f""" | 41 | return generate_define(f'{define}_PINS{postfix}', f'{{ {pin_array} }}') |
| 38 | #ifndef {define}_PINS{postfix} | ||
| 39 | # define {define}_PINS{postfix} {{ {pin_array} }} | ||
| 40 | #endif // {define}_PINS{postfix} | ||
| 41 | """ | ||
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | def matrix_pins(matrix_pins, postfix=''): | 44 | def matrix_pins(matrix_pins, postfix=''): |
| @@ -62,18 +62,8 @@ def generate_matrix_size(kb_info_json, config_h_lines): | |||
| 62 | """Add the matrix size to the config.h. | 62 | """Add the matrix size to the config.h. |
| 63 | """ | 63 | """ |
| 64 | if 'matrix_pins' in kb_info_json: | 64 | if 'matrix_pins' in kb_info_json: |
| 65 | col_count = kb_info_json['matrix_size']['cols'] | 65 | config_h_lines.append(generate_define('MATRIX_COLS', kb_info_json['matrix_size']['cols'])) |
| 66 | row_count = kb_info_json['matrix_size']['rows'] | 66 | config_h_lines.append(generate_define('MATRIX_ROWS', kb_info_json['matrix_size']['rows'])) |
| 67 | |||
| 68 | config_h_lines.append(f""" | ||
| 69 | #ifndef MATRIX_COLS | ||
| 70 | # define MATRIX_COLS {col_count} | ||
| 71 | #endif // MATRIX_COLS | ||
| 72 | |||
| 73 | #ifndef MATRIX_ROWS | ||
| 74 | # define MATRIX_ROWS {row_count} | ||
| 75 | #endif // MATRIX_ROWS | ||
| 76 | """) | ||
| 77 | 67 | ||
| 78 | 68 | ||
| 79 | def generate_config_items(kb_info_json, config_h_lines): | 69 | def generate_config_items(kb_info_json, config_h_lines): |
| @@ -95,44 +85,23 @@ def generate_config_items(kb_info_json, config_h_lines): | |||
| 95 | continue | 85 | continue |
| 96 | 86 | ||
| 97 | if key_type.startswith('array.array'): | 87 | if key_type.startswith('array.array'): |
| 98 | config_h_lines.append('') | 88 | config_h_lines.append(generate_define(config_key, f'{{ {", ".join(["{" + ",".join(list(map(str, x))) + "}" for x in config_value])} }}')) |
| 99 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 100 | config_h_lines.append(f'# define {config_key} {{ {", ".join(["{" + ",".join(list(map(str, x))) + "}" for x in config_value])} }}') | ||
| 101 | config_h_lines.append(f'#endif // {config_key}') | ||
| 102 | elif key_type.startswith('array'): | 89 | elif key_type.startswith('array'): |
| 103 | config_h_lines.append('') | 90 | config_h_lines.append(generate_define(config_key, f'{{ {", ".join(map(str, config_value))} }}')) |
| 104 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 105 | config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}') | ||
| 106 | config_h_lines.append(f'#endif // {config_key}') | ||
| 107 | elif key_type == 'bool': | 91 | elif key_type == 'bool': |
| 108 | if config_value: | 92 | if config_value: |
| 109 | config_h_lines.append('') | 93 | config_h_lines.append(generate_define(config_key)) |
| 110 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 111 | config_h_lines.append(f'# define {config_key}') | ||
| 112 | config_h_lines.append(f'#endif // {config_key}') | ||
| 113 | elif key_type == 'mapping': | 94 | elif key_type == 'mapping': |
| 114 | for key, value in config_value.items(): | 95 | for key, value in config_value.items(): |
| 115 | config_h_lines.append('') | 96 | config_h_lines.append(generate_define(key, value)) |
| 116 | config_h_lines.append(f'#ifndef {key}') | ||
| 117 | config_h_lines.append(f'# define {key} {value}') | ||
| 118 | config_h_lines.append(f'#endif // {key}') | ||
| 119 | elif key_type == 'str': | 97 | elif key_type == 'str': |
| 120 | escaped_str = config_value.replace('\\', '\\\\').replace('"', '\\"') | 98 | escaped_str = config_value.replace('\\', '\\\\').replace('"', '\\"') |
| 121 | config_h_lines.append('') | 99 | config_h_lines.append(generate_define(config_key, f'"{escaped_str}"')) |
| 122 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 123 | config_h_lines.append(f'# define {config_key} "{escaped_str}"') | ||
| 124 | config_h_lines.append(f'#endif // {config_key}') | ||
| 125 | elif key_type == 'bcd_version': | 100 | elif key_type == 'bcd_version': |
| 126 | (major, minor, revision) = config_value.split('.') | 101 | (major, minor, revision) = config_value.split('.') |
| 127 | config_h_lines.append('') | 102 | config_h_lines.append(generate_define(config_key, f'0x{major.zfill(2)}{minor}{revision}')) |
| 128 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 129 | config_h_lines.append(f'# define {config_key} 0x{major.zfill(2)}{minor}{revision}') | ||
| 130 | config_h_lines.append(f'#endif // {config_key}') | ||
| 131 | else: | 103 | else: |
| 132 | config_h_lines.append('') | 104 | config_h_lines.append(generate_define(config_key, config_value)) |
| 133 | config_h_lines.append(f'#ifndef {config_key}') | ||
| 134 | config_h_lines.append(f'# define {config_key} {config_value}') | ||
| 135 | config_h_lines.append(f'#endif // {config_key}') | ||
| 136 | 105 | ||
| 137 | 106 | ||
| 138 | def generate_encoder_config(encoder_json, config_h_lines, postfix=''): | 107 | def generate_encoder_config(encoder_json, config_h_lines, postfix=''): |
| @@ -145,24 +114,15 @@ def generate_encoder_config(encoder_json, config_h_lines, postfix=''): | |||
| 145 | b_pads.append(encoder["pin_b"]) | 114 | b_pads.append(encoder["pin_b"]) |
| 146 | resolutions.append(encoder.get("resolution", None)) | 115 | resolutions.append(encoder.get("resolution", None)) |
| 147 | 116 | ||
| 148 | config_h_lines.append(f'#ifndef ENCODERS_PAD_A{postfix}') | 117 | config_h_lines.append(generate_define(f'ENCODERS_PAD_A{postfix}', f'{{ {", ".join(a_pads)} }}')) |
| 149 | config_h_lines.append(f'# define ENCODERS_PAD_A{postfix} {{ { ", ".join(a_pads) } }}') | 118 | config_h_lines.append(generate_define(f'ENCODERS_PAD_B{postfix}', f'{{ {", ".join(b_pads)} }}')) |
| 150 | config_h_lines.append(f'#endif // ENCODERS_PAD_A{postfix}') | ||
| 151 | |||
| 152 | config_h_lines.append(f'#ifndef ENCODERS_PAD_B{postfix}') | ||
| 153 | config_h_lines.append(f'# define ENCODERS_PAD_B{postfix} {{ { ", ".join(b_pads) } }}') | ||
| 154 | config_h_lines.append(f'#endif // ENCODERS_PAD_B{postfix}') | ||
| 155 | 119 | ||
| 156 | if None in resolutions: | 120 | if None in resolutions: |
| 157 | cli.log.debug("Unable to generate ENCODER_RESOLUTION configuration") | 121 | cli.log.debug("Unable to generate ENCODER_RESOLUTION configuration") |
| 158 | elif len(set(resolutions)) == 1: | 122 | elif len(set(resolutions)) == 1: |
| 159 | config_h_lines.append(f'#ifndef ENCODER_RESOLUTION{postfix}') | 123 | config_h_lines.append(generate_define(f'ENCODER_RESOLUTION{postfix}', resolutions[0])) |
| 160 | config_h_lines.append(f'# define ENCODER_RESOLUTION{postfix} { resolutions[0] }') | ||
| 161 | config_h_lines.append(f'#endif // ENCODER_RESOLUTION{postfix}') | ||
| 162 | else: | 124 | else: |
| 163 | config_h_lines.append(f'#ifndef ENCODER_RESOLUTIONS{postfix}') | 125 | config_h_lines.append(generate_define(f'ENCODER_RESOLUTIONS{postfix}', f'{{ {", ".join(map(str,resolutions))} }}')) |
| 164 | config_h_lines.append(f'# define ENCODER_RESOLUTIONS{postfix} {{ { ", ".join(map(str,resolutions)) } }}') | ||
| 165 | config_h_lines.append(f'#endif // ENCODER_RESOLUTIONS{postfix}') | ||
| 166 | 126 | ||
| 167 | 127 | ||
| 168 | def generate_split_config(kb_info_json, config_h_lines): | 128 | def generate_split_config(kb_info_json, config_h_lines): |
| @@ -171,35 +131,23 @@ def generate_split_config(kb_info_json, config_h_lines): | |||
| 171 | if kb_info_json['split']['primary'] in ('left', 'right'): | 131 | if kb_info_json['split']['primary'] in ('left', 'right'): |
| 172 | config_h_lines.append('') | 132 | config_h_lines.append('') |
| 173 | config_h_lines.append('#ifndef MASTER_LEFT') | 133 | config_h_lines.append('#ifndef MASTER_LEFT') |
| 174 | config_h_lines.append('# ifndef MASTER_RIGHT') | 134 | config_h_lines.append('# ifndef MASTER_RIGHT') |
| 175 | if kb_info_json['split']['primary'] == 'left': | 135 | if kb_info_json['split']['primary'] == 'left': |
| 176 | config_h_lines.append('# define MASTER_LEFT') | 136 | config_h_lines.append('# define MASTER_LEFT') |
| 177 | elif kb_info_json['split']['primary'] == 'right': | 137 | elif kb_info_json['split']['primary'] == 'right': |
| 178 | config_h_lines.append('# define MASTER_RIGHT') | 138 | config_h_lines.append('# define MASTER_RIGHT') |
| 179 | config_h_lines.append('# endif // MASTER_RIGHT') | 139 | config_h_lines.append('# endif // MASTER_RIGHT') |
| 180 | config_h_lines.append('#endif // MASTER_LEFT') | 140 | config_h_lines.append('#endif // MASTER_LEFT') |
| 181 | elif kb_info_json['split']['primary'] == 'pin': | 141 | elif kb_info_json['split']['primary'] == 'pin': |
| 182 | config_h_lines.append('') | 142 | config_h_lines.append(generate_define('SPLIT_HAND_PIN')) |
| 183 | config_h_lines.append('#ifndef SPLIT_HAND_PIN') | ||
| 184 | config_h_lines.append('# define SPLIT_HAND_PIN') | ||
| 185 | config_h_lines.append('#endif // SPLIT_HAND_PIN') | ||
| 186 | elif kb_info_json['split']['primary'] == 'matrix_grid': | 143 | elif kb_info_json['split']['primary'] == 'matrix_grid': |
| 187 | config_h_lines.append('') | 144 | config_h_lines.append(generate_define('SPLIT_HAND_MATRIX_GRID', f'{{ {",".join(kb_info_json["split"]["matrix_grid"])} }}')) |
| 188 | config_h_lines.append('#ifndef SPLIT_HAND_MATRIX_GRID') | ||
| 189 | config_h_lines.append('# define SPLIT_HAND_MATRIX_GRID {%s}' % (','.join(kb_info_json["split"]["matrix_grid"],))) | ||
| 190 | config_h_lines.append('#endif // SPLIT_HAND_MATRIX_GRID') | ||
| 191 | elif kb_info_json['split']['primary'] == 'eeprom': | 145 | elif kb_info_json['split']['primary'] == 'eeprom': |
| 192 | config_h_lines.append('') | 146 | config_h_lines.append(generate_define('EE_HANDS')) |
| 193 | config_h_lines.append('#ifndef EE_HANDS') | ||
| 194 | config_h_lines.append('# define EE_HANDS') | ||
| 195 | config_h_lines.append('#endif // EE_HANDS') | ||
| 196 | 147 | ||
| 197 | if 'protocol' in kb_info_json['split'].get('transport', {}): | 148 | if 'protocol' in kb_info_json['split'].get('transport', {}): |
| 198 | if kb_info_json['split']['transport']['protocol'] == 'i2c': | 149 | if kb_info_json['split']['transport']['protocol'] == 'i2c': |
| 199 | config_h_lines.append('') | 150 | config_h_lines.append(generate_define('USE_I2C')) |
| 200 | config_h_lines.append('#ifndef USE_I2C') | ||
| 201 | config_h_lines.append('# define USE_I2C') | ||
| 202 | config_h_lines.append('#endif // USE_I2C') | ||
| 203 | 151 | ||
| 204 | if 'right' in kb_info_json['split'].get('matrix_pins', {}): | 152 | if 'right' in kb_info_json['split'].get('matrix_pins', {}): |
| 205 | config_h_lines.append(matrix_pins(kb_info_json['split']['matrix_pins']['right'], '_RIGHT')) | 153 | config_h_lines.append(matrix_pins(kb_info_json['split']['matrix_pins']['right'], '_RIGHT')) |
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index c8c4e2f80c..9bfc5a0a79 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
| @@ -263,16 +263,16 @@ def test_generate_rgb_breathe_table(): | |||
| 263 | def test_generate_config_h(): | 263 | def test_generate_config_h(): |
| 264 | result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic') | 264 | result = check_subcommand('generate-config-h', '-kb', 'handwired/pytest/basic') |
| 265 | check_returncode(result) | 265 | check_returncode(result) |
| 266 | assert '# define DEVICE_VER 0x0001' in result.stdout | 266 | assert '# define DEVICE_VER 0x0001' in result.stdout |
| 267 | assert '# define DIODE_DIRECTION COL2ROW' in result.stdout | 267 | assert '# define DIODE_DIRECTION COL2ROW' in result.stdout |
| 268 | assert '# define MANUFACTURER "none"' in result.stdout | 268 | assert '# define MANUFACTURER "none"' in result.stdout |
| 269 | assert '# define PRODUCT "pytest"' in result.stdout | 269 | assert '# define PRODUCT "pytest"' in result.stdout |
| 270 | assert '# define PRODUCT_ID 0x6465' in result.stdout | 270 | assert '# define PRODUCT_ID 0x6465' in result.stdout |
| 271 | assert '# define VENDOR_ID 0xFEED' in result.stdout | 271 | assert '# define VENDOR_ID 0xFEED' in result.stdout |
| 272 | assert '# define MATRIX_COLS 1' in result.stdout | 272 | assert '# define MATRIX_COLS 1' in result.stdout |
| 273 | assert '# define MATRIX_COL_PINS { F4 }' in result.stdout | 273 | assert '# define MATRIX_COL_PINS { F4 }' in result.stdout |
| 274 | assert '# define MATRIX_ROWS 1' in result.stdout | 274 | assert '# define MATRIX_ROWS 1' in result.stdout |
| 275 | assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout | 275 | assert '# define MATRIX_ROW_PINS { F5 }' in result.stdout |
| 276 | 276 | ||
| 277 | 277 | ||
| 278 | def test_generate_rules_mk(): | 278 | def test_generate_rules_mk(): |
