README.mdpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <p align="center">
  2. <img alt="logo" src="document/image/144221063-a85b2cb0-0416-493f-9afb-56cff31e977d.jpg" width="300">
  3. </p>
  4. <h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">PikaPython</h1>
  5. <h4 align="center">Cross platform ultra lightweight embedded Python engine</h4>
  6. <p align="center">
  7. <a href="https://gitee.com/lyon1998/pikapython/stargazers"><img src="https://gitee.com/lyon1998/pikapython/badge/star.svg?theme=gvp"></a>
  8. <a href="https://gitee.com/lyon1998/pikapython/members"><img src="https://gitee.com/lyon1998/pikapython/badge/fork.svg?theme=gvp"></a>
  9. <a href="https://github.com/pikastech/pikapython/stargazers"><img src="https://img.shields.io/github/stars/pikastech/pikapython?style=flat-square&logo=GitHub"></a>
  10. <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>
  11. <a href="https://github.com/pikastech/pikapython/watchers"><img src="https://img.shields.io/github/watchers/pikastech/pikapython?style=flat-square&logo=GitHub"></a>
  12. <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>
  13. <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>
  14. <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>
  15. <a href="https://app.codecov.io/gh/pikasTech/PikaPython"><img src="https://codecov.io/gh/pikasTech/PikaPython/branch/master/graph/badge.svg"> </a>
  16. </p>
  17. <p align="center">
  18. <a href="README_zh.md" >中文页</a> |
  19. <a href="https://whycan.com/f_55.html" >Forum</a> |
  20. <a href="https://pikadoc-en.readthedocs.io/en/latest/index.html" >Documents</a> |
  21. <a href="https://space.bilibili.com/5365336/channel/detail?cid=202329">Videos</a> |
  22. <a href="../../tree/master/bsp#platform-support">BSP</a> |
  23. <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> |
  24. <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> |
  25. <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>
  26. </p>
  27. [![image](document/image/147997370-ff37b6e7-25b2-4174-aa64-c1fb92cede04.png)](https://pikastech.github.io/PikaPython/dev/bench/)
  28. # 1. Abstract
  29. 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.
  30. PikaPython also known as PikaScript and PikaPy.
  31. <img src="https://user-images.githubusercontent.com/88232613/147799704-d79345da-a2b9-4593-b1eb-ff9de127429a.png" width="500"/>
  32. 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.
  33. ![image-20220520233540245](document/image/image-20220520233540245.png)
  34. # Get pikapython:
  35. ## Use Online Project Generator
  36. Generator url:
  37. http://pikascript.com
  38. Note: You need the [Community Edition license (Now Free)](https://www.keil.com/pr/article/1299.htm) to build Keil projects, and the version of Keil should be newer than v5.36.
  39. [![](assets/1644129110261-049ad5bb-21af-40e2-9533-a1c8c86790f1.jpg)](http://pikascript.com)
  40. ## PikaPython studio
  41. PikaPython serial terminal, script download and package menage tool.
  42. [![](assets/115.png)](https://gitee.com/Lyon1998/pikapython/attach_files/1285327/download)
  43. # Quick Start
  44. You can use [simulation project](https://pikadoc-en.readthedocs.io/en/latest/Keil%20%E4%BB%BF%E7%9C%9F%E5%B7%A5%E7%A8%8B.html) to quick start without hardware,or use the out-of-the-box develop board [Pika-Pi—Zero](https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-23991764791.11.37216340XZtYt9&id=738387991209).
  45. ![image](https://user-images.githubusercontent.com/88232613/141252834-93011ca7-f84b-4192-8e61-0e242796f62c.png)
  46. 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).
  47. 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://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-23991764791.12.16f97c58fsLjVk&id=660745643102).
  48. # Related Projects
  49. - ▶️ [pikapython Binding For LVGL](https://github.com/lvgl/lv_binding_pikascript)
  50. - ⭐ [pikapython Bluepill Demo In PlatformIO — Python-like REPL 🐍🔌](https://github.com/maxgerhardt/pikascript-pio-bluepill)
  51. - ⭐ [pikapython Bluepill Demo In GCC 🐍](https://github.com/Chandler-Kluser/pikascript_gcc_bluepill)
  52. - ⏩ [pika_startup_demo](https://gitee.com/kcfkwok/pika_startup_demo) This program demonstrate the 5 startup methods of pikapython.
  53. - 🎮 [PikaPython-OpenHardware](https://gitee.com/Lyon1998/pikapython_openhardware) PikaPython 开源硬件
  54. - 💻 [pikapython-msvc-qt](https://gitee.com/zuto360_460135301/pikapython-msvc-qt) 移植pikapython到windows平台,基于QT,采用MSVC编译器,移植pthread库,支持多线程。
  55. # Document
  56. https://pikadoc-en.readthedocs.io/en/latest/index.html
  57. ## How to contribute
  58. 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
  59. ## Folders
  60. [src](../../tree/master/src) - core code
  61. [bsp](../../tree/master/bsp) - mcu/board support
  62. [port](../../tree/master/port) - OS and package manager
  63. [test](../../tree/master/port/linux/test) - unit test
  64. [document](../../tree/master/document) - developt document
  65. [examples](../../tree/master/examples) - example scripts
  66. [package](../../tree/master/package) - packages and moudles
  67. [pikaCompiler](../../tree/master/tools/pikaCompiler) - pre-compiler write by Rust, used to bind C function to python moudle.
  68. [pikaPackageManager](../../tree/master/tools/pikaPackageManager) - pacakge manager
  69. # 2.Platform support
  70. ## MCU support
  71. !INCLUDE "bsp.mdpp"
  72. ## Board support
  73. !INCLUDE "board.mdpp"
  74. ## OS support
  75. !INCLUDE "os.mdpp"
  76. # 3.Characteristic
  77. ### (1)Run environment
  78. Support run in mcu without OS or file system. Can run in everywhere with **RAM ≥ 4kB** and **FLASH ≥ 64kB**,such as stm32g030, stm32f103c8t6,esp8266.
  79. ### (2)Develop enviroment
  80. #### Support REPL by serial.
  81. <img src="assets/1641178790145-2f026e70-4ba1-4e9a-b05f-c602b2bd8cad.png" width="400" alt="微信交流群"/><br/>
  82. #### Support run and program python scripts by serial.
  83. <img src="document/image/134841230-85de6734-8467-4245-93a5-d452b5022b42.gif" width="400" alt="微信交流群"/><br/>
  84. Support IDEs like Keil, IAR, RT-Thread studio and segger embedded studio to develop C moudle.
  85. Support build tools like CMake, makeFile and Scons.
  86. Zero dependencies, zero configuration, out-of-the-box, easy to integrated into privious C projcet.
  87. Eazy to extern customized C moudles.
  88. Support linux.
  89. ### (3)Syntax
  90. Support subaggregate of python 3 standard syntax.
  91. Support class and method define, encapsulation-inheritance-polymorphism and moudles in `.pyi ` of C module.
  92. <details><summary>More Details About Syntax Support</summary>
  93. |Syntax|Compile-Time|Run-Time|Shell|
  94. |---|---|---|---|
  95. |Module Define |√|-|-|
  96. |Module Import |√|√|√|
  97. |Class Define |√|√|√|
  98. |Class Inherit |√|√|√|
  99. |Method Define |√|√|√|
  100. |Method Override |√|√|√|
  101. |Method Invoke |√|√|√|
  102. |Argument Define |√|√|√|
  103. |Argument Assignment |√|√|√|
  104. |Object New |√|√|√|
  105. |Object Free |√|√|√|
  106. |Object Nest |√|√|√|
  107. |Control flow |√|√|√|
  108. !INCLUDE "syntax.mdpp"
  109. </details>
  110. ### (4)Develop standard.
  111. Readability first, nearly never use macro function and global argument.
  112. Complete unit testes based on google test.
  113. # 4.Both talk and show the code:
  114. ## E-mail: liang6516@outlook.com
  115. # 5.Demo
  116. The scripts in demos are in the [examples](examples) folder.
  117. ## Demo 01 GPIO
  118. <details><summary>View Code</summary>
  119. ``` python
  120. import PikaStdLib
  121. import STM32G0
  122. mem = PikaStdLib.MemChecker()
  123. io1 = STM32G0.GPIO()
  124. time = STM32G0.Time()
  125. io1.setPin('PA8')
  126. io1.setMode('out')
  127. io1.enable()
  128. io1.low()
  129. print('hello pikapython')
  130. print('mem.max :')
  131. mem.max()
  132. print('mem.now :')
  133. mem.now()
  134. while True:
  135. io1.low()
  136. time.sleep_ms(500)
  137. io1.high()
  138. time.sleep_ms(500)
  139. ```
  140. </details>
  141. ![Hnet-image (2)](document/image/132943428-f2b365ca-140e-42f4-936c-db6a7d9f8dee.gif)
  142. ## Demo 02 USART
  143. <details><summary>View Code</summary>
  144. ``` python
  145. import PikaStdLib
  146. import STM32G0
  147. time = STM32G0.Time()
  148. uart = STM32G0.UART()
  149. uart.setId(1)
  150. uart.setBaudRate(115200)
  151. uart.enable()
  152. while True:
  153. time.sleep_ms(500)
  154. readBuff = uart.read(2)
  155. print('read 2 char:')
  156. print(readBuff)
  157. ```
  158. </details>
  159. ![Hnet-image (3)](document/image/132943365-0f7059b3-4f9d-4989-a5ec-2cce72b0cc96.gif)
  160. ## Demo 03 ADC
  161. <details>
  162. <summary>View Code</summary>
  163. ``` python
  164. import PikaStdLib
  165. import STM32G0
  166. time = STM32G0.Time()
  167. adc1 = STM32G0.ADC()
  168. adc1.setPin('PA1')
  169. adc1.enable()
  170. while True:
  171. val = adc1.read()
  172. print('adc1 value:')
  173. print(val)
  174. time.sleep_ms(500)
  175. ```
  176. </details>
  177. ![mmexport1631351523907](document/image/132944185-0a01b1ba-8cf7-4f9f-9d73-fe9cbcd52f0b.png)
  178. ## Demo 04 PWM output
  179. <details><summary>View Code</summary>
  180. ``` python
  181. import PikaStdLib
  182. import STM32G0
  183. time = STM32G0.Time()
  184. pwm = STM32G0.PWM()
  185. pwm.setPin('PA8')
  186. pwm.setFrequency(2000)
  187. pwm.setDuty(0.5)
  188. pwm.enable()
  189. while True:
  190. time.sleep_ms(500)
  191. pwm.setDuty(0.5)
  192. time.sleep_ms(500)
  193. pwm.setDuty(0.001)
  194. ```
  195. </details>
  196. ## Demo 05 RGB
  197. <details><summary>View Code</summary>
  198. ``` python
  199. import STM32G0
  200. import PikaPiZero
  201. import PikaStdLib
  202. rgb = PikaPiZero.RGB()
  203. mem = PikaStdLib.MemChecker()
  204. rgb.init()
  205. rgb.enable()
  206. print('hello 2')
  207. print('mem used max:')
  208. mem.max()
  209. while True:
  210. print('flowing')
  211. rgb.flow()
  212. ```
  213. </details>
  214. ## Demo 06 Snake(Need LCD)
  215. <details>
  216. <summary>View Code</summary>
  217. ``` python
  218. from PikaObj import *
  219. import PikaStdLib
  220. import PikaPiZero
  221. import STM32G0
  222. # hardware init
  223. lcd = PikaPiZero.LCD()
  224. lcd.init()
  225. lcd.clear('white')
  226. key = PikaPiZero.KEY()
  227. key.init()
  228. time = STM32G0.Time()
  229. x_max = 120
  230. y_max = 150
  231. # snake init
  232. s = PikaPiZero.Point()
  233. w = 9
  234. h = 9
  235. s.x = 50
  236. s.y = 10
  237. len = 0
  238. while len < 3:
  239. b = s
  240. i = 0
  241. while i < len:
  242. b = b.next
  243. i = i + 1
  244. b.next = PikaPiZero.Point()
  245. b.next.x = b.x - 10
  246. b.next.y = b.y
  247. b.next.prev = b
  248. len = len + 1
  249. # ring link
  250. b.next = s
  251. s.prev = b
  252. i = 0
  253. b = s
  254. while i < len:
  255. lcd.fill(b.x, b.y, w, h, 'blue')
  256. b = b.next
  257. i = i + 1
  258. print('snake lengh')
  259. print(len)
  260. # fruit init
  261. f = PikaPiZero.Point()
  262. f.x = 30
  263. f.y = 20
  264. lcd.fill(f.x, f.y, w, h, 'green')
  265. # memory check
  266. mem = PikaStdLib.MemChecker()
  267. print('mem used max:')
  268. mem.max()
  269. # main loop
  270. d = 0
  271. isUpdate = 1
  272. isEat = 0
  273. while True:
  274. if isUpdate:
  275. # isUpdate = 0
  276. # check eat fruit
  277. if f.x == s.x and f.y == s.y:
  278. # have eat fruit
  279. isEat = 1
  280. f.x = f.x + 30
  281. if f.x > x_max:
  282. f.x = f.x - x_max
  283. f.y = f.y + 30
  284. if f.y > y_max:
  285. f.y = f.y - y_max
  286. lcd.fill(f.x, f.y, w, h, 'green')
  287. # move snake by the direction
  288. if d == 0:
  289. x_new = s.x + 10
  290. y_new = s.y
  291. if x_new > x_max:
  292. x_new = 0
  293. elif d == 1:
  294. x_new = s.x
  295. y_new = s.y - 10
  296. if y_new < 0:
  297. y_new = y_max
  298. elif d == 2:
  299. x_new = s.x
  300. y_new = s.y + 10
  301. if y_new > y_max:
  302. y_new = 0
  303. elif d == 3:
  304. x_new = s.x - 10
  305. y_new = s.y
  306. if x_new < 0:
  307. x_new = x_max
  308. if isEat:
  309. isEat = 0
  310. b_new = PikaPiZero.Point()
  311. b_new.x = x_new
  312. b_new.y = y_new
  313. b_new.prev = s.prev
  314. b_new.next = s
  315. s.prev.next = b_new
  316. s.prev = b_new
  317. s = b_new
  318. len = len + 1
  319. print('snake lengh')
  320. print(len)
  321. print('mem used max:')
  322. mem.max()
  323. # drow the snake and fruit
  324. # clear last body
  325. lcd.fill(s.prev.x, s.prev.y, w, h, 'white')
  326. # new body
  327. s.prev.x = x_new
  328. s.prev.y = y_new
  329. # head is last body
  330. s = s.prev
  331. lcd.fill(s.x, s.y, w, h, 'blue')
  332. b = s
  333. i = 0
  334. # scan key
  335. key_val = key.get()
  336. if key_val == 0:
  337. d = 0
  338. isUpdate = 1
  339. elif key_val == 1:
  340. d = 1
  341. isUpdate = 1
  342. elif key_val == 2:
  343. d = 2
  344. isUpdate = 1
  345. elif key_val == 3:
  346. d = 3
  347. isUpdate = 1
  348. ```
  349. </details>
  350. ![image](https://user-images.githubusercontent.com/88232613/150269059-1fe3d62d-0c1b-40f4-9298-0d787f476a20.png)
  351. 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.
  352. ## About the custom extern module ?
  353. 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.
  354. ## Boxes~
  355. ![Hnet-image (7)](document/image/132945282-bfd310df-8063-456d-b90c-6b798a2c8ed5.gif)
  356. ## Rotating suns~
  357. ![Hnet-image (6)](document/image/132945107-e473a2cc-9fbc-47f9-aaed-a28d3ad1048c.gif)
  358. # 6.Test and develop Core
  359. ## Test core in docker (recommend)
  360. [get start with docker](https://pikadoc-en.readthedocs.io/en/latest/get-start_linux.html)
  361. # ✨ Contributors
  362. <a href="https://github.com/pikastech/pikapython/graphs/contributors">
  363. <img src="https://contrib.rocks/image?repo=pikastech/pikascript" />
  364. </a>
  365. ## [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)
  366. <div class="log"></div>
  367. ### Contribute Log On 2023
  368. | Contribute log | Contributer |
  369. | --- | --- |
  370. !INCLUDE "contributor2023.mdpp"
  371. <details><summary> Contribute Log On 2022 </summary>
  372. | Contribute log | Contributer |
  373. | --- | --- |
  374. !INCLUDE "contributor2022.mdpp"
  375. </details>
  376. <details><summary> Contribute Log On 2021 </summary>
  377. | Contribute log | Contributer |
  378. | --- | --- |
  379. !INCLUDE "contributor2021.mdpp"
  380. </details>
  381. ## Star History
  382. [![Star History Chart](https://api.star-history.com/svg?repos=pikastech/pikascript&type=Date)](https://star-history.com/#pikastech/pikascript&Date)