keyboard_c.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  9. def _gen_led_config(info_data):
  10. """Convert info.json content to g_led_config
  11. """
  12. cols = info_data['matrix_size']['cols']
  13. rows = info_data['matrix_size']['rows']
  14. config_type = None
  15. if 'layout' in info_data.get('rgb_matrix', {}):
  16. config_type = 'rgb_matrix'
  17. elif 'layout' in info_data.get('led_matrix', {}):
  18. config_type = 'led_matrix'
  19. lines = []
  20. if not config_type:
  21. return lines
  22. matrix = [['NO_LED'] * cols for i in range(rows)]
  23. pos = []
  24. flags = []
  25. led_config = info_data[config_type]['layout']
  26. for index, item in enumerate(led_config, start=0):
  27. if 'matrix' in item:
  28. (x, y) = item['matrix']
  29. matrix[x][y] = str(index)
  30. pos.append(f'{{ {item.get("x", 0)},{item.get("y", 0)} }}')
  31. flags.append(str(item.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. return lines
  48. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  49. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  50. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.c for.')
  51. @cli.subcommand('Used by the make system to generate keyboard.c from info.json', hidden=True)
  52. def generate_keyboard_c(cli):
  53. """Generates the keyboard.h file.
  54. """
  55. kb_info_json = info_json(cli.args.keyboard)
  56. # Build the layouts.h file.
  57. keyboard_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#include QMK_KEYBOARD_H', '']
  58. keyboard_h_lines.extend(_gen_led_config(kb_info_json))
  59. # Show the results
  60. dump_lines(cli.args.output, keyboard_h_lines, cli.args.quiet)