pikapython/doc/1.三分钟快速上手.md
2021-08-25 15:54:38 +08:00

86 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

在本篇中你将可以在手边没有硬件的情况下对pikascript进行上手测试。
测试使用keil5的仿真工程仿真目标板为stm32f103下载仿真工程即可直接开始测试。
## (1) 下载工程
进入stm32 demo 仓库
(右键在新标签页中打开)
[pikascript-stm32-demo](../../../../pikascript-demo-stm32)
然后下载源码
![MJ8Y8RH0P@ILH~_E}TK5Q57](https://user-images.githubusercontent.com/88232613/130744247-2f71ba72-2d1b-49d9-bbb5-4eda334ec912.png)
(github 页面)
![image](https://user-images.githubusercontent.com/88232613/130744477-e6760afb-99bf-4be0-aa04-8fbe2ea737ec.png)
(gitee 页面)
本篇的测试只需要解压simulation-keil工程即可
![image](https://user-images.githubusercontent.com/88232613/130745409-364d67b8-04a4-45ab-bdd0-c59179419717.png)
直接打开工程
![image](https://user-images.githubusercontent.com/88232613/130745821-864038df-d8b0-41d2-97e8-199815d0d57d.png)
## (2) 运行仿真工程
选择使用仿真器进行调试
![image](https://user-images.githubusercontent.com/88232613/130747706-b912e09f-3f68-495a-a69f-f8f7500b1e4e.png)
编译工程然后进入调试
![3MT68@ }AWJTGAYJA12VG%V](https://user-images.githubusercontent.com/88232613/130747350-70ffa319-f04d-4f26-a75b-61864a19b8d8.png)
打开串口显示面板
![image](https://user-images.githubusercontent.com/88232613/130747952-42073ba1-c4c4-4acb-9495-766cd5731374.png)
运行然后查看输出结果
![image](https://user-images.githubusercontent.com/88232613/130748221-53fff9f6-6427-417d-b95a-3fa52a57eeaf.png)
## (3) 改改脚本看看
用任意编辑器打开main.py推荐vscode 没有vscode用记事本打开也可以
![image](https://user-images.githubusercontent.com/88232613/130748847-477facfb-e16e-4e0e-8876-d66efd0ae48c.png)
以下就是main.py
``` python
# main.py
from PikaObj import *
import Device
import PikaStdLib
led = Device.LED()
uart = Device.Uart()
mem = PikaStdLib.MemChecker()
print('hello wrold')
uart.setName('com1')
uart.send('My name is:')
uart.printName()
print('mem used max:')
mem.max()
print('mem used now:')
mem.now()
```
这个脚本使用标准的python3语法那么如何让这个脚本在单片机里运行呢
事实上pikascript虽然使用python语法但原理上更像是java是半编译半解释型的pikascript的类和方法是需要编译的而方法调用和对象新建/销毁则是在运行时解释的。
编译pikascript分为两步第一步是使用pikascript预编译器将.py文件编译为pikascript-api中的.c和.h文件。
第二步是使用c编译器编译所有的c工程然后下载到单片机里即可。
双击rust-msc-v0.5.0.exe运行pika预编译器值得一提的是这个预编译器是使用rust语言编写的。
![image](https://user-images.githubusercontent.com/88232613/130749341-d12b7985-3685-419c-b9b8-8a09ae6f73d3.png)
为了验证编译的效果我们可以先把pikascript-api文件夹里的文件全部删除然后再运行编译器看看能不能自动生成pikascript-api里面的.c,.h文件。
注意不要把pikascript-api文件夹给删掉了只删除里面的文件即可。