2.1. How to Get Started with PikaScript using KEIL Simulator

    Open the pikascript official website

    Select simulation-keil and click “Start Generation”

    Unzip the downloaded zip archive and open the project

    _images/130745821-864038df-d8b0-41d2-97e8-199815d0d57d.png

    Make sure you have select the simulator as the debugging target

    Compile and debug:

    https://user-images.githubusercontent.com/88232613/171086774-faf8a9b8-7ad8-4241-aad5-34e49ae9e01b.png

    Once entering the debug session, make sure you have opened the serial windows as shown below:

    run and check the output:

    https://user-images.githubusercontent.com/88232613/171086180-ddeec7eb-39c6-47ec-bcd2-4d7380e8b703.png

    Python scripts can be run interactively by typing them directly in the UART window.

    Open the in any editor, e.g. vscode:

    https://user-images.githubusercontent.com/88232613/171086868-3ac1b9f6-c59f-4306-9b43-edf45844a203.png

    In main.py, you might see something similar to:

    This script uses standard python3 syntax. Suppose we have already modified this script, so how to run it on the device?

    The interesting part is, pikascript uses a method similar to java, i.e. it is semi-compiled and semi-interpreted. For example, the pikascript compiler compiles classes and methods, while PikaVM interprets method-calls and object-creation/destruction at runtime.

    The pikascript compilation is a two-step process:

    1. Using pikascript compiler to compile the .py files into .c and .h files and store them in the pikascript-api folder.

    2. Using the ordinary c compiler to compile all the c source files and generate an executable image for the target device.

    Double-click rust-msc-vxx.yy.zz.exe to run the pika precompiler which is written in Rust.

    NOTE: Here xx.yy.zz is the version number.

    If you want to ensure that the updated script is compiled as expected, please

    1. delete all files in the pikascript-api folder,

    2. check whether the new .c and .h files have been generated or not.

    IMPORTANT: Please do NOT remove the pikascript-api folder but only the files inside.

    Here is an example that shows the *.c and files generated in the pikascript-api folder

    _images/130750476-eaffce03-caeb-40b3-9841-550034fa191a.png

    Now, let’s modify main.py as a practice:

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

    As you can see, we have added 4 new lines to the main.py. Let’s compile and run:

    Compile pikascript-api

    Compile the keil project and enter the debugging session:

    _images/130751539-aa0bdb82-750f-4f98-8f6f-02d653dda64a.png

    run and observe the output

    As shown above, there are 3 new lines in the output, indicating that our modification works as expected.

    That’s all, enjoy!!