config_h.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """Used by the make system to generate info_config.h from info.json.
  2. """
  3. from pathlib import Path
  4. from dotty_dict import dotty
  5. from milc import cli
  6. from qmk.info import info_json, get_keyboard_overrides
  7. from qmk.json_schema import json_load
  8. from qmk.keyboard import keyboard_completer, keyboard_folder
  9. from qmk.path import normpath
  10. def direct_pins(direct_pins, postfix):
  11. """Return the config.h lines that set the direct pins.
  12. """
  13. rows = []
  14. for row in direct_pins:
  15. cols = ','.join(map(str, [col or 'NO_PIN' for col in row]))
  16. rows.append('{' + cols + '}')
  17. col_count = len(direct_pins[0])
  18. row_count = len(direct_pins)
  19. return f"""
  20. #ifndef MATRIX_COLS{postfix}
  21. # define MATRIX_COLS{postfix} {col_count}
  22. #endif // MATRIX_COLS{postfix}
  23. #ifndef MATRIX_ROWS{postfix}
  24. # define MATRIX_ROWS{postfix} {row_count}
  25. #endif // MATRIX_ROWS{postfix}
  26. #ifndef DIRECT_PINS{postfix}
  27. # define DIRECT_PINS{postfix} {{ {", ".join(rows)} }}
  28. #endif // DIRECT_PINS{postfix}
  29. """
  30. def pin_array(define, pins, postfix):
  31. """Return the config.h lines that set a pin array.
  32. """
  33. pin_num = len(pins)
  34. pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins]))
  35. return f"""
  36. #ifndef {define}S{postfix}
  37. # define {define}S{postfix} {pin_num}
  38. #endif // {define}S{postfix}
  39. #ifndef {define}_PINS{postfix}
  40. # define {define}_PINS{postfix} {{ {pin_array} }}
  41. #endif // {define}_PINS{postfix}
  42. """
  43. def matrix_pins(matrix_pins, postfix=''):
  44. """Add the matrix config to the config.h.
  45. """
  46. pins = []
  47. if 'direct' in matrix_pins:
  48. pins.append(direct_pins(matrix_pins['direct'], postfix))
  49. if 'cols' in matrix_pins:
  50. pins.append(pin_array('MATRIX_COL', matrix_pins['cols'], postfix))
  51. if 'rows' in matrix_pins:
  52. pins.append(pin_array('MATRIX_ROW', matrix_pins['rows'], postfix))
  53. return '\n'.join(pins)
  54. def generate_config_items(kb_info_json, config_h_lines):
  55. """Iterate through the info_config map to generate basic config values.
  56. """
  57. info_config_map = json_load(Path('data/mappings/info_config.json'))
  58. for config_key, info_dict in info_config_map.items():
  59. info_key = info_dict['info_key']
  60. key_type = info_dict.get('value_type', 'str')
  61. to_config = info_dict.get('to_config', True)
  62. if not to_config:
  63. continue
  64. try:
  65. config_value = kb_info_json[info_key]
  66. except KeyError:
  67. continue
  68. if key_type.startswith('array'):
  69. config_h_lines.append('')
  70. config_h_lines.append(f'#ifndef {config_key}')
  71. config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}')
  72. config_h_lines.append(f'#endif // {config_key}')
  73. elif key_type == 'bool':
  74. if config_value:
  75. config_h_lines.append('')
  76. config_h_lines.append(f'#ifndef {config_key}')
  77. config_h_lines.append(f'# define {config_key}')
  78. config_h_lines.append(f'#endif // {config_key}')
  79. elif key_type == 'mapping':
  80. for key, value in config_value.items():
  81. config_h_lines.append('')
  82. config_h_lines.append(f'#ifndef {key}')
  83. config_h_lines.append(f'# define {key} {value}')
  84. config_h_lines.append(f'#endif // {key}')
  85. else:
  86. config_h_lines.append('')
  87. config_h_lines.append(f'#ifndef {config_key}')
  88. config_h_lines.append(f'# define {config_key} {config_value}')
  89. config_h_lines.append(f'#endif // {config_key}')
  90. def generate_split_config(kb_info_json, config_h_lines):
  91. """Generate the config.h lines for split boards."""
  92. if 'primary' in kb_info_json['split']:
  93. if kb_info_json['split']['primary'] in ('left', 'right'):
  94. config_h_lines.append('')
  95. config_h_lines.append('#ifndef MASTER_LEFT')
  96. config_h_lines.append('# ifndef MASTER_RIGHT')
  97. if kb_info_json['split']['primary'] == 'left':
  98. config_h_lines.append('# define MASTER_LEFT')
  99. elif kb_info_json['split']['primary'] == 'right':
  100. config_h_lines.append('# define MASTER_RIGHT')
  101. config_h_lines.append('# endif // MASTER_RIGHT')
  102. config_h_lines.append('#endif // MASTER_LEFT')
  103. elif kb_info_json['split']['primary'] == 'pin':
  104. config_h_lines.append('')
  105. config_h_lines.append('#ifndef SPLIT_HAND_PIN')
  106. config_h_lines.append('# define SPLIT_HAND_PIN')
  107. config_h_lines.append('#endif // SPLIT_HAND_PIN')
  108. elif kb_info_json['split']['primary'] == 'matrix_grid':
  109. config_h_lines.append('')
  110. config_h_lines.append('#ifndef SPLIT_HAND_MATRIX_GRID')
  111. config_h_lines.append('# define SPLIT_HAND_MATRIX_GRID {%s}' % (','.join(kb_info_json["split"]["matrix_grid"],)))
  112. config_h_lines.append('#endif // SPLIT_HAND_MATRIX_GRID')
  113. elif kb_info_json['split']['primary'] == 'eeprom':
  114. config_h_lines.append('')
  115. config_h_lines.append('#ifndef EE_HANDS')
  116. config_h_lines.append('# define EE_HANDS')
  117. config_h_lines.append('#endif // EE_HANDS')
  118. if 'protocol' in kb_info_json['split'].get('transport', {}):
  119. if kb_info_json['split']['transport']['protocol'] == 'i2c':
  120. config_h_lines.append('')
  121. config_h_lines.append('#ifndef USE_I2C')
  122. config_h_lines.append('# define USE_I2C')
  123. config_h_lines.append('#endif // USE_I2C')
  124. if 'right' in kb_info_json['split'].get('matrix_pins', {}):
  125. config_h_lines.append(matrix_pins(kb_info_json['split']['matrix_pins']['right'], '_RIGHT'))
  126. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  127. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  128. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate config.h for.')
  129. @cli.argument('-km', '--keymap', arg_only=True, help='Keymap to get overrides from.')
  130. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  131. def generate_config_h(cli):
  132. """Generates the info_config.h file.
  133. """
  134. kb_info_json = dotty(info_json(cli.args.keyboard, overrides=get_keyboard_overrides(cli.args.keyboard, cli.args.keymap)))
  135. # Build the info_config.h file.
  136. config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once']
  137. generate_config_items(kb_info_json, config_h_lines)
  138. if 'matrix_pins' in kb_info_json:
  139. config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))
  140. if 'split' in kb_info_json:
  141. generate_split_config(kb_info_json, config_h_lines)
  142. # Show the results
  143. config_h = '\n'.join(config_h_lines)
  144. if cli.args.output:
  145. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  146. if cli.args.output.exists():
  147. cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
  148. cli.args.output.write_text(config_h)
  149. if not cli.args.quiet:
  150. cli.log.info('Wrote info_config.h to %s.', cli.args.output)
  151. else:
  152. print(config_h)