4.1. Module Import

    But don’t worry, PikaScript already helps you to import modules easily with its official tools, all you need to do is to write a line , just like you do with Python on PC.

    The only difference with Python for PC is that you need to run the pre-compiler provided by PikaScript once (no complicated parameters and options, just double-click to run) before you can compile your PikaScript project with the compiler.

    PikaScript supports importing multiple Python files as modules, and there is no need to port the filesystem inside the MCU (if you want to base it on a filesystem, you can, of course).

    PikaScript’s pre-compiler converts Python files into bytecode and packages them into a library right on the PC development machine, just like C.

    This eliminates the need for a filesystem in a MCU with few resources (usually 20kB of ROM).

    On the other hand, if you want to quickly try PikaScript on a new platform, you don’t need to go through the effort of porting the filesystem for the new platform and then interfacing the filesystem with PikaScript.

    (Note that a kernel version of not less than v1.8.0 is required)

    We still use keil’s emulation project as our experiment platform, so that we can experiment quickly without hardware.

    First, refer to keil’s emulation project documentation to get the project.

    Then create a new Python file test.py in the pikascript_simulation-keil/pikascript/ directory (all Python modules should be placed in this directory).

    Next, introduce test.py inside main.py and test the functions mytest() and add() that we defined in test.py

    Then, if you compile directly inside the keil project, you will see that the PikaScript Compiler message appears before you start compiling the .c file, including the compiled test.py.

    _images/image-20220620175646395.png

    This is because the PikaScript precompiler has been automatically run, a Keil-supplied setting that executes a script before compilation begins, including running the PikaScript precompiler.

    Then we start debugging the run and open the serial window to see the results

    _images/image-20220620175959680.png

    If you are interested in the principle, you can watch the .

    4.1.2. Importing C modules

    A C module is a module that is implemented in C at the bottom, but can still be called with Python.

    A C module named <module> usually consists of a <module>.pyi file (a python interface file) and the pikascript-lib/<module> folder.

    PikaScript imports C modules in the same way as Python modules, by directly and then running a pre-compile.

    • All .c files in the pikascript-lib/<module> folder

    4.1.2.1. Experiment

    We are still using the keil emulation project as our experimentation platform.

    We introduce the PikaStdData.pyi C module in main.py.

    We open PikaStdData.pyi to see the classes and functions provided by this C module.

    You can see that there is a List class inside.

    Introduce PikaStdData in main.py and create a new object with the List class, then test the append() method and the method of List.

    When compiling, you can see that the PikaScript pre-compiler binds the PikaStdData C module to the project.

    Running the simulation you can see the result

    _images/image-20220620191048505.png

    Please refer to the documentation for making C modules for details.