4. keil simulation project

In this article, you will be able to get started with pikascript without the hardware at hand. The test uses the simulation project of keil5, the simulation target board is stm32f103, and the test can be started directly by downloading the simulation project.

4.1. Create project

Enter pikascript official website http://pikascript.com Select simulation-keil and click “Start Generation” _images/1644128841425-378e4391-426d-4dc3-bb2d-934e8facd22e.png Unzip the downloaded zip archive and open the project _images/130745821-864038df-d8b0-41d2-97e8-199815d0d57d.png

4.2. Run the simulation project

Choose to use the emulator for debugging _images/130747706-b912e09f-3f68-495a-a69f-f8f7500b1e4e.png

Compile the project and then enter debugging _images/130747350-70ffa319-f04d-4f26-a75b-61864a19b8d8.png Open the serial display panel _images/130747952-42073ba1-c4c4-4acb-9495-766cd5731374.png run and see the output _images/130748221-53fff9f6-6427-417d-b95a-3fa52a57eeaf.png

4.3. Change the script to see

Open main.py with any editor, vscode is recommended, you can also open it with Notepad without vscode _images/130748847-477facfb-e16e-4e0e-8876-d66efd0ae48c.png The following is main.py

  1. # main.py
  2. from PikaObj import *
  3. import Device
  4. import PikaStdLib
  5. led = Device.LED()
  6. uart = Device.Uart()
  7. mem = PikaStdLib.MemChecker()
  8. print('hello wrold')
  9. uart.setName('com1')
  10. uart.send('My name is:')
  11. uart.printName()
  12. print('mem used max:')
  13. mem.max()
  14. print('mem used now:')
  15. mem.now()

This script uses standard python3 syntax, so how to make this script run in the microcontroller? ​

In fact, although pikascript uses python syntax, it is more like java in principle. It is semi-compiled and semi-interpreted. Pikascript classes and methods need to be compiled, while method calls and object creation/destruction are interpreted at runtime. . ​

Compiling pikascript is a two-step process, the first is to use the pikascript precompiler to compile the .py files into .c and .h files in pikascript-api.

The second step is to use the c compiler to compile all the c files, and then download them into the microcontroller.

Double-click rust-msc-v0.5.0.exe to run the pika precompiler. It is worth mentioning that this precompiler is written in the rust language. _images/130749341-d12b7985-3685-419c-b9b8-8a09ae6f73d3.png In order to verify the effect of compilation, we can delete all the files in the pikascript-api folder first, and then run the compiler to see if the .c and .h files in pikascript-api can be automatically generated.

Be careful not to delete the pikascript-api folder, just delete the files inside.

The following are the .c,.h files generated by pikascript-api _images/130750476-eaffce03-caeb-40b3-9841-550034fa191a.png Next, let’s modify main.py to see the effect

  1. from PikaObj import *
  2. import Device
  3. import PikaStdLib
  4. led = Device.LED()
  5. uart = Device.Uart()
  6. mem = PikaStdLib.MemChecker()
  7. print('hello wrold')
  8. uart.setName('com1')
  9. uart.send('My name is:')
  10. uart.printName()
  11. print('mem used max:')
  12. mem.max()
  13. print('mem used now:')
  14. mem.now()
  15. # new code start
  16. print('add new code start')
  17. uart.setName('com2')
  18. uart.printName()
  19. print('add new code end')
  20. # new code end

We added 4 new lines of script below main.py, let’s compile and run to see the effect. Compile pikascript-api _images/130751195-40944d60-7d56-48a9-9f47-cab87d77d5a8.png Compile the keil project and then enter the debugging _images/130751539-aa0bdb82-750f-4f98-8f6f-02d653dda64a.png run and watch the output _images/130751653-cad627c2-367c-45a6-8c5f-686c7514df3c.png We found that there are 3 more lines of output, indicating that the compilation runs smoothly. Well, the three-minute quick start of pikaScript is over here.