中文页 | Forum | Documents | Videos | BSP | Package Manager | RT-Thread package
PikaPython is an ultra-lightweight Python engine with zero dependencies and zero-configuration, that can run with 4KB of RAM (such as STM32G030C8 and STM32F103C8).
PikaPython also known as PikaScript and PikaPy.
It's very easy to bind C function to python module with the help of Pika Pre-compiler.Only write the API of python in .pyi
and the bindings are autoly generated by Pre-compiler.
The Pre-compiler is fully open-source, and the source code is in the tools folder.
Generator url: http://pikascript.com
Note: You need the Community Edition license (Now Free) to build Keil projects, and the version of Keil should be newer than v5.36.
Use Pika package manager PikaPackage.exe
-
Run the PikaPackage.exe directly, then the pikapython repo would be downloaded auto in the /tmp/pikascript folder of your current disc partition.
-
Add requestment.txt to the same folder of PikaPackage.exe then run PikaPackage.exe. The run-time core, pre-compiler and moudles would be installed in the current folder.
-
Released modules:packages.toml
You can use simulation project to quick start without hardware,or use the out-of-the-box develop board Pika-Pi—Zero.
The board based on STM32G030C8T6 MCU, only 64kB Flash, 8kB RAM can run pikapython with total peripheral device (GPIO、TIME、IIC、RGB、KEY、LCD、RGB). CH340 is deployed to support USB to serial with Type-C USB, support download python script py serial, there are 4 RGB on the board and support the LCD.
https://pikadoc.readthedocs.io/en/latest/index.html
https://pikadoc.readthedocs.io/en/latest/如何参与社区贡献.html
src - core code
bsp - mcu/board support
port - OS and package manager
test - unit test
document - developt document
examples - example scripts
package - packages and moudles
pikaCompiler - pre-compiler write by Rust, used to bind C function to python moudle.
pikaPackageManager - pacakge manager
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 | √ | |||||||
Raspberry Pico | √ | |||||||
ESP32C3 | √ | √ | √ | |||||
TC264D | √ | |||||||
devc | √ | |||||||
visual-studio | √ | |||||||
EC600N | √ | |||||||
mm32f5277e9p | √ | √ | ||||||
xr806(openharmony) | √ |
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 | port | GPIO | TIME | PWM |
---|---|---|---|---|
RT-Thread | √ | √ | √ | √ |
vsf | √ | √ |
OS | port | Google Test | Benchmark |
---|---|---|---|
linux | √ | √ | √ |
Support run in mcu without OS or file system. Can run in everywhere with RAM ≥ 4kB and FLASH ≥ 64kB,such as stm32g030, stm32f103c8t6,esp8266.
Support IDEs like Keil, IAR, RT-Thread studio and segger embedded studio to develop C moudle.
Support build tools like CMake, makeFile and Scons.
Zero dependencies, zero configuration, out-of-the-box, easy to integrated into privious C projcet.
Eazy to extern customized C moudles.
Support linux.
Support subaggregate of python 3 standard syntax.
Support class and method define, encapsulation-inheritance-polymorphism and moudles in .pyi
of C module.
More Details About Syntax Support
Syntax | Compile-Time | Run-Time | Shell |
---|---|---|---|
Module Define | √ | - | - |
Module Import | √ | √ | √ |
Class Define | √ | √ | √ |
Class Inherit | √ | √ | √ |
Method Define | √ | √ | √ |
Method Override | √ | √ | √ |
Method Invoke | √ | √ | √ |
Argument Define | √ | √ | √ |
Argument Assignment | √ | √ | √ |
Object New | √ | √ | √ |
Object Free | √ | √ | √ |
Object Nest | √ | √ | √ |
Control flow | √ | √ | √ |
- | * | / | == | > | < | >= | <= | % | ** | // | != | & | >> | << | and | or | not | in | = | -= | *= | /= | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ |
Syntax | State |
---|---|
if | √ |
while | √ |
for in [list] | √ |
for in range(a, b) | √ |
for in [dict] | √ |
if elif else | √ |
for break/continue | √ |
while break/continue | √ |
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 |
Syntax | State |
---|---|
l = list() | √ |
l = [a, b, c] | √ |
d = dict() | √ |
d = {'a':x, 'b':y, 'c':z} | √ |
Syntax | State |
---|---|
try: | √ |
except: | √ |
except [Exception]: | - |
except [Exception] as [err]: | - |
except: ... else: | - |
raise: | √ |
raise [Exception]: | - |
finally: | - |
Syntax | str | bytes | list |
---|---|---|---|
test[i] | √ | √ | √ |
test[a : b] | √ | √ | √ |
test[a :] | √ | √ | √ |
yield | is | comprehensions |
---|---|---|
- | √ | - |
Readability first, nearly never use macro function and global argument.
Complete unit testes based on google test.
The scripts in demos are in the examples folder.
View Code
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)
View Code
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)
View Code
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)
View Code
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)
View Code
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()
View Code
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
The maximum RAM usage of these demos is only 3.56K, which is 4.56K if the 1K stack is included, and the maximum Flash usage is 30.4K. Refer to the 20K RAM and 64K Flash of STM32F103C8T6, less than 25% RAM and less than 50% Flash are used.
In addition to device drivers, developing custom Python modules for MCU is very easy. The following two demos are extensions of the custom module, which developed python interfaces for the ARM-2D image driver library.