You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.0 KiB

  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.decorators import automagic_keyboard, automagic_keymap
  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.path import is_keyboard, normpath
  11. def direct_pins(direct_pins):
  12. """Return the config.h lines that set the direct pins.
  13. """
  14. rows = []
  15. for row in direct_pins:
  16. cols = ','.join(map(str, [col or 'NO_PIN' for col in row]))
  17. rows.append('{' + cols + '}')
  18. col_count = len(direct_pins[0])
  19. row_count = len(direct_pins)
  20. return """
  21. #ifndef MATRIX_COLS
  22. # define MATRIX_COLS %s
  23. #endif // MATRIX_COLS
  24. #ifndef MATRIX_ROWS
  25. # define MATRIX_ROWS %s
  26. #endif // MATRIX_ROWS
  27. #ifndef DIRECT_PINS
  28. # define DIRECT_PINS {%s}
  29. #endif // DIRECT_PINS
  30. """ % (col_count, row_count, ','.join(rows))
  31. def pin_array(define, pins):
  32. """Return the config.h lines that set a pin array.
  33. """
  34. pin_num = len(pins)
  35. pin_array = ', '.join(map(str, [pin or 'NO_PIN' for pin in pins]))
  36. return f"""
  37. #ifndef {define}S
  38. # define {define}S {pin_num}
  39. #endif // {define}S
  40. #ifndef {define}_PINS
  41. # define {define}_PINS {{ {pin_array} }}
  42. #endif // {define}_PINS
  43. """
  44. def matrix_pins(matrix_pins):
  45. """Add the matrix config to the config.h.
  46. """
  47. pins = []
  48. if 'direct' in matrix_pins:
  49. pins.append(direct_pins(matrix_pins['direct']))
  50. if 'cols' in matrix_pins:
  51. pins.append(pin_array('MATRIX_COL', matrix_pins['cols']))
  52. if 'rows' in matrix_pins:
  53. pins.append(pin_array('MATRIX_ROW', matrix_pins['rows']))
  54. return '\n'.join(pins)
  55. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  56. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  57. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate config.h for.')
  58. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  59. @automagic_keyboard
  60. @automagic_keymap
  61. def generate_config_h(cli):
  62. """Generates the info_config.h file.
  63. """
  64. # Determine our keyboard(s)
  65. if not cli.config.generate_config_h.keyboard:
  66. cli.log.error('Missing parameter: --keyboard')
  67. cli.subcommands['info'].print_help()
  68. return False
  69. if not is_keyboard(cli.config.generate_config_h.keyboard):
  70. cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard)
  71. return False
  72. # Build the info_config.h file.
  73. kb_info_json = dotty(info_json(cli.config.generate_config_h.keyboard))
  74. info_config_map = json_load(Path('data/mappings/info_config.json'))
  75. config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once']
  76. # Iterate through the info_config map to generate basic things
  77. for config_key, info_dict in info_config_map.items():
  78. info_key = info_dict['info_key']
  79. key_type = info_dict.get('value_type', 'str')
  80. to_config = info_dict.get('to_config', True)
  81. if not to_config:
  82. continue
  83. try:
  84. config_value = kb_info_json[info_key]
  85. except KeyError:
  86. continue
  87. if key_type.startswith('array'):
  88. config_h_lines.append('')
  89. config_h_lines.append(f'#ifndef {config_key}')
  90. config_h_lines.append(f'# define {config_key} {{ {", ".join(map(str, config_value))} }}')
  91. config_h_lines.append(f'#endif // {config_key}')
  92. elif key_type == 'bool':
  93. if config_value:
  94. config_h_lines.append('')
  95. config_h_lines.append(f'#ifndef {config_key}')
  96. config_h_lines.append(f'# define {config_key}')
  97. config_h_lines.append(f'#endif // {config_key}')
  98. elif key_type == 'mapping':
  99. for key, value in config_value.items():
  100. config_h_lines.append('')
  101. config_h_lines.append(f'#ifndef {key}')
  102. config_h_lines.append(f'# define {key} {value}')
  103. config_h_lines.append(f'#endif // {key}')
  104. else:
  105. config_h_lines.append('')
  106. config_h_lines.append(f'#ifndef {config_key}')
  107. config_h_lines.append(f'# define {config_key} {config_value}')
  108. config_h_lines.append(f'#endif // {config_key}')
  109. if 'matrix_pins' in kb_info_json:
  110. config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))
  111. # Show the results
  112. config_h = '\n'.join(config_h_lines)
  113. if cli.args.output:
  114. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  115. if cli.args.output.exists():
  116. cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
  117. cli.args.output.write_text(config_h)
  118. if not cli.args.quiet:
  119. cli.log.info('Wrote info_config.h to %s.', cli.args.output)
  120. else:
  121. print(config_h)