diff options
Diffstat (limited to 'lib/python/qmk/info.py')
-rw-r--r-- | lib/python/qmk/info.py | 284 |
1 files changed, 248 insertions, 36 deletions
diff --git a/lib/python/qmk/info.py b/lib/python/qmk/info.py index d23b3592ee..b6f2ecf644 100644 --- a/lib/python/qmk/info.py +++ b/lib/python/qmk/info.py @@ -9,7 +9,7 @@ from milc import cli from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS from qmk.c_parse import find_layouts -from qmk.json_schema import deep_update, json_load, keyboard_validate, keyboard_api_validate +from qmk.json_schema import deep_update, json_load, validate from qmk.keyboard import config_h, rules_mk from qmk.keymap import list_keymaps from qmk.makefile import parse_rules_mk_file @@ -49,7 +49,7 @@ def info_json(keyboard): info_data['keymaps'][keymap.name] = {'url': f'https://raw.githubusercontent.com/qmk/qmk_firmware/master/{keymap}/keymap.json'} # Populate layout data - layouts, aliases = _find_all_layouts(info_data, keyboard) + layouts, aliases = _search_keyboard_h(keyboard) if aliases: info_data['layout_aliases'] = aliases @@ -61,12 +61,15 @@ def info_json(keyboard): # Merge in the data from info.json, config.h, and rules.mk info_data = merge_info_jsons(keyboard, info_data) - info_data = _extract_config_h(info_data) info_data = _extract_rules_mk(info_data) + info_data = _extract_config_h(info_data) + + # Ensure that we have matrix row and column counts + info_data = _matrix_size(info_data) # Validate against the jsonschema try: - keyboard_api_validate(info_data) + validate(info_data, 'qmk.api.keyboard.v1') except jsonschema.ValidationError as e: json_path = '.'.join([str(p) for p in e.absolute_path]) @@ -75,6 +78,9 @@ def info_json(keyboard): # Make sure we have at least one layout if not info_data.get('layouts'): + _find_missing_layouts(info_data, keyboard) + + if not info_data.get('layouts'): _log_error(info_data, 'No LAYOUTs defined! Need at least one layout defined in the keyboard.h or info.json.') # Filter out any non-existing community layouts @@ -90,6 +96,9 @@ def info_json(keyboard): if layout_name not in info_data.get('layouts', {}) and layout_name not in info_data.get('layout_aliases', {}): _log_error(info_data, 'Claims to support community layout %s but no %s() macro found' % (layout, layout_name)) + # Check that the reported matrix size is consistent with the actual matrix size + _check_matrix(info_data) + return info_data @@ -143,10 +152,7 @@ def _pin_name(pin): elif pin == 'NO_PIN': return None - elif pin[0] in 'ABCDEFGHIJK' and pin[1].isdigit(): - return pin - - raise ValueError(f'Invalid pin: {pin}') + return pin def _extract_pins(pins): @@ -155,10 +161,9 @@ def _extract_pins(pins): return [_pin_name(pin) for pin in pins.split(',')] -def _extract_direct_matrix(info_data, direct_pins): +def _extract_direct_matrix(direct_pins): """ """ - info_data['matrix_pins'] = {} direct_pin_array = [] while direct_pins[-1] != '}': @@ -182,12 +187,157 @@ def _extract_direct_matrix(info_data, direct_pins): return direct_pin_array +def _extract_audio(info_data, config_c): + """Populate data about the audio configuration + """ + audio_pins = [] + + for pin in 'B5', 'B6', 'B7', 'C4', 'C5', 'C6': + if config_c.get(f'{pin}_AUDIO'): + audio_pins.append(pin) + + if audio_pins: + info_data['audio'] = {'pins': audio_pins} + + +def _extract_split_main(info_data, config_c): + """Populate data about the split configuration + """ + # Figure out how the main half is determined + if config_c.get('SPLIT_HAND_PIN') is True: + if 'split' not in info_data: + info_data['split'] = {} + + if 'main' in info_data['split']: + _log_warning(info_data, 'Split main hand is specified in both config.h (SPLIT_HAND_PIN) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main']) + + info_data['split']['main'] = 'pin' + + if config_c.get('SPLIT_HAND_MATRIX_GRID'): + if 'split' not in info_data: + info_data['split'] = {} + + if 'main' in info_data['split']: + _log_warning(info_data, 'Split main hand is specified in both config.h (SPLIT_HAND_MATRIX_GRID) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main']) + + info_data['split']['main'] = 'matrix_grid' + info_data['split']['matrix_grid'] = _extract_pins(config_c['SPLIT_HAND_MATRIX_GRID']) + + if config_c.get('EE_HANDS') is True: + if 'split' not in info_data: + info_data['split'] = {} + + if 'main' in info_data['split']: + _log_warning(info_data, 'Split main hand is specified in both config.h (EE_HANDS) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main']) + + info_data['split']['main'] = 'eeprom' + + if config_c.get('MASTER_RIGHT') is True: + if 'split' not in info_data: + info_data['split'] = {} + + if 'main' in info_data['split']: + _log_warning(info_data, 'Split main hand is specified in both config.h (MASTER_RIGHT) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main']) + + info_data['split']['main'] = 'right' + + if config_c.get('MASTER_LEFT') is True: + if 'split' not in info_data: + info_data['split'] = {} + + if 'main' in info_data['split']: + _log_warning(info_data, 'Split main hand is specified in both config.h (MASTER_LEFT) and info.json (split.main) (Value: %s), the config.h value wins.' % info_data['split']['main']) + + info_data['split']['main'] = 'left' + + +def _extract_split_transport(info_data, config_c): + # Figure out the transport method + if config_c.get('USE_I2C') is True: + if 'split' not in info_data: + info_data['split'] = {} + + if 'transport' not in info_data['split']: + info_data['split']['transport'] = {} + + if 'protocol' in info_data['split']['transport']: + _log_warning(info_data, 'Split transport is specified in both config.h (USE_I2C) and info.json (split.transport.protocol) (Value: %s), the config.h value wins.' % info_data['split']['transport']) + + info_data['split']['transport']['protocol'] = 'i2c' + + elif 'protocol' not in info_data.get('split', {}).get('transport', {}): + if 'split' not in info_data: + info_data['split'] = {} + + if 'transport' not in info_data['split']: + info_data['split']['transport'] = {} + + info_data['split']['transport']['protocol'] = 'serial' + + +def _extract_split_right_pins(info_data, config_c): + # Figure out the right half matrix pins + row_pins = config_c.get('MATRIX_ROW_PINS_RIGHT', '').replace('{', '').replace('}', '').strip() + col_pins = config_c.get('MATRIX_COL_PINS_RIGHT', '').replace('{', '').replace('}', '').strip() + unused_pin_text = config_c.get('UNUSED_PINS_RIGHT') + unused_pins = unused_pin_text.replace('{', '').replace('}', '').strip() if isinstance(unused_pin_text, str) else None + direct_pins = config_c.get('DIRECT_PINS_RIGHT', '').replace(' ', '')[1:-1] + + if row_pins and col_pins: + if info_data.get('split', {}).get('matrix_pins', {}).get('right') in info_data: + _log_warning(info_data, 'Right hand matrix data is specified in both info.json and config.h, the config.h values win.') + + if 'split' not in info_data: + info_data['split'] = {} + + if 'matrix_pins' not in info_data['split']: + info_data['split']['matrix_pins'] = {} + + if 'right' not in info_data['split']['matrix_pins']: + info_data['split']['matrix_pins']['right'] = {} + + info_data['split']['matrix_pins']['right'] = { + 'cols': _extract_pins(col_pins), + 'rows': _extract_pins(row_pins), + } + + if direct_pins: + if info_data.get('split', {}).get('matrix_pins', {}).get('right', {}): + _log_warning(info_data, 'Right hand matrix data is specified in both info.json and config.h, the config.h values win.') + + if 'split' not in info_data: + info_data['split'] = {} + + if 'matrix_pins' not in info_data['split']: + info_data['split']['matrix_pins'] = {} + + if 'right' not in info_data['split']['matrix_pins']: + info_data['split']['matrix_pins']['right'] = {} + + info_data['split']['matrix_pins']['right']['direct'] = _extract_direct_matrix(direct_pins) + + if unused_pins: + if 'split' not in info_data: + info_data['split'] = {} + + if 'matrix_pins' not in info_data['split']: + info_data['split']['matrix_pins'] = {} + + if 'right' not in info_data['split']['matrix_pins']: + info_data['split']['matrix_pins']['right'] = {} + + info_data['split']['matrix_pins']['right']['unused'] = _extract_pins(unused_pins) + + def _extract_matrix_info(info_data, config_c): """Populate the matrix information. """ row_pins = config_c.get('MATRIX_ROW_PINS', '').replace('{', '').replace('}', '').strip() col_pins = config_c.get('MATRIX_COL_PINS', '').replace('{', '').replace('}', '').strip() + unused_pin_text = config_c.get('UNUSED_PINS') + unused_pins = unused_pin_text.replace('{', '').replace('}', '').strip() if isinstance(unused_pin_text, str) else None direct_pins = config_c.get('DIRECT_PINS', '').replace(' ', '')[1:-1] + info_snippet = {} if 'MATRIX_ROWS' in config_c and 'MATRIX_COLS' in config_c: if 'matrix_size' in info_data: @@ -199,19 +349,35 @@ def _extract_matrix_info(info_data, config_c): } if row_pins and col_pins: - if 'matrix_pins' in info_data: + if 'matrix_pins' in info_data and 'cols' in info_data['matrix_pins'] and 'rows' in info_data['matrix_pins']: _log_warning(info_data, 'Matrix pins are specified in both info.json and config.h, the config.h values win.') - info_data['matrix_pins'] = { - 'cols': _extract_pins(col_pins), - 'rows': _extract_pins(row_pins), - } + info_snippet['cols'] = _extract_pins(col_pins) + info_snippet['rows'] = _extract_pins(row_pins) if direct_pins: - if 'matrix_pins' in info_data: + if 'matrix_pins' in info_data and 'direct' in info_data['matrix_pins']: _log_warning(info_data, 'Direct pins are specified in both info.json and config.h, the config.h values win.') - info_data['matrix_pins']['direct'] = _extract_direct_matrix(info_data, direct_pins) + info_snippet['direct'] = _extract_direct_matrix(direct_pins) + + if unused_pins: + if 'matrix_pins' not in info_data: + info_data['matrix_pins'] = {} + + info_snippet['unused'] = _extract_pins(unused_pins) + + if config_c.get('CUSTOM_MATRIX', 'no') != 'no': + if 'matrix_pins' in info_data and 'custom' in info_data['matrix_pins']: + _log_warning(info_data, 'Custom Matrix is specified in both info.json and config.h, the config.h values win.') + + info_snippet['custom'] = True + + if config_c['CUSTOM_MATRIX'] == 'lite': + info_snippet['custom_lite'] = True + + if info_snippet: + info_data['matrix_pins'] = info_snippet return info_data @@ -269,6 +435,10 @@ def _extract_config_h(info_data): # Pull data that easily can't be mapped in json _extract_matrix_info(info_data, config_c) + _extract_audio(info_data, config_c) + _extract_split_main(info_data, config_c) + _extract_split_transport(info_data, config_c) + _extract_split_right_pins(info_data, config_c) return info_data @@ -341,6 +511,46 @@ def _extract_rules_mk(info_data): return info_data +def _matrix_size(info_data): + """Add info_data['matrix_size'] if it doesn't exist. + """ + if 'matrix_size' not in info_data and 'matrix_pins' in info_data: + info_data['matrix_size'] = {} + + if 'direct' in info_data['matrix_pins']: + info_data['matrix_size']['cols'] = len(info_data['matrix_pins']['direct'][0]) + info_data['matrix_size']['rows'] = len(info_data['matrix_pins']['direct']) + elif 'cols' in info_data['matrix_pins'] and 'rows' in info_data['matrix_pins']: + info_data['matrix_size']['cols'] = len(info_data['matrix_pins']['cols']) + info_data['matrix_size']['rows'] = len(info_data['matrix_pins']['rows']) + + return info_data + + +def _check_matrix(info_data): + """Check the matrix to ensure that row/column count is consistent. + """ + if 'matrix_pins' in info_data and 'matrix_size' in info_data: + actual_col_count = info_data['matrix_size'].get('cols', 0) + actual_row_count = info_data['matrix_size'].get('rows', 0) + col_count = row_count = 0 + + if 'direct' in info_data['matrix_pins']: + col_count = len(info_data['matrix_pins']['direct'][0]) + row_count = len(info_data['matrix_pins']['direct']) + elif 'cols' in info_data['matrix_pins'] and 'rows' in info_data['matrix_pins']: + col_count = len(info_data['matrix_pins']['cols']) + row_count = len(info_data['matrix_pins']['rows']) + + if col_count != actual_col_count and col_count != (actual_col_count / 2): + # FIXME: once we can we should detect if split is enabled to do the actual_col_count/2 check. + _log_error(info_data, f'MATRIX_COLS is inconsistent with the size of MATRIX_COL_PINS: {col_count} != {actual_col_count}') + + if row_count != actual_row_count and row_count != (actual_row_count / 2): + # FIXME: once we can we should detect if split is enabled to do the actual_row_count/2 check. + _log_error(info_data, f'MATRIX_ROWS is inconsistent with the size of MATRIX_ROW_PINS: {row_count} != {actual_row_count}') + + def _merge_layouts(info_data, new_info_data): """Merge new_info_data into info_data in an intelligent way. """ @@ -374,12 +584,13 @@ def _merge_layouts(info_data, new_info_data): return info_data -def _search_keyboard_h(path): +def _search_keyboard_h(keyboard): + keyboard = Path(keyboard) current_path = Path('keyboards/') aliases = {} layouts = {} - for directory in path.parts: + for directory in keyboard.parts: current_path = current_path / directory keyboard_h = '%s.h' % (directory,) keyboard_h_path = current_path / keyboard_h @@ -394,27 +605,28 @@ def _search_keyboard_h(path): return layouts, aliases -def _find_all_layouts(info_data, keyboard): - """Looks for layout macros associated with this keyboard. - """ - layouts, aliases = _search_keyboard_h(Path(keyboard)) +def _find_missing_layouts(info_data, keyboard): + """Looks for layout macros when they aren't found other places. - if not layouts: - # If we don't find any layouts from info.json or keyboard.h we widen our search. This is error prone which is why we want to encourage people to follow the standard above. - info_data['parse_warnings'].append('%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard)) + If we don't find any layouts from info.json or keyboard.h we widen our search. This is error prone which is why we want to encourage people to follow the standard above. + """ + _log_warning(info_data, '%s: Falling back to searching for KEYMAP/LAYOUT macros.' % (keyboard)) - for file in glob('keyboards/%s/*.h' % keyboard): - if file.endswith('.h'): - these_layouts, these_aliases = find_layouts(file) + for file in glob('keyboards/%s/*.h' % keyboard): + these_layouts, these_aliases = find_layouts(file) - if these_layouts: - layouts.update(these_layouts) + if these_layouts: + for layout_name, layout_json in these_layouts.items(): + if not layout_name.startswith('LAYOUT_kc'): + layout_json['c_macro'] = True + info_data['layouts'][layout_name] = layout_json - for alias, alias_text in these_aliases.items(): - if alias_text in layouts: - aliases[alias] = alias_text + for alias, alias_text in these_aliases.items(): + if alias_text in these_layouts: + if 'layout_aliases' not in info_data: + info_data['layout_aliases'] = {} - return layouts, aliases + info_data['layout_aliases'][alias] = alias_text def _log_error(info_data, message): @@ -493,7 +705,7 @@ def merge_info_jsons(keyboard, info_data): continue try: - keyboard_validate(new_info_data) + validate(new_info_data, 'qmk.keyboard.v1') except jsonschema.ValidationError as e: json_path = '.'.join([str(p) for p in e.absolute_path]) cli.log.error('Not including data from file: %s', info_file) |