2020-12-30 18:27:37 +00:00
|
|
|
"""Used by the make system to generate info_config.h from info.json.
|
|
|
|
"""
|
2021-01-31 20:46:00 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from dotty_dict import dotty
|
2020-12-30 18:27:37 +00:00
|
|
|
from milc import cli
|
|
|
|
|
|
|
|
from qmk.decorators import automagic_keyboard, automagic_keymap
|
2021-03-24 16:26:38 +00:00
|
|
|
from qmk.info import info_json
|
|
|
|
from qmk.json_schema import json_load
|
|
|
|
from qmk.keyboard import keyboard_folder
|
2020-12-30 18:27:37 +00:00
|
|
|
from qmk.path import is_keyboard, normpath
|
|
|
|
|
|
|
|
|
|
|
|
def direct_pins(direct_pins):
|
|
|
|
"""Return the config.h lines that set the direct pins.
|
|
|
|
"""
|
|
|
|
rows = []
|
|
|
|
|
|
|
|
for row in direct_pins:
|
2021-01-08 08:00:15 +00:00
|
|
|
cols = ','.join(map(str, [col or 'NO_PIN' for col in row]))
|
2020-12-30 18:27:37 +00:00
|
|
|
rows.append('{' + cols + '}')
|
|
|
|
|
|
|
|
col_count = len(direct_pins[0])
|
|
|
|
row_count = len(direct_pins)
|
|
|
|
|
|
|
|
return """
|
|
|
|
#ifndef MATRIX_COLS
|
2021-01-31 20:46:00 +00:00
|
|
|
# define MATRIX_COLS %s
|
2020-12-30 18:27:37 +00:00
|
|
|
#endif // MATRIX_COLS
|
|
|
|
|
|
|
|
#ifndef MATRIX_ROWS
|
2021-01-31 20:46:00 +00:00
|
|
|
# define MATRIX_ROWS %s
|
2020-12-30 18:27:37 +00:00
|
|
|
#endif // MATRIX_ROWS
|
|
|
|
|
|
|
|
#ifndef DIRECT_PINS
|
2021-01-31 20:46:00 +00:00
|
|
|
# define DIRECT_PINS {%s}
|
2020-12-30 18:27:37 +00:00
|
|
|
#endif // DIRECT_PINS
|
|
|
|
""" % (col_count, row_count, ','.join(rows))
|
|
|
|
|
|
|
|
|
2021-01-31 20:46:00 +00:00
|
|
|
def pin_array(define, pins):
|
|
|
|
"""Return the config.h lines that set a pin array.
|
2020-12-30 18:27:37 +00:00
|
|
|
"""
|
2021-01-31 20:46:00 +00:00
|
|
|
pin_num = len(pins)
|
|
|
|
pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins]))
|
2020-12-30 18:27:37 +00:00
|
|
|
|
2021-01-31 20:46:00 +00:00
|
|
|
return f"""
|
|
|
|
#ifndef {define}S
|
|
|
|
# define {define}S {pin_num}
|
|
|
|
#endif // {define}S
|
2020-12-30 18:27:37 +00:00
|
|
|
|
2021-01-31 20:46:00 +00:00
|
|
|
#ifndef {define}_PINS
|
|
|
|
# define {define}_PINS {{ {pin_array} }}
|
|
|
|
#endif // {define}_PINS
|
|
|
|
"""
|
2020-12-30 18:27:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def matrix_pins(matrix_pins):
|
|
|
|
"""Add the matrix config to the config.h.
|
|
|
|
"""
|
|
|
|
pins = []
|
|
|
|
|
|
|
|
if 'direct' in matrix_pins:
|
|
|
|
pins.append(direct_pins(matrix_pins['direct']))
|
|
|
|
|
|
|
|
if 'cols' in matrix_pins:
|
2021-01-31 20:46:00 +00:00
|
|
|
pins.append(pin_array('MATRIX_COL', matrix_pins['cols']))
|
2020-12-30 18:27:37 +00:00
|
|
|
|
|
|
|
if 'rows' in matrix_pins:
|
2021-01-31 20:46:00 +00:00
|
|
|
pins.append(pin_array('MATRIX_ROW', matrix_pins['rows']))
|
2020-12-30 18:27:37 +00:00
|
|
|
|
|
|
|
return '\n'.join(pins)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
|
|
|
|
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
|
2021-03-24 16:26:38 +00:00
|
|
|
@cli.argument('-kb', '--keyboard', type=keyboard_folder, help='Keyboard to generate config.h for.')
|
2020-12-30 18:27:37 +00:00
|
|
|
@cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
|
|
|
|
@automagic_keyboard
|
|
|
|
@automagic_keymap
|
|
|
|
def generate_config_h(cli):
|
|
|
|
"""Generates the info_config.h file.
|
|
|
|
"""
|
|
|
|
# Determine our keyboard(s)
|
|
|
|
if not cli.config.generate_config_h.keyboard:
|
2021-02-28 20:19:07 +00:00
|
|
|
cli.log.error('Missing parameter: --keyboard')
|
2020-12-30 18:27:37 +00:00
|
|
|
cli.subcommands['info'].print_help()
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not is_keyboard(cli.config.generate_config_h.keyboard):
|
|
|
|
cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Build the info_config.h file.
|
2021-01-31 20:46:00 +00:00
|
|
|
kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard))
|
2021-03-24 16:26:38 +00:00
|
|
|
info_config_map = json_load(Path('data/mappings/info_config.json'))
|
2020-12-30 18:27:37 +00:00
|
|
|
|
2021-01-31 20:46:00 +00:00
|
|
|
config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once']
|
2020-12-30 18:27:37 +00:00
|
|
|
|
2021-01-31 20:46:00 +00:00
|
|
|
# Iterate through the info_config map to generate basic things
|
|
|
|
for config_key, info_dict in info_config_map.items():
|
|
|
|
info_key = info_dict['info_key']
|
|
|
|
key_type = info_dict.get('value_type', 'str')
|
|
|
|
to_config = info_dict.get('to_config', True)
|
|
|
|
|
|
|
|
if not to_config:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
config_value = kb_info_json[info_key]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if key_type.startswith('array'):
|
|
|
|
config_h_lines.append('')
|
|
|
|
config_h_lines.append(f'#ifndef {config_key}')
|
|
|
|
config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}')
|
|
|
|
config_h_lines.append(f'#endif // {config_key}')
|
|
|
|
elif key_type == 'bool':
|
|
|
|
if config_value:
|
|
|
|
config_h_lines.append('')
|
|
|
|
config_h_lines.append(f'#ifndef {config_key}')
|
|
|
|
config_h_lines.append(f'# define {config_key}')
|
|
|
|
config_h_lines.append(f'#endif // {config_key}')
|
|
|
|
elif key_type == 'mapping':
|
|
|
|
for key, value in config_value.items():
|
|
|
|
config_h_lines.append('')
|
|
|
|
config_h_lines.append(f'#ifndef {key}')
|
|
|
|
config_h_lines.append(f'# define {key} {value}')
|
|
|
|
config_h_lines.append(f'#endif // {key}')
|
|
|
|
else:
|
|
|
|
config_h_lines.append('')
|
|
|
|
config_h_lines.append(f'#ifndef {config_key}')
|
|
|
|
config_h_lines.append(f'# define {config_key} {config_value}')
|
|
|
|
config_h_lines.append(f'#endif // {config_key}')
|
2020-12-30 18:27:37 +00:00
|
|
|
|
|
|
|
if 'matrix_pins' in kb_info_json:
|
|
|
|
config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))
|
|
|
|
|
|
|
|
# Show the results
|
|
|
|
config_h = '\n'.join(config_h_lines)
|
|
|
|
|
|
|
|
if cli.args.output:
|
|
|
|
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if cli.args.output.exists():
|
2021-02-28 20:19:07 +00:00
|
|
|
cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
|
2020-12-30 18:27:37 +00:00
|
|
|
cli.args.output.write_text(config_h)
|
|
|
|
|
|
|
|
if not cli.args.quiet:
|
|
|
|
cli.log.info('Wrote info_config.h to %s.', cli.args.output)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print(config_h)
|