Home

Awesome

<p align="center"> <img alt="logo" src="document/image/144221063-a85b2cb0-0416-493f-9afb-56cff31e977d.jpg" width="300"> </p> <h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">PikaPython</h1> <h4 align="center">Cross platform ultra lightweight embedded Python engine</h4> <p align="center"> <a href="https://gitee.com/lyon1998/pikapython/stargazers"><img src="https://gitee.com/lyon1998/pikapython/badge/star.svg?theme=gvp"></a> <a href="https://gitee.com/lyon1998/pikapython/members"><img src="https://gitee.com/lyon1998/pikapython/badge/fork.svg?theme=gvp"></a> <a href="https://github.com/pikastech/pikapython/stargazers"><img src="https://img.shields.io/github/stars/pikastech/pikapython?style=flat-square&logo=GitHub"></a> <a href="https://github.com/pikastech/pikapython/network/members"><img src="https://img.shields.io/github/forks/pikastech/pikapython?style=flat-square&logo=GitHub"></a> <a href="https://github.com/pikastech/pikapython/watchers"><img src="https://img.shields.io/github/watchers/pikastech/pikapython?style=flat-square&logo=GitHub"></a> <a href="https://github.com/pikastech/pikapython/issues"><img src="https://img.shields.io/github/issues/pikastech/pikapython.svg?style=flat-square&logo=GitHub"></a> <a href="https://github.com/pikastech/pikapython/blob/master/LICENSE"><img src="https://img.shields.io/github/license/pikastech/pikapython.svg?style=flat-square"></a> <a href="https://github.com/pikasTech/pikapython/actions/workflows/CI.yml"><img src="https://github.com/pikasTech/pikapython/actions/workflows/CI.yml/badge.svg"> </a> <a href="https://app.codecov.io/gh/pikasTech/PikaPython"><img src="https://codecov.io/gh/pikasTech/PikaPython/branch/master/graph/badge.svg"> </a> </p> <p align="center"> <a href="README_zh.md" >中文页</a> | <a href="https://whycan.com/f_55.html" >Forum</a> | <a href="https://pikadoc-en.readthedocs.io/en/latest/index.html" >Documents</a> | <a href="https://space.bilibili.com/5365336/channel/detail?cid=202329">Videos</a> | <a href="../../tree/master/bsp#platform-support">BSP</a> | <a href="https://pikadoc-en.readthedocs.io/en/latest/%E5%8C%85%E7%AE%A1%E7%90%86%E5%99%A8%E4%B8%8E%E6%A8%A1%E5%9D%97%E7%AE%A1%E7%90%86.html">Package Manager</a> | <a href="https://pikadoc-en.readthedocs.io/en/latest/%E5%A6%82%E4%BD%95%E5%8F%82%E4%B8%8E%E7%A4%BE%E5%8C%BA%E8%B4%A1%E7%8C%AE.html" >Contribute</a> | <a href="https://pikadoc-en.readthedocs.io/en/latest/%E5%95%86%E4%B8%9A%E5%90%88%E4%BD%9C%E6%96%B9%E5%BC%8F.html">Business</a> </p>

image

1. Abstract

PikaPython is an ultra-lightweight Python interpreter that runs with only 4KB of RAM, zero dependencies. It is ready to use out of the box without any configuration required and easy to extend with C.

PikaPython also known as PikaScript and PikaPy.

<img src="https://user-images.githubusercontent.com/88232613/147799704-d79345da-a2b9-4593-b1eb-ff9de127429a.png" width="500"/>

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.

image-20220520233540245

Get pikapython:

Use Online Project Generator

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.

PikaPython studio

PikaPython serial terminal, script download and package menage tool.

Quick Start

You can use simulation project to quick start without hardware,or use the out-of-the-box develop board Pika-Pi—Zero.

image

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.

Related Projects

Document

https://pikadoc-en.readthedocs.io/en/latest/index.html

How to contribute

https://pikadoc-en.readthedocs.io/en/latest/%E5%A6%82%E4%BD%95%E5%8F%82%E4%B8%8E%E7%A4%BE%E5%8C%BA%E8%B4%A1%E7%8C%AE.html

Folders

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

2.Platform support

MCU support

MCUbspgpiotimeuartpwmadci2cdac
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

Boardbspgpiouartpwmadci2crgblcdarm-2d
Pika-Pi-Zero
Boardbsppika_lvgl
lvgl-vs-simu
swm320
Boardbsparm-2d
QEMU-arm2d
BoardbspLEDKEY
SmartLoong

OS support

OSportGPIOTIMEPWM
RT-Thread
vsf
OSportGoogle TestBenchmark
linux

3.Characteristic

(1)Run environment

Support run in mcu without OS or file system. Can run in everywhere with RAM ≥ 4kB and FLASH ≥ 64kB,such as stm32g030, stm32f103c8t6,esp8266.

(2)Develop enviroment

Support REPL by serial.

<img src="assets/1641178790145-2f026e70-4ba1-4e9a-b05f-c602b2bd8cad.png" width="400" alt="微信交流群"/><br/>

Support run and program python scripts by serial.

<img src="document/image/134841230-85de6734-8467-4245-93a5-d452b5022b42.gif" width="400" alt="微信交流群"/><br/>

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.

(3)Syntax

Support subaggregate of python 3 standard syntax.

Support class and method define, encapsulation-inheritance-polymorphism and moudles in .pyi of C module.

<details><summary>More Details About Syntax Support</summary>
SyntaxCompile-TimeRun-TimeShell
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

Operator

+-*/==><>=<=%**//!=&>><<andornotin+=-=*=/=

Control flow

SyntaxState
if
while
for in [list]
for in range(a, b)
for in [dict]
if elif else
for break/continue
while break/continue

Module

SyntaxPython ModuleC 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

SyntaxState
l = list()
l = [a, b, c]
d = dict()
d = {'a':x, 'b':y, 'c':z}

Exception

SyntaxState
try:
except:
except [Exception]:-
except [Exception] as [err]:-
except: ... else:-
raise:
raise [Exception]:-
finally:-

Slice

Syntaxstrbyteslist
test[i]
test[a : b]
test[a :]

Other keywords/Syntax

yieldiscomprehensions
--
</details>

(4)Develop standard.

Readability first, nearly never use macro function and global argument.

Complete unit testes based on google test.

4.Both talk and show the code:

E-mail: liang6516@outlook.com

5.Demo

The scripts in demos are in the examples folder.

Demo 01 GPIO

<details><summary>View Code</summary>
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)

</details>

Hnet-image (2)

Demo 02 USART

<details><summary>View Code</summary>
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)

</details>

Hnet-image (3)

Demo 03 ADC

<details> <summary>View Code</summary>
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)

</details>

mmexport1631351523907

Demo 04 PWM output

<details><summary>View Code</summary>
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)
    
</details>

Demo 05 RGB

<details><summary>View Code</summary>
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()

</details>

Demo 06 Snake(Need LCD)

<details> <summary>View Code</summary>
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
	

</details>

image

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.

About the custom extern module ?

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.

Boxes~

Hnet-image (7)

Rotating suns~

Hnet-image (6)

6.Test and develop Core

Test core in docker (recommend)

get start with docker

✨ Contributors

<a href="https://github.com/pikastech/pikapython/graphs/contributors"> <img src="https://contrib.rocks/image?repo=pikastech/pikascript" /> </a>

How to contribute

<div class="log"></div>

Contribute Log On 2023

Contribute logContributer
fix REPL echo for '\b' and 0x7Fversaloon
pika_hal support for ESP32geekheart
Driver Design for PIKA-OPEN-HARDWAREKirin
bsp support for Makefile-win-mingwunsigned_0
Fix bug for STM32F4 delay_unit()MrLeekon
bsp support for air780eKirin
Component Selection, Schematic Diagram, PCB Design for PIKA-OPEN-HARDWARE冰点 (WeChat Name)
Driver Design for PIKA-OPEN-HARDWAREKirin
Product Design, Component Selection for PIKA-OPEN-HARDWARE方海钰
Hardware Verification for PIKA-OPEN-HARDWAREHonestQiao
support PIKA_UNBUFFERD_ENABLE config itemSeny Lee
add this moduleblueloveTH
Fix memory leak in event queuerandyscott
Fix a bug of _OP_POWblueloveTH
fix bsp/esp32/README.mderzongxie
Fixed a bug where help("modules") crashed when no lib was loaded梦程MI
support GPIO driver for rt-threadsupperthomas
Fixed a typoJ. Neuschäfer
codecov CI supportRenzhihan
rt-thread build script fix and improvesupperthomas
support pikafs pack and unpack to pathsjy
fix pika_platform_get_tick() in linux port梦程MI
CH307开发板、BL618开发板VeriMake
VM instruction extension frameworkGorgonMeducer
iotcloud module梦程MI
implement shell filterGorgonMeducer
修复socket模块在lwip中fcntl的参数数量错误梦程MI
support GPIO pika_hal for BLMCUcodercmd
Fix Typo on PikaVM.cIkko Eltociear Ashimine
Fix type assert on dataArgs.citschina
add examples/Device/KEY_POLL.PYcodercmd
hmac hashlib base64 aes modules梦程MI
fix pikaCompiler build error on macos梦程MI
<details><summary> Contribute Log On 2022 </summary>
Contribute logContributer
request moduleonceday
mqtt moduleFlintJ
Donate:500¥hardsofttech
Donate:500¥edmund_wz
Donate:500¥Meco Jianting Man
Donate:500¥hardsofttech
PikaNN moduleRenzhihan
re moduleeglwang
PikaMath.Quaternion()purewind7
contrubute to PikaCVpurewind7
xr806(openharmony) bspsjy
ESP32 package沧御
MM32F5277E9P packageunsigned
mm32f5277e9p BSPunsigned
Add CMSIS-PACKGorgonMeducer
donate: air32f103 board * 20合宙 xinxi204
contrubute to PikaMath.Math()onceday
add support for Unix Timeonceday
contrubute to PikaStdLib.String()purewind7
Add PLOOC support for pikapython CoreGorgonMeducer
Donate:500¥ (ctypes 相关)蒋太平
fix linux install scriptMaximilian Gerhardt
ch582 package梦程MI
ch582 BSP梦程MI
Donate:EC600X QuecPython Develop board移远通信 武加玉
Performance Point: 900->1400GorgonMeducer
stm32f051r8 BSPunsigned
stm32f407ze BSPunsigned
devc BSPunsigned
TC264D BSPunsigned
PikaVM improvementGorgonMeducer
W801Device package刘延(微信名)
W806 bsp刘延(微信名)
Donate:ESP32C3 5pic,linker启明云端 沧御
Donate:LS1C101 mcu 10pic, LS1c101 develop board,debuger龙芯俱乐部 石南
PikaVSF OS packageversaloon
ESP32C3 BSP沧御
</details> <details><summary> Contribute Log On 2021 </summary>
Contribute logContributer
Donate:ESP32 board、ink lcd*4name(微信名)
Donate:smartloong board龙芯俱乐部 石南
package/STM32F1sjy
package/STM32F103RBBootersjy
bsp/stm32f103rbsjy
QEMU ARM-2D simulation proejctliuduanfei
Donate:GD32E103TB2 2 pic信息牛(微信名)
Rt-thread bsp、port、moduleMeco Jianting Man
Donate:EC600S-CN 4G移远模块
Donate:BL706 board博流智能 bouffalolab
Donate:CM32M101A 开发板孟巍(微信名)
Donate:APM32F030R8 board极海半导体 陈成
Donate:APM32E103VB board极海半导体 陈成
Donate:APEX-Link极海半导体 陈成
corde format,add gitattributesMeco Jianting Man
demo/simulation-keil千帆(微信名)
demo/stm32f103zet6/demo01-led-stm32f103zet6甜航
demo/stm32f103zet6/demo02-led-stm32f103zet6_tworoot甜航
demo/stm32f407zgt/demo01-led-stm32f407vgt甜航
</details>

Star History

Star History Chart