是否可以在本地编译microbit python代码



我正在使用xorg运行Ubuntu 22.04。我需要找到一种方法,将microbit python代码本地编译为固件十六进制文件。首先,我跟随导游来到这里https://microbit-micropython.readthedocs.io/en/latest/devguide/flashfirmware.html.

经过大量的调试,我得出了以下结论:https://pastebin.com/MGShD31N

但是,文件平台.h确实存在。

sawntoe@uwubuntu:~/Documents/Assignments/2022/TVP/micropython$ ls /home/sawntoe/Documents/Assignments/2022/TVP/micropython/yotta_modules/mbed-classic/api/platform.h
/home/sawntoe/Documents/Assignments/2022/TVP/micropython/yotta_modules/mbed-classic/api/platform.h
sawntoe@uwubuntu:~/Documents/Assignments/2022/TVP/micropython$ 

在这一点上,我放弃了这一点,并尝试在AppImage中使用Mu编辑器。然而,穆需要航路,而我在xorg。

有人知道这是否可能吗?谢谢

Mu和uflash命令能够从.hex文件中检索Python代码。使用uflash可以执行以下操作,例如:

uflash my_script.py

我认为你想要的在某种程度上是可能做到的,但这比仅仅使用他们的网络python编辑器更难:https://python.microbit.org/v/2

Peter Till回答了最初的问题。下面的附加内容通过展示如何自动化构建和加载过程来补充这个答案。我用Debian。最初的问题是使用了基于Debian的Ubuntu。

用于查找和装载micro:bit的脚本

当代码加载到micro:bit时,板将从系统中卸下。因此,每次有新的代码要加载时,都必须重新安装板。

我修改了一个脚本来查找并装载micro:bit。

#!/bin/bash

BASEPATH="/media/$(whoami)/"
MICRO="MICROBIT"

if [ $# -eq 0 ]
then
echo "no argument supplied, use 'mount' or 'unmount'"
exit 1
fi

if [ $1 == "--help" ]
then
echo "mounts or unmounts a BBC micro:bit"
echo "args: mount - mount the microbit, unmout - unmount the microbit"
fi

# how many MICRO found in udiksctl dump
RESULTS=$(udisksctl dump | grep IdLabel | grep -c -i $MICRO)

case "$RESULTS" in

0 )     echo "no $MICRO found in 'udkisksctl dump'"
exit 0
;;

1 )     DEVICELABEL=$(udisksctl dump | grep IdLabel | grep -i $MICRO | cut -d ":" -f 2 | sed 's/^[ t]*//')
DEVICE=$(udisksctl dump | grep -i "IdLabel: +$DEVICELABEL" -B 12 | grep " Device:" | cut -d ":" -f 2 | sed 's/^[ t]*//')
DEVICEPATH="$BASEPATH""$DEVICELABEL"
echo "found one $MICRO, device: $DEVICE"

if [[ -z $(mount | grep "$DEVICE") ]]
then
echo "$DEVICELABEL was unmounted"
if [ $1 == "mount" ]
then
udisksctl mount -b "$DEVICE"
exit 0
fi
else
echo "$DEVICELABEL was mounted"
if [ $1 == "unmount" ]
then
udisksctl unmount -b "$DEVICE"
exit 0
fi
fi
;;

* )     echo "more than one $MICRO found"
;;

esac

echo "exiting without doing anything"

我在.bashrc文件中将此脚本别名为mm

自动安装micro:bit并闪烁python文件

我使用inotifywait命令运行mm,然后运行uflash来加载我正在处理的.py文件。每次保存python文件时,都会运行别名命令mm,然后再运行ufflash命令。

while inotifywait -e modify <your_file>.py ; do mm && uflash <your_file>.py ; done

好的,详细介绍彼得·蒂尔的答案。

首先,你可以使用uflash:

uflash path/to/your/code .

或者,你可以使用缩微胶片:

ufs put path/to/main.py

使用Carlos Atencio的Docker设置Ubuntu 22.04主机CLI,构建自己的固件

在尝试设置工具链一段时间后,我最终决定用工具链在谷歌上搜索Docker图像,并发现https://github.com/carlosperate/docker-microbit-toolchain根据Micro:Bit基金会员工Carlos Atencio的承诺,这绝对奏效了:

# Get examples.
git clone https://github.com/bbcmicrobit/micropython
cd micropython
git checkout 7fc33d13b31a915cbe90dc5d515c6337b5fa1660
# Get Docker image.
docker pull ghcr.io/carlosperate/microbit-toolchain:latest
# Build setup to be run once.
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest yt target bbc-microbit-classic-gcc-nosd@https://github.com/lancaster-university/yotta-target-bbc-microbit-classic-gcc-nosd
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest make all
# Build one example.
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest 
tools/makecombinedhex.py build/firmware.hex examples/counter.py -o build/counter.hex
# Build all examples.
docker run -v $(pwd):/home --rm ghcr.io/carlosperate/microbit-toolchain:latest 
bash -c 'for f in examples/*; do b="$(basename "$f")"; echo $b; tools/makecombinedhex.py build/firmware.hex "$f" -o "build/${b%.py}.hex"; done'

然后你可以闪现你想要运行的例子:

cp build/counter.hex "/media/$USER/MICROBIT/"

一些进一步的评论:从BBC micro:bit 的命令行生成micropython+python代码".hex"文件

最新更新