6.1. PikaScript C module overview

    We open the pikascript folder and find that in addition to main.py, there are Device.pyi, PikaObj.pyi and PikaStdLib.pyi in the root of the folder, which correspond to three PikaScript C modules (class package), each .pyi file itself is called the module interface (package interface). A C module can contain several classes that are more related.

    Each PikaScript C module consists of two parts: module interface and module implementation (package implement). Let’s start by opening Device.pyi to see the contents, we will call Device.pyi the Device module interface in the subsequent documentation. Here is the entire contents of Device.pyi.

    As you can see, there are two classes defined in Device.pyi using pyhon standard syntax, the class and the Uart class.

    The LED class defines two methods, the on() method and the off() method, while the class defines the send(data:str) method, the setName(name:str) method, and the printName() method.

    As you can see, all these methods have the feature that instead of being definitions of methods, they are declarations (annotations) of methods, because all method implementations are passed out and none of them are written for implementation. And the method’s entry parameters are all with type declarations. For example, means a data parameter with the type str, i.e. a string type.

    This is because the module implementation of this module is written in C, i.e. the C modules of PikaScript are written with declarations in python syntax and implementations in C. PikaScript’s module development is a hybrid programming technique for interface-oriented programming.

    6.1.2. Importing and calling modules

    Let’s see how to use this module.

    Let’s open main.py in the project, see the name, this file is the entry file for PikaScript.

    The content of main.py is as follows

    Importing an already written C module is very simple, for example, to import the Device module, you just need to import Device, and note that all .py and .pyi files should be placed in the root directory of the pikascript fileshelf.

    The call method uses the form , which is standard Python syntax and does not need much introduction.

    After writing the module calls in main.py, double-click on rust-msc-v0.5.0.exe to pre-compile the PikaScript project, the pre-compiled output file is in the pikascrip-api folder.

    _images/1638582989556-feafe97a-037f-44b2-8f2c-55ddf8f041ea.png

    And PikaMain-api.c and PikaMain.h correspond to a special class that is the main PikaScript class, compiled from main.py.

    https://user-images.githubusercontent.com/88232613/171088880-83247a92-2b1c-4d3f-a075-b4811132e54e.png

    pikaScript.c and pikaScript.h, on the other hand, are initialization functions compiled from main.py. When the initialization functions are run, the startup script is automatically executed.

    In the current main.py, the startup script is written in the outermost method call, which is:

    The compiled pikaScriptInit() initialization function corresponds to: