flash.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """Compile and flash QMK Firmware
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. A bootloader must be specified.
  4. """
  5. import subprocess
  6. from argparse import FileType
  7. from milc import cli
  8. import qmk.path
  9. from qmk.decorators import automagic_keyboard, automagic_keymap
  10. from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
  11. def print_bootloader_help():
  12. """Prints the available bootloaders listed in docs.qmk.fm.
  13. """
  14. cli.log.info('Here are the available bootloaders:')
  15. cli.echo('\tdfu')
  16. cli.echo('\tdfu-ee')
  17. cli.echo('\tdfu-split-left')
  18. cli.echo('\tdfu-split-right')
  19. cli.echo('\tavrdude')
  20. cli.echo('\tBootloadHID')
  21. cli.echo('\tdfu-util')
  22. cli.echo('\tdfu-util-split-left')
  23. cli.echo('\tdfu-util-split-right')
  24. cli.echo('\tst-link-cli')
  25. cli.echo('\tst-flash')
  26. cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
  27. @cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.')
  28. @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
  29. @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
  30. @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
  31. @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
  32. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  33. @cli.subcommand('QMK Flash.')
  34. @automagic_keyboard
  35. @automagic_keymap
  36. def flash(cli):
  37. """Compile and or flash QMK Firmware or keyboard/layout
  38. If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments
  39. will be ignored.
  40. If no file is supplied, keymap and keyboard are expected.
  41. If bootloader is omitted the make system will use the configured bootloader for that keyboard.
  42. """
  43. command = ''
  44. if cli.args.bootloaders:
  45. # Provide usage and list bootloaders
  46. cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
  47. print_bootloader_help()
  48. return False
  49. if cli.args.filename:
  50. # Handle compiling a configurator JSON
  51. user_keymap = parse_configurator_json(cli.args.filename)
  52. keymap_path = qmk.path.keymap(user_keymap['keyboard'])
  53. command = compile_configurator_json(user_keymap, cli.args.bootloader)
  54. cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
  55. else:
  56. if cli.config.flash.keyboard and cli.config.flash.keymap:
  57. # Generate the make command for a specific keyboard/keymap.
  58. command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader)
  59. elif not cli.config.flash.keyboard:
  60. cli.log.error('Could not determine keyboard!')
  61. elif not cli.config.flash.keymap:
  62. cli.log.error('Could not determine keymap!')
  63. # Compile the firmware, if we're able to
  64. if command:
  65. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
  66. if not cli.args.dry_run:
  67. cli.echo('\n')
  68. compile = subprocess.run(command)
  69. return compile.returncode
  70. else:
  71. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  72. cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
  73. return False