月度归档:2024年02月

编译其他模块到micropython

将其他模块下载到文件

以下命令编译一下 mpy-coress ,这是为了构建MicroPython交叉编译器,以便将一些内置脚本预编译为字节码,这只需要做一次:

make -C mpy-cross
$ make -C mpy-cross
#转到模块
$ cd ports/esp32

$ make BOARD=GENERIC_C3 clean # 可选项,如果需要完全重新编译则执行这行命令
$ make submodules
$ make BOARD=GENERIC_C3

ST7789

git clone https://github.com/micropython/micropython.git
git clone https://github.com/russhughes/st7789_mpy.git
cd micropython/
git submodule update --init
cd mpy-cross/
make
cd ..
cd ports/esp32
#若需要加入python模块,请将py文件复制到modules的文件夹下
cp ../../../st7789_mpy/fonts/bitmap/vga1_16x16.py modules
cp ../../../st7789_mpy/fonts/truetype/NotoSans_32.py modules
cp ../../../st7789_mpy/fonts/vector/scripts.py modules
#添加py模块 上面
make USER_C_MODULES=../../../../st7789_mpy/st7789/micropython.cmake FROZEN_MANIFEST="" FROZEN_MPY_DIR=$UPYDIR/modules

ULAB

git clone https://github.com/micropython/micropython.git
git clone https://github.com/v923z/micropython-ulab.git ulab
make BOARD=PYBV11 USER_C_MODULES=../../../ulab all

make就是编译命令

-j 立即多个编译命令同时执行,加快速度 -j4 (连在一起)

clean 清楚之前编译

BOARD=PYBV11 #设置编译后的文件及开发板子

USER_C_MODULES= #设置额外的C模块 .cmake文件
FROZEN_MANIFEST=”” #冻结文件
FROZEN_MPY_DIR=$UPYDIR/modules #python模块目录

docker 编译micropython esp8266 添加PYthon模块

1.git 官方的micropython库(默认在root目录下执行)

git clone https://github.com/micropython/micropython.git

2.对需要的模块进行修改,在ports/esp8266/modules 文件下创建的python文件都会被编译到固件中(bbb.py 到时候引入 import bbb),若是文件夹 lis下的 a.py文件,到时候访问时 import lis.a

3.使用docker安装官方的镜像,进行编译(若第二步git在root下 ,那文件就是 root/micropython 即~/micropython)

DOCKER:

docker run -it --rm -v ~/micropython:$HOME -u $UID -w $PWD larsks/esp-open-sdk bash

进入docker的mpy编译系统之后

make -C mpy-cross

然后 转到ports/esp8266目录进行编译

$ cd ports/esp8266 #转到esp8266目录
$ make submodules #初始化模块
$ make -j BOARD=ESP8266_GENERIC
#进行交叉编译,保存到ports/esp8266下的build-ESP8266_GENERIC文件夹下的firmware.bin固件,若该文件夹存在,则无法编辑,可以先删除
$ exit
#退出docker的mpy编译界面

注意:

1.无法放入非python文件在modules中,modules中的文件无法使用open()去读取

2.若需要对不同esp8266进行编译(esp8266默认4MBFlash ESP01S为1MB 则需要设置)

#在执行make -j BOARD=ESP8266_GENERIC前先设置大小
$ make BOARD=ESP8266_GENERIC BOARD_VARIANT=FLASH_512K
#设置flash为512KB
$ make BOARD=ESP8266_GENERIC BOARD_VARIANT=FLASH_1M
#设置flash为1MB,esp01s编译mpy固件,请选择该语句设置。

全自动设置:

$ cd ~/micropython
#先设置到micropython目录下
$ docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -C mpy-cross

$ cd ports/esp8266
$ docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -j BOARD=ESP8266_GENERIC

Building with Docker

Once you have installed Docker, you can run all of the following build commands inside the Docker container by prefixing them with docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk ...command.... This will automatically download the Docker image provided by @larsks which contains the full toolchain and SDK.

Then you need to compile the MicroPython cross-compiler (mpy-cross). From the root of this repository, run:$ docker run –rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -C mpy-cross

Note: The mpy-cross binary will likely only work inside the Docker container. This will not be a problem if you’re only building ESP8266 firmware, but if you’re also working on other ports then you will need to recompile for your host when switching between ports. To avoid this, use the local toolchain instead.

Then to compile the ESP8266 firmware:

$ cd ports/esp8266
$ docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -j BOARD=ESP8266_GENERIC

全自动设置

1.选择上传的python模块,记录下来,上传到对应的开发板【esp8266 esp32】的modules上面,并在数据库中标记,等待完成编译后删除

2.shell执行脚本

#esp8266为例子,micropython文件存放在root下【请删除ESP8266_GENERIC】
#! /bin/bash
cd ~/micropython
docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -C mpy-cross
cd ports/esp8266
if [ ! -f "build-ESP8266_GENERIC" ];then
    echo "文件不存在,执行编译"
else
    rm -rf build-ESP8266_GENERIC
fi
docker run --rm -v $HOME:$HOME -u $UID -w $PWD larsks/esp-open-sdk make -j BOARD=ESP8266_GENERIC