README_zh.md 29 KB

logo

PikaPython

跨平台的超轻量级嵌入式 Python 引擎

论坛 | 文档中心 | 视频 | 开发板 | BSP | 包管理器 | RT-Thread 软件包 | 参与贡献 | 商业合作

image

1.简介

PikaPython 是一个完全重写的超轻量级 python 引擎,零依赖,零配置,可以在 Flash ≤ 64KB,RAM≤ 4KB 的平台下运行(如 stm32g030c8 和 stm32f103c8),极易部署和扩展,具有大量的中文文档和视频资料。

PikaPython 也称 PikaScript、PikaPy。

PikaPython 具有框架式 C 模块开发工具,只要用 Python 写好调用 API ,就能够自动连接到 C 模块,非常方便快捷。不用手动处理任何全局表、宏定义、等等。

PikaPython 也支持 MDK、iar、RT-studio 等常见 ide 开发,能够轻松地调试 C 模块。

最新资讯

获取 PikaPython:

使用在线图形化工程生成器

生成器地址: http://pikascript.com

PikaPython Studio

PikaPython 串口终端、脚本下载、模块管理工具。

下载PikaPython Studio

相关开源项目

  1. 已发布的模块列表:packages.toml

快速上手

可使用仿真工程快速上手,无需硬件,也可以使用官方支持的开发板Pika派—Wireless,上手即玩。

PikaPi-WIRELESS

开发板基于 ESP32S3 配套全流程移植、适配、模块开发课程。

开发手册

点此进入文档中心

image

视频教程

点此进入视频中心

image

交流论坛

点此进入论坛

image

文件目录

src - 内核源码

bsp - 裸机芯片/板卡支持

port - 操作系统和包管理器支持

test - 单元测试

examples - 示例脚本

package - 模块目录

pikaCompiler - 使用 rust 编写的预编译器

pikaPackageManager - 使用 go 编写的模块管理器

2.平台支持列表

MCU support

MCU bsp gpio time uart pwm adc i2c dac
stm32g030c8
stm32g070cB
stm32f103c8
stm32f103rb
stm32f103ze
stm32f103rc
stm32f401cc
stm32f411ce
stm32f407ze
stm32f407zg
stm32h750vb
stm32f051r8
air32f103cb
ch582
ch32v103r8t6
cm32m101a
w806
apm32f030r8
apm32e103vb
bl-602
bl-706
bl-618
Raspberry Pico
ESP32C3
TC264D
devc
visual-studio
EC600N
mm32f5277e9p
xr806(openharmony)

Board support

Board bsp gpio uart pwm adc i2c rgb lcd arm-2d
Pika-Pi-Zero
Board bsp pika_lvgl
lvgl-vs-simu
swm320
Board bsp arm-2d
QEMU-arm2d
Board bsp LED KEY
SmartLoong

OS support

OS port GPIO TIME PWM
RT-Thread
vsf
OS port Google Test Benchmark
linux

3.特性

(1)运行环境

支持裸机运行,可运行于 RAM ≥ 4kBFLASH ≥ 64kB 的mcu中,如stm32g030, stm32f103c8t6,esp8266。

(2)开发环境

支持串口下载 Python 脚本。

微信交流群

支持 Keil、IAR、rt-thread studio、segger embedded studio 等IDE开发。

支持 CMake、makeFile、Scons 等构建工具

零依赖,零配置,开箱即用,极易集成进已有的C工程。

极易拓展自定义的C原生函数。

支持跨平台,可在 linux 环境开发内核。

(3)语法特性

使用 python3 标准语法的子集。

在编译时支持 python 类和方法定义,完整支持封装、继承、多态、模块功能 - 基于 Pika 预编译器

在运行时支持 python 方法调用、变量定义、对象构造、对象释放、控制流(if\while) - 基于 Pika 运行时内核

更多语法特性细节 |语法|编译时|运行时|Shell| |---|---|---|---| |模块定义 |√|-|-| |模块导入 |√|√|√| |类定义 |√|√|√| |类继承 |√|√|√| |方法定义 |√|√|√| |方法重载 |√|√|√| |方法调用 |√|√|√| |参数定义 |√|√|√| |参数赋值 |√|√|√| |对象新建 |√|√|√| |对象销毁 |√|√|√| |对象嵌套 |√|√|√| |控制流 |√|√|√| #### Operator | + | - | * | / | == | > | < | >= | <= | % | ** | // | != | & | >> | << | and | or | not | in | += | -= | *= | /= | |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| |√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√|√| #### Control flow | Syntax | State | | --- | --- | | if | √ | | while | √ | | for in [list] | √ | | for in range(a, b) | √ | | for in [dict] | √ | | if elif else | √ | | for break/continue | √ | | while break/continue | √ | #### Module | Syntax | Python Module | C Module | | --- | --- | --- | | import [module] | √ | √ | | import [module] as | √ | - | | from [module] import [class/function>]| √ | - | | from [module] import [class/function>] as | √ | - | | from [module] import * | - | PikaObj Module Only | #### List/Dict | Syntax | State | | --- | --- | | l = list() | √ | | l = [a, b, c] | √ | | d = dict() | √ | | d = {'a':x, 'b':y, 'c':z} | √ | #### Exception | Syntax | State | | --- | --- | |try:| √ | |except:| √ | |except [Exception]:| - | |except [Exception] as [err]: | - | |except: ... else:| - | |raise:| √ | |raise [Exception]:| - | |finally:| - | #### Slice | Syntax | str | bytes | list | | --- | --- | --- | --- | | test[i] | √ | √ | √ | | test[a : b] | √ | √ | √ | | test[a :] | √ | √ | √ | #### Other keywords/Syntax | yield | is | comprehensions | | --- | --- | --- | | - | √ | - |

(4)源码规范

注重源码可读性,命名规范,标准统一,完全不使用宏,几乎不使用全局变量。

完整的 googletest 单元测试。

4.交流与技术支持:

Tencent QQ Group:

微信交流群

E-mail: liang6516@outlook.com

5.Demo展示

Demo 01 GPIO

查看代码 ``` python import PikaStdLib import STM32G0 mem = PikaStdLib.MemChecker() io1 = STM32G0.GPIO() time = STM32G0.Time() io1.setPin('PA8') io1.setMode('out') io1.enable() io1.low() print('hello pikapython') print('mem.max :') mem.max() print('mem.now :') mem.now() while True: io1.low() time.sleep_ms(500) io1.high() time.sleep_ms(500) ```

Hnet-image (2)

Demo 02 USART

查看代码 ``` python import PikaStdLib import STM32G0 time = STM32G0.Time() uart = STM32G0.UART() uart.setId(1) uart.setBaudRate(115200) uart.enable() while True: time.sleep_ms(500) readBuff = uart.read(2) print('read 2 char:') print(readBuff) ```

Hnet-image (3)

Demo 03 ADC

查看代码 ``` python import PikaStdLib import STM32G0 time = STM32G0.Time() adc1 = STM32G0.ADC() adc1.setPin('PA1') adc1.enable() while True: val = adc1.read() print('adc1 value:') print(val) time.sleep_ms(500) ```

mmexport1631351523907

Demo 04 PWM output

查看代码 ``` python import PikaStdLib import STM32G0 time = STM32G0.Time() pwm = STM32G0.PWM() pwm.setPin('PA8') pwm.setFrequency(2000) pwm.setDuty(0.5) pwm.enable() while True: time.sleep_ms(500) pwm.setDuty(0.5) time.sleep_ms(500) pwm.setDuty(0.001) ```

Demo 05 RGB

查看代码 ``` python import STM32G0 import PikaPiZero import PikaStdLib rgb = PikaPiZero.RGB() mem = PikaStdLib.MemChecker() rgb.init() rgb.enable() print('hello 2') print('mem used max:') mem.max() while True: print('flowing') rgb.flow() ```

Demo 06 Snake(Need LCD)

查看代码 ``` python from PikaObj import * import PikaStdLib import PikaPiZero import STM32G0 # hardware init lcd = PikaPiZero.LCD() lcd.init() lcd.clear('white') key = PikaPiZero.KEY() key.init() time = STM32G0.Time() x_max = 120 y_max = 150 # snake init s = PikaPiZero.Point() w = 9 h = 9 s.x = 50 s.y = 10 len = 0 while len < 3: b = s i = 0 while i < len: b = b.next i = i + 1 b.next = PikaPiZero.Point() b.next.x = b.x - 10 b.next.y = b.y b.next.prev = b len = len + 1 # ring link b.next = s s.prev = b i = 0 b = s while i < len: lcd.fill(b.x, b.y, w, h, 'blue') b = b.next i = i + 1 print('snake lengh') print(len) # fruit init f = PikaPiZero.Point() f.x = 30 f.y = 20 lcd.fill(f.x, f.y, w, h, 'green') # memory check mem = PikaStdLib.MemChecker() print('mem used max:') mem.max() # main loop d = 0 isUpdate = 1 isEat = 0 while True: if isUpdate: # isUpdate = 0 # check eat fruit if f.x == s.x and f.y == s.y: # have eat fruit isEat = 1 f.x = f.x + 30 if f.x > x_max: f.x = f.x - x_max f.y = f.y + 30 if f.y > y_max: f.y = f.y - y_max lcd.fill(f.x, f.y, w, h, 'green') # move snake by the direction if d == 0: x_new = s.x + 10 y_new = s.y if x_new > x_max: x_new = 0 elif d == 1: x_new = s.x y_new = s.y - 10 if y_new < 0: y_new = y_max elif d == 2: x_new = s.x y_new = s.y + 10 if y_new > y_max: y_new = 0 elif d == 3: x_new = s.x - 10 y_new = s.y if x_new < 0: x_new = x_max if isEat: isEat = 0 b_new = PikaPiZero.Point() b_new.x = x_new b_new.y = y_new b_new.prev = s.prev b_new.next = s s.prev.next = b_new s.prev = b_new s = b_new len = len + 1 print('snake lengh') print(len) print('mem used max:') mem.max() # drow the snake and fruit # clear last body lcd.fill(s.prev.x, s.prev.y, w, h, 'white') # new body s.prev.x = x_new s.prev.y = y_new # head is last body s = s.prev lcd.fill(s.x, s.y, w, h, 'blue') b = s i = 0 # scan key key_val = key.get() if key_val == 0: d = 0 isUpdate = 1 elif key_val == 1: d = 1 isUpdate = 1 elif key_val == 2: d = 2 isUpdate = 1 elif key_val == 3: d = 3 isUpdate = 1 ```

输入图片说明

这几个 Demo 占用的 RAM 最大值只有3.56K,把1K的堆栈也算上就是4.56K,Flash 最大占用是30.4K,以 STM32F103C8T6 的 20K RAM 和 64K Flash 为标准,RAM 才用掉不到25%,Flash 才用掉不到50%。

我们可以简单对比一下 micropython 的常用芯片 STM32F405RG 和这次跑 pikapython 的芯片STM32G070CB

RAM资源对比

image

Flash资源对比

image

参考价对比(以2021年9月11日立创商城10片售价为参考)

image

拓展能力如何呢?

除了设备驱动之外,为 mcu 开发自定义的 python 脚本绑定在 pikapython 的开发框架下非常轻松,下面两个 Demo 就是自定义的C模块拓展,这个 Demo 基于 ARM-2D 图像驱动库开发了一些 python 脚本接口。

几个小方块~

Hnet-image (7)

几个旋转太阳~

Hnet-image (6)

6.内核测试与开发

在 Docker 中测试 (recommend)

Docker 开发环境搭建指南

7.贡献者

如何参与贡献

贡献记录 (2023 年)

贡献记录 贡献者
fix REPL echo for '\b' and 0x7F versaloon
pika_hal support for ESP32 geekheart
Driver Design for PIKA-OPEN-HARDWARE Kirin
bsp support for Makefile-win-mingw unsigned_0
Fix bug for STM32F4 delay_unit() MrLeekon
bsp support for air780e Kirin
Component Selection, Schematic Diagram, PCB Design for PIKA-OPEN-HARDWARE 冰点 (WeChat Name)
Driver Design for PIKA-OPEN-HARDWARE Kirin
Product Design, Component Selection for PIKA-OPEN-HARDWARE 方海钰
Hardware Verification for PIKA-OPEN-HARDWARE HonestQiao
support PIKA_UNBUFFERD_ENABLE config item Seny Lee
add this module blueloveTH
Fix memory leak in event queue randyscott
Fix a bug of _OP_POW blueloveTH
fix bsp/esp32/README.md erzongxie
Fixed a bug where help("modules") crashed when no lib was loaded 梦程MI
support GPIO driver for rt-thread supperthomas
Fixed a typo J. Neuschäfer
codecov CI support Renzhihan
rt-thread build script fix and improve supperthomas
support pikafs pack and unpack to path sjy
fix pika_platform_get_tick() in linux port 梦程MI
CH307开发板、BL618开发板 VeriMake
VM instruction extension framework GorgonMeducer
iotcloud module 梦程MI
implement shell filter GorgonMeducer
修复socket模块在lwip中fcntl的参数数量错误 梦程MI
support GPIO pika_hal for BLMCU codercmd
Fix Typo on PikaVM.c Ikko Eltociear Ashimine
Fix type assert on dataArgs.c itschina
add examples/Device/KEY_POLL.PY codercmd
hmac hashlib base64 aes modules 梦程MI
fix pikaCompiler build error on macos 梦程MI
贡献记录 (2022 年) | 贡献记录 | 贡献者 | | --- | --- | | request module | [onceday](https://gitee.com/onceday) | | mqtt module | [FlintJ](https://gitee.com/flintj) | | Donate:500¥ | [hardsofttech](https://gitee.com/hardsofttech) | | Donate:500¥ | [edmund_wz](https://gitee.com/edmund_wz) | | Donate:500¥ | [Meco Jianting Man](https://github.com/mysterywolf) | | Donate:500¥ | [hardsofttech](https://gitee.com/hardsofttech) | |PikaNN module| [Renzhihan](https://github.com/Renzhihan) | | re module | [eglwang](https://gitee.com/eglwang) | | PikaMath.Quaternion() | [purewind7](https://gitee.com/purewind7) | | contrubute to PikaCV | [purewind7](https://gitee.com/purewind7) | | xr806(openharmony) bsp |[sjy](https://gitee.com/shanjiayang)| | ESP32 package |沧御| | MM32F5277E9P package |[unsigned](https://gitee.com/unsigned0)| | mm32f5277e9p BSP |[unsigned](https://gitee.com/unsigned0)| | Add [CMSIS-PACK](https://pikadoc-en.readthedocs.io/en/latest/get-start_cmsis-pack.html) | [GorgonMeducer](https://github.com/GorgonMeducer) | | donate: air32f103 board * 20 | 合宙 [xinxi204](https://gitee.com/xinxi204) | | contrubute to PikaMath.Math() | [onceday](https://gitee.com/onceday) | | add support for Unix Time | [onceday](https://gitee.com/onceday) | | contrubute to PikaStdLib.String() | [purewind7](https://gitee.com/purewind7) | | Add [PLOOC](https://github.com/GorgonMeducer/PLOOC) support for pikapython Core | [GorgonMeducer](https://github.com/GorgonMeducer) | | Donate:500¥ (ctypes 相关) | 蒋太平 | | fix linux install script | [Maximilian Gerhardt](https://github.com/maxgerhardt) | | ch582 package | [梦程MI](https://gitee.com/dreamcmi) | | ch582 BSP | [梦程MI](https://gitee.com/dreamcmi) | | Donate:EC600X QuecPython Develop board| 移远通信 武加玉 | | Performance Point: 900->1400 | [GorgonMeducer](https://github.com/GorgonMeducer) | | stm32f051r8 BSP |[unsigned](https://gitee.com/unsigned0)| | stm32f407ze BSP |[unsigned](https://gitee.com/unsigned0)| | devc BSP |[unsigned](https://gitee.com/unsigned0)| | TC264D BSP |[unsigned](https://gitee.com/unsigned0)| | PikaVM improvement | [GorgonMeducer](https://github.com/GorgonMeducer) | | W801Device package | 刘延(微信名) | | W806 bsp | 刘延(微信名) | | Donate:ESP32C3 5pic,linker|启明云端 沧御| | Donate:LS1C101 mcu 10pic, LS1c101 develop board,debuger|龙芯俱乐部 石南| | PikaVSF OS package | [versaloon](https://github.com/versaloon) | | ESP32C3 BSP |沧御|
贡献记录 (2021 年) | 贡献记录 | 贡献者 | | --- | --- | | Donate:ESP32 board、ink lcd*4 | name(微信名) | | Donate:smartloong board | 龙芯俱乐部 石南 | | package/STM32F1 | [sjy](https://gitee.com/shanjiayang) | | package/STM32F103RBBooter | [sjy](https://gitee.com/shanjiayang) | | bsp/stm32f103rb | [sjy](https://gitee.com/shanjiayang) | | QEMU ARM-2D simulation proejct | [liuduanfei](https://github.com/liuduanfei) | | Donate:GD32E103TB2 2 pic | 信息牛(微信名) | | Rt-thread bsp、port、module| [Meco Jianting Man](https://github.com/mysterywolf) | | Donate:EC600S-CN 4G| 移远模块 | | Donate:BL706 board|博流智能 [bouffalolab](https://github.com/bouffalolab)| | Donate:CM32M101A 开发板| 孟巍(微信名) | | Donate:APM32F030R8 board | 极海半导体 陈成 | | Donate:APM32E103VB board | 极海半导体 陈成 | | Donate:APEX-Link| 极海半导体 陈成 | | corde format,add gitattributes| [Meco Jianting Man](https://github.com/mysterywolf) | | demo/simulation-keil | 千帆(微信名) | | demo/stm32f103zet6/demo01-led-stm32f103zet6 | [甜航](https://github.com/easyzoom) | | demo/stm32f103zet6/demo02-led-stm32f103zet6_tworoot | [甜航](https://github.com/easyzoom) | | demo/stm32f407zgt/demo01-led-stm32f407vgt | [甜航](https://github.com/easyzoom) |