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.

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