keyboard_c.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """Used by the make system to generate keyboard.c from info.json.
  2. """
  3. from milc import cli
  4. from qmk.info import info_json
  5. from qmk.commands import dump_lines
  6. from qmk.keyboard import keyboard_completer, keyboard_folder
  7. from qmk.path import normpath
  8. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, JOYSTICK_AXES
  9. def _gen_led_configs(info_data):
  10. lines = []
  11. if 'layout' in info_data.get('rgb_matrix', {}):
  12. lines.extend(_gen_led_config(info_data, 'rgb_matrix'))
  13. if 'layout' in info_data.get('led_matrix', {}):
  14. lines.extend(_gen_led_config(info_data, 'led_matrix'))
  15. return lines
  16. def _gen_led_config(info_data, config_type):
  17. """Convert info.json content to g_led_config
  18. """
  19. cols = info_data['matrix_size']['cols']
  20. rows = info_data['matrix_size']['rows']
  21. lines = []
  22. matrix = [['NO_LED'] * cols for _ in range(rows)]
  23. pos = []
  24. flags = []
  25. led_layout = info_data[config_type]['layout']
  26. for index, led_data in enumerate(led_layout):
  27. if 'matrix' in led_data:
  28. row, col = led_data['matrix']
  29. matrix[row][col] = str(index)
  30. pos.append(f'{{{led_data.get("x", 0)}, {led_data.get("y", 0)}}}')
  31. flags.append(str(led_data.get('flags', 0)))
  32. if config_type == 'rgb_matrix':
  33. lines.append('#ifdef RGB_MATRIX_ENABLE')
  34. lines.append('#include "rgb_matrix.h"')
  35. elif config_type == 'led_matrix':
  36. lines.append('#ifdef LED_MATRIX_ENABLE')
  37. lines.append('#include "led_matrix.h"')
  38. lines.append('__attribute__ ((weak)) led_config_t g_led_config = {')
  39. lines.append(' {')
  40. for line in matrix:
  41. lines.append(f' {{ {", ".join(line)} }},')
  42. lines.append(' },')
  43. lines.append(f' {{ {", ".join(pos)} }},')
  44. lines.append(f' {{ {", ".join(flags)} }},')
  45. lines.append('};')
  46. lines.append('#endif')
  47. lines.append('')
  48. return lines
  49. def _gen_matrix_mask(info_data):
  50. """Convert info.json content to matrix_mask
  51. """
  52. cols = info_data['matrix_size']['cols']
  53. rows = info_data['matrix_size']['rows']
  54. # Default mask to everything disabled
  55. mask = [['0'] * cols for _ in range(rows)]
  56. # Mirror layout macros squashed on top of each other
  57. for layout_name, layout_data in info_data['layouts'].items():
  58. for key_data in layout_data['layout']:
  59. row, col = key_data['matrix']
  60. if row >= rows or col >= cols:
  61. cli.log.error(f'Skipping matrix_mask due to {layout_name} containing invalid matrix values')
  62. return []
  63. mask[row][col] = '1'
  64. lines = []
  65. lines.append('#ifdef MATRIX_MASKED')
  66. lines.append('__attribute__((weak)) const matrix_row_t matrix_mask[] = {')
  67. for i in range(rows):
  68. lines.append(f' 0b{"".join(reversed(mask[i]))},')
  69. lines.append('};')
  70. lines.append('#endif')
  71. return lines
  72. def _gen_joystick_axes(info_data):
  73. """Convert info.json content to joystick_axes
  74. """
  75. if 'axes' not in info_data.get('joystick', {}):
  76. return []
  77. axes = info_data['joystick']['axes']
  78. axes_keys = list(axes.keys())
  79. lines = []
  80. lines.append('#ifdef JOYSTICK_ENABLE')
  81. lines.append('joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT] = {')
  82. # loop over all available axes - injecting virtual axis for those not specified
  83. for index, cur in enumerate(JOYSTICK_AXES):
  84. # bail out if we have generated all requested axis
  85. if len(axes_keys) == 0:
  86. break
  87. axis = 'virtual'
  88. if cur in axes:
  89. axis = axes[cur]
  90. axes_keys.remove(cur)
  91. if axis == 'virtual':
  92. lines.append(f" [{index}] = JOYSTICK_AXIS_VIRTUAL,")
  93. else:
  94. lines.append(f" [{index}] = JOYSTICK_AXIS_IN({axis['input_pin']}, {axis['low']}, {axis['rest']}, {axis['high']}),")
  95. lines.append('};')
  96. lines.append('#endif')
  97. return lines
  98. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  99. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  100. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.c for.')
  101. @cli.subcommand('Used by the make system to generate keyboard.c from info.json', hidden=True)
  102. def generate_keyboard_c(cli):
  103. """Generates the keyboard.h file.
  104. """
  105. kb_info_json = info_json(cli.args.keyboard)
  106. # Build the layouts.h file.
  107. keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', '']
  108. keyboard_h_lines.extend(_gen_led_configs(kb_info_json))
  109. keyboard_h_lines.extend(_gen_matrix_mask(kb_info_json))
  110. keyboard_h_lines.extend(_gen_joystick_axes(kb_info_json))
  111. # Show the results
  112. dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet)