config_h.py 7.9 KB

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