config_h.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. """Used by the make system to generate info_config.h from info.json.
  2. """
  3. from milc import cli
  4. from qmk.constants import LED_INDICATORS
  5. from qmk.decorators import automagic_keyboard, automagic_keymap
  6. from qmk.info import info_json, rgblight_animations, rgblight_properties, rgblight_toggles
  7. from qmk.path import is_keyboard, normpath
  8. usb_prop_map = {
  9. 'vid': 'VENDOR_ID',
  10. 'pid': 'PRODUCT_ID',
  11. 'device_ver': 'DEVICE_VER',
  12. }
  13. def debounce(debounce):
  14. """Return the config.h lines that set debounce
  15. """
  16. return """
  17. #ifndef DEBOUNCE
  18. # define DEBOUNCE %s
  19. #endif // DEBOUNCE
  20. """ % debounce
  21. def diode_direction(diode_direction):
  22. """Return the config.h lines that set diode direction
  23. """
  24. return """
  25. #ifndef DIODE_DIRECTION
  26. # define DIODE_DIRECTION %s
  27. #endif // DIODE_DIRECTION
  28. """ % diode_direction
  29. def keyboard_name(keyboard_name):
  30. """Return the config.h lines that set the keyboard's name.
  31. """
  32. return """
  33. #ifndef DESCRIPTION
  34. # define DESCRIPTION %s
  35. #endif // DESCRIPTION
  36. #ifndef PRODUCT
  37. # define PRODUCT %s
  38. #endif // PRODUCT
  39. """ % (keyboard_name, keyboard_name)
  40. def manufacturer(manufacturer):
  41. """Return the config.h lines that set the manufacturer.
  42. """
  43. return """
  44. #ifndef MANUFACTURER
  45. # define MANUFACTURER %s
  46. #endif // MANUFACTURER
  47. """ % (manufacturer)
  48. def direct_pins(direct_pins):
  49. """Return the config.h lines that set the direct pins.
  50. """
  51. rows = []
  52. for row in direct_pins:
  53. cols = ','.join([col or 'NO_PIN' for col in row])
  54. rows.append('{' + cols + '}')
  55. col_count = len(direct_pins[0])
  56. row_count = len(direct_pins)
  57. return """
  58. #ifndef MATRIX_COLS
  59. # define MATRIX_COLS %s
  60. #endif // MATRIX_COLS
  61. #ifndef MATRIX_ROWS
  62. # define MATRIX_ROWS %s
  63. #endif // MATRIX_ROWS
  64. #ifndef DIRECT_PINS
  65. # define DIRECT_PINS {%s}
  66. #endif // DIRECT_PINS
  67. """ % (col_count, row_count, ','.join(rows))
  68. def col_pins(col_pins):
  69. """Return the config.h lines that set the column pins.
  70. """
  71. cols = ','.join(col_pins)
  72. col_num = len(col_pins)
  73. return """
  74. #ifndef MATRIX_COLS
  75. # define MATRIX_COLS %s
  76. #endif // MATRIX_COLS
  77. #ifndef MATRIX_COL_PINS
  78. # define MATRIX_COL_PINS {%s}
  79. #endif // MATRIX_COL_PINS
  80. """ % (col_num, cols)
  81. def row_pins(row_pins):
  82. """Return the config.h lines that set the row pins.
  83. """
  84. rows = ','.join(row_pins)
  85. row_num = len(row_pins)
  86. return """
  87. #ifndef MATRIX_ROWS
  88. # define MATRIX_ROWS %s
  89. #endif // MATRIX_ROWS
  90. #ifndef MATRIX_ROW_PINS
  91. # define MATRIX_ROW_PINS {%s}
  92. #endif // MATRIX_ROW_PINS
  93. """ % (row_num, rows)
  94. def indicators(config):
  95. """Return the config.h lines that setup LED indicators.
  96. """
  97. defines = []
  98. for led, define in LED_INDICATORS.items():
  99. if led in config:
  100. defines.append('')
  101. defines.append('#ifndef %s' % (define,))
  102. defines.append('# define %s %s' % (define, config[led]))
  103. defines.append('#endif // %s' % (define,))
  104. return '\n'.join(defines)
  105. def layout_aliases(layout_aliases):
  106. """Return the config.h lines that setup layout aliases.
  107. """
  108. defines = []
  109. for alias, layout in layout_aliases.items():
  110. defines.append('')
  111. defines.append('#ifndef %s' % (alias,))
  112. defines.append('# define %s %s' % (alias, layout))
  113. defines.append('#endif // %s' % (alias,))
  114. return '\n'.join(defines)
  115. def matrix_pins(matrix_pins):
  116. """Add the matrix config to the config.h.
  117. """
  118. pins = []
  119. if 'direct' in matrix_pins:
  120. pins.append(direct_pins(matrix_pins['direct']))
  121. if 'cols' in matrix_pins:
  122. pins.append(col_pins(matrix_pins['cols']))
  123. if 'rows' in matrix_pins:
  124. pins.append(row_pins(matrix_pins['rows']))
  125. return '\n'.join(pins)
  126. def rgblight(config):
  127. """Return the config.h lines that setup rgblight.
  128. """
  129. rgblight_config = []
  130. for json_key, config_key in rgblight_properties.items():
  131. if json_key in config:
  132. rgblight_config.append('')
  133. rgblight_config.append('#ifndef %s' % (config_key,))
  134. rgblight_config.append('# define %s %s' % (config_key, config[json_key]))
  135. rgblight_config.append('#endif // %s' % (config_key,))
  136. for json_key, config_key in rgblight_toggles.items():
  137. if config.get(json_key):
  138. rgblight_config.append('')
  139. rgblight_config.append('#ifndef %s' % (config_key,))
  140. rgblight_config.append('# define %s' % (config_key,))
  141. rgblight_config.append('#endif // %s' % (config_key,))
  142. for json_key, config_key in rgblight_animations.items():
  143. if 'animations' in config and config['animations'].get(json_key):
  144. rgblight_config.append('')
  145. rgblight_config.append('#ifndef %s' % (config_key,))
  146. rgblight_config.append('# define %s' % (config_key,))
  147. rgblight_config.append('#endif // %s' % (config_key,))
  148. return '\n'.join(rgblight_config)
  149. def usb_properties(usb_props):
  150. """Return the config.h lines that setup USB params.
  151. """
  152. usb_lines = []
  153. for info_name, config_name in usb_prop_map.items():
  154. if info_name in usb_props:
  155. usb_lines.append('')
  156. usb_lines.append('#ifndef ' + config_name)
  157. usb_lines.append('# define %s %s' % (config_name, usb_props[info_name]))
  158. usb_lines.append('#endif // ' + config_name)
  159. return '\n'.join(usb_lines)
  160. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  161. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  162. @cli.argument('-kb', '--keyboard', help='Keyboard to generate config.h for.')
  163. @cli.subcommand('Used by the make system to generate info_config.h from info.json', hidden=True)
  164. @automagic_keyboard
  165. @automagic_keymap
  166. def generate_config_h(cli):
  167. """Generates the info_config.h file.
  168. """
  169. # Determine our keyboard(s)
  170. if not cli.config.generate_config_h.keyboard:
  171. cli.log.error('Missing paramater: --keyboard')
  172. cli.subcommands['info'].print_help()
  173. return False
  174. if not is_keyboard(cli.config.generate_config_h.keyboard):
  175. cli.log.error('Invalid keyboard: "%s"', cli.config.generate_config_h.keyboard)
  176. return False
  177. # Build the info.json file
  178. kb_info_json = info_json(cli.config.generate_config_h.keyboard)
  179. # Build the info_config.h file.
  180. config_h_lines = ['/* This file was generated by `qmk generate-config-h`. Do not edit or copy.' ' */', '', '#pragma once']
  181. if 'debounce' in kb_info_json:
  182. config_h_lines.append(debounce(kb_info_json['debounce']))
  183. if 'diode_direction' in kb_info_json:
  184. config_h_lines.append(diode_direction(kb_info_json['diode_direction']))
  185. if 'indicators' in kb_info_json:
  186. config_h_lines.append(indicators(kb_info_json['indicators']))
  187. if 'keyboard_name' in kb_info_json:
  188. config_h_lines.append(keyboard_name(kb_info_json['keyboard_name']))
  189. if 'layout_aliases' in kb_info_json:
  190. config_h_lines.append(layout_aliases(kb_info_json['layout_aliases']))
  191. if 'manufacturer' in kb_info_json:
  192. config_h_lines.append(manufacturer(kb_info_json['manufacturer']))
  193. if 'rgblight' in kb_info_json:
  194. config_h_lines.append(rgblight(kb_info_json['rgblight']))
  195. if 'matrix_pins' in kb_info_json:
  196. config_h_lines.append(matrix_pins(kb_info_json['matrix_pins']))
  197. if 'usb' in kb_info_json:
  198. config_h_lines.append(usb_properties(kb_info_json['usb']))
  199. # Show the results
  200. config_h = '\n'.join(config_h_lines)
  201. if cli.args.output:
  202. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  203. if cli.args.output.exists():
  204. cli.args.output.replace(cli.args.output.name + '.bak')
  205. cli.args.output.write_text(config_h)
  206. if not cli.args.quiet:
  207. cli.log.info('Wrote info_config.h to %s.', cli.args.output)
  208. else:
  209. print(config_h)