config_h.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 argcomplete.completers import FilesCompleter
  6. from milc import cli
  7. from qmk.info import info_json
  8. from qmk.json_schema import json_load
  9. from qmk.keyboard import keyboard_completer, keyboard_folder
  10. from qmk.commands import dump_lines, parse_configurator_json
  11. from qmk.path import normpath, FileType
  12. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  13. def generate_define(define, value=None):
  14. value = f' {value}' if value is not None else ''
  15. return f"""
  16. #ifndef {define}
  17. # define {define}{value}
  18. #endif // {define}"""
  19. def direct_pins(direct_pins, postfix):
  20. """Return the config.h lines that set the direct pins.
  21. """
  22. rows = []
  23. for row in direct_pins:
  24. cols = ','.join(map(str, [col or 'NO_PIN' for col in row]))
  25. rows.append('{' + cols + '}')
  26. return generate_define(f'DIRECT_PINS{postfix}', f'{{ {", ".join(rows)} }}')
  27. def pin_array(define, pins, postfix):
  28. """Return the config.h lines that set a pin array.
  29. """
  30. pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins]))
  31. return generate_define(f'{define}_PINS{postfix}', f'{{ {pin_array} }}')
  32. def matrix_pins(matrix_pins, postfix=''):
  33. """Add the matrix config to the config.h.
  34. """
  35. pins = []
  36. if 'direct' in matrix_pins:
  37. pins.append(direct_pins(matrix_pins['direct'], postfix))
  38. if 'cols' in matrix_pins:
  39. pins.append(pin_array('MATRIX_COL', matrix_pins['cols'], postfix))
  40. if 'rows' in matrix_pins:
  41. pins.append(pin_array('MATRIX_ROW', matrix_pins['rows'], postfix))
  42. return '\n'.join(pins)
  43. def generate_matrix_size(kb_info_json, config_h_lines):
  44. """Add the matrix size to the config.h.
  45. """
  46. if 'matrix_pins' in kb_info_json:
  47. config_h_lines.append(generate_define('MATRIX_COLS', kb_info_json['matrix_size']['cols']))
  48. config_h_lines.append(generate_define('MATRIX_ROWS', kb_info_json['matrix_size']['rows']))
  49. def generate_config_items(kb_info_json, config_h_lines):
  50. """Iterate through the info_config map to generate basic config values.
  51. """
  52. info_config_map = json_load(Path('data/mappings/info_config.hjson'))
  53. for config_key, info_dict in info_config_map.items():
  54. info_key = info_dict['info_key']
  55. key_type = info_dict.get('value_type', 'raw')
  56. to_config = info_dict.get('to_config', True)
  57. if not to_config:
  58. continue
  59. try:
  60. config_value = kb_info_json[info_key]
  61. except KeyError:
  62. continue
  63. if key_type.startswith('array.array'):
  64. config_h_lines.append(generate_define(config_key, f'{{ {", ".join(["{" + ",".join(list(map(str, x))) + "}" for x in config_value])} }}'))
  65. elif key_type.startswith('array'):
  66. config_h_lines.append(generate_define(config_key, f'{{ {", ".join(map(str, config_value))} }}'))
  67. elif key_type == 'bool':
  68. if config_value:
  69. config_h_lines.append(generate_define(config_key))
  70. elif key_type == 'mapping':
  71. for key, value in config_value.items():
  72. config_h_lines.append(generate_define(key, value))
  73. elif key_type == 'str':
  74. escaped_str = config_value.replace('\\', '\\\\').replace('"', '\\"')
  75. config_h_lines.append(generate_define(config_key, f'"{escaped_str}"'))
  76. elif key_type == 'bcd_version':
  77. (major, minor, revision) = config_value.split('.')
  78. config_h_lines.append(generate_define(config_key, f'0x{major.zfill(2)}{minor}{revision}'))
  79. else:
  80. config_h_lines.append(generate_define(config_key, config_value))
  81. def generate_encoder_config(encoder_json, config_h_lines, postfix=''):
  82. """Generate the config.h lines for encoders."""
  83. a_pads = []
  84. b_pads = []
  85. resolutions = []
  86. for encoder in encoder_json.get("rotary", []):
  87. a_pads.append(encoder["pin_a"])
  88. b_pads.append(encoder["pin_b"])
  89. resolutions.append(encoder.get("resolution", None))
  90. config_h_lines.append(generate_define(f'ENCODERS_PAD_A{postfix}', f'{{ {", ".join(a_pads)} }}'))
  91. config_h_lines.append(generate_define(f'ENCODERS_PAD_B{postfix}', f'{{ {", ".join(b_pads)} }}'))
  92. if None in resolutions:
  93. cli.log.debug("Unable to generate ENCODER_RESOLUTION configuration")
  94. elif len(set(resolutions)) == 1:
  95. config_h_lines.append(generate_define(f'ENCODER_RESOLUTION{postfix}', resolutions[0]))
  96. else:
  97. config_h_lines.append(generate_define(f'ENCODER_RESOLUTIONS{postfix}', f'{{ {", ".join(map(str,resolutions))} }}'))
  98. def generate_pointing_device_config(pointing_device_json, config_h_lines, postfix=''):
  99. rotation = pointing_device_json.get('rotation', 0)
  100. generate_define(f'POINTING_DEVICE_ROTATION_{rotation}{postfix}')
  101. def generate_split_config(kb_info_json, config_h_lines):
  102. """Generate the config.h lines for split boards."""
  103. if 'primary' in kb_info_json['split']:
  104. if kb_info_json['split']['primary'] in ('left', 'right'):
  105. config_h_lines.append('')
  106. config_h_lines.append('#ifndef MASTER_LEFT')
  107. config_h_lines.append('# ifndef MASTER_RIGHT')
  108. if kb_info_json['split']['primary'] == 'left':
  109. config_h_lines.append('# define MASTER_LEFT')
  110. elif kb_info_json['split']['primary'] == 'right':
  111. config_h_lines.append('# define MASTER_RIGHT')
  112. config_h_lines.append('# endif // MASTER_RIGHT')
  113. config_h_lines.append('#endif // MASTER_LEFT')
  114. elif kb_info_json['split']['primary'] == 'pin':
  115. config_h_lines.append(generate_define('SPLIT_HAND_PIN'))
  116. elif kb_info_json['split']['primary'] == 'matrix_grid':
  117. config_h_lines.append(generate_define('SPLIT_HAND_MATRIX_GRID', f'{{ {",".join(kb_info_json["split"]["matrix_grid"])} }}'))
  118. elif kb_info_json['split']['primary'] == 'eeprom':
  119. config_h_lines.append(generate_define('EE_HANDS'))
  120. if 'protocol' in kb_info_json['split'].get('transport', {}):
  121. if kb_info_json['split']['transport']['protocol'] == 'i2c':
  122. config_h_lines.append(generate_define('USE_I2C'))
  123. if 'right' in kb_info_json['split'].get('matrix_pins', {}):
  124. config_h_lines.append(matrix_pins(kb_info_json['split']['matrix_pins']['right'], '_RIGHT'))
  125. if 'right' in kb_info_json['split'].get('encoder', {}):
  126. generate_encoder_config(kb_info_json['split']['encoder']['right'], config_h_lines, '_RIGHT')
  127. if 'right' in kb_info_json['split'].get('pointing_device', {}):
  128. generate_pointing_device_config(kb_info_json['split']['pointing_device']['right'], config_h_lines, '_RIGHT')
  129. def generate_led_animations_config(led_feature_json, config_h_lines, prefix):
  130. for animation in led_feature_json.get('animations', {}):
  131. if led_feature_json['animations'][animation]:
  132. config_h_lines.append(generate_define(f'{prefix}{animation.upper()}'))
  133. @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON to be compiled and flashed or a pre-compiled binary firmware file (bin/hex) to be flashed.')
  134. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  135. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  136. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
  137. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  138. def generate_config_h(cli):
  139. """Generates the info_config.h file.
  140. """
  141. # Determine our keyboard/keymap
  142. if cli.args.filename:
  143. user_keymap = parse_configurator_json(cli.args.filename)
  144. kb_info_json = dotty(user_keymap.get('config', {}))
  145. elif cli.args.keyboard:
  146. kb_info_json = dotty(info_json(cli.args.keyboard))
  147. else:
  148. cli.log.error('You must supply a configurator export or `--keyboard`.')
  149. cli.subcommands['generate-config-h'].print_help()
  150. return False
  151. # Build the info_config.h file.
  152. config_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
  153. generate_config_items(kb_info_json, config_h_lines)
  154. generate_matrix_size(kb_info_json, config_h_lines)
  155. if 'matrix_pins' in kb_info_json:
  156. config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))
  157. if 'encoder' in kb_info_json:
  158. generate_encoder_config(kb_info_json['encoder'], config_h_lines)
  159. if 'split' in kb_info_json:
  160. generate_split_config(kb_info_json, config_h_lines)
  161. if 'led_matrix' in kb_info_json:
  162. generate_led_animations_config(kb_info_json['led_matrix'], config_h_lines, 'ENABLE_LED_MATRIX_')
  163. if 'rgb_matrix' in kb_info_json:
  164. generate_led_animations_config(kb_info_json['rgb_matrix'], config_h_lines, 'ENABLE_RGB_MATRIX_')
  165. if 'rgblight' in kb_info_json:
  166. generate_led_animations_config(kb_info_json['rgblight'], config_h_lines, 'RGBLIGHT_EFFECT_')
  167. if 'pointing_device' in kb_info_json:
  168. generate_pointing_device_config(kb_info_json['pointing_device'], config_h_lines)
  169. # Show the results
  170. dump_lines(cli.args.output, config_h_lines, cli.args.quiet)