diff options
Diffstat (limited to 'lib/python/qmk/cli/generate/config_h.py')
-rwxr-xr-x | lib/python/qmk/cli/generate/config_h.py | 245 |
1 files changed, 60 insertions, 185 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 | ||