从命令行生成 micropython + python 代码 `.hex` 文件

Generating micropython + python code `.hex` file from commandline

是否可以在 Linux 命令行而不是在其中一个编辑器中使用 MicroPython 和我自己的 python 程序代码生成 .hex 文件?

看你问题中的标签,你似乎想在 BBC micro:bit 上使用 MicroPython,对吗? 如果是这种情况,那么您可以使用此 Python 命令行工具:https://github.com/ntoll/uflash/

关于如何安装和使用它的说明可以在 link 的 README 中找到。

这适用于 Python 2 和 3,并且您的 Linux 发行版很可能至少有一个 Python 开箱即用的版本。

如果你安装了 pip,你可以很容易地安装它:pip install uflash

但您也可以下载源代码,使用 git 或从 GitHub (https://github.com/ntoll/uflash/archive/master.zip) 下载 zip 文件,并且 运行 它无需安装任何东西。在这种情况下,您可以使用 Python:

执行 uFlash 脚本

python uflash.py path_to_your_code.py

当前版本的 uFlash 包括 micro:bit 的最新版本的 MicroPython。

您可以在vscode或vim等任何文本编辑器中为microbit编写micropython代码。将其保存为 .py 文件。

要创建 .hex 文件,请使用在使用以下命令安装 uflash 时随 uflash 安装的 py2exe 工具:

pip install uflash

为名为 hello.py 的 microbit micropython 文件创建 .hex 文件:

py2hex hello.py

这将创建一个名为 hello.hex 的文件。这可以通过文件资源管理器拖放到连接的 microbit 上。我使用 Nautilus,microbit 显示为 'MICROBIT'.

您可以使用 uflash 自动创建 .hex 文件并将其加载到 microbit,例如

uflash hello.py

这将创建 .hex 文件,然后将其加载到附加的 microbit 上。 .hex 文件不会留在您的文件系统中。 microbit 有一个习惯,在加载 .hex 文件后不再附加到文件系统,需要在构建之间 re-attached。