12. Overview of PikaScript modules

We still take Keil’s simulation project as an example. If you haven’t obtained the simulation project, please refer to 1. Quick Start in Three Minutes

12.1. PikaScript modules and module interface

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

_images/1638582993068-0a8afe28-baa2-41ad-bac1-6626d50192ad.png

Each PikaScript module consists of two parts: the module interface and the package implement. Let’s open Device.py to see the contents, and we will refer to Device.py as the Device module interface in the subsequent documentation. Here is the entire contents of Device.py.

  1. # Device.py
  2. from PikaObj import *
  3. class LED(TinyObj):
  4. def on(self):
  5. pass
  6. def off(self):
  7. pass
  8. class Uart(TinyObj):
  9. def send(self, data:str):
  10. pass
  11. def setName(self, name:str):
  12. pass
  13. def printName(self):
  14. pass

As you can see, Device.py uses pyhon standard syntax to define two classes, LED class and Uart class, both of which inherit from TinyObj.

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

As you can see, all these methods have one feature: instead of being method definitions, they are method declarations (annotations), because all method implementations are passed out and not written. Also, the method entry parameters are all type-declared. For example, data:str means a data parameter of type str, which is a string type.

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

However, when you use an existing module, you don’t need to know the module implementation, you only need to know the module interface to use the module.

12.2. Import and call the module

Let’s see how to use this module.

We open main.py in our project, see the name, this file is the entry file for PikaScript.

The content of main.py is as follows

  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()

Importing a module that has already been written is very simple, for example, to import the Device module, you just need to import Device, it is important to note that all .py files should be placed in the root of the pikascript file shelf.

The call method uses the form uart.setName(’com’), 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

The pika pre-compiler generates .h declaration files and -api.c constructor files for the imported modules. The filenames start with the module name and each class corresponds to one .h file and one -api.c file.

_images/1638582990457-2540db61-f185-4100-8b63-4d6d599c3b0e.png

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

_images/1638582990858-10783588-5ff0-469e-b64d-50e56e2357bc.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.

_images/1638582992822-6c4a7f39-a379-4c66-991a-1935ec3bfa7a.png

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

  1. led = Device.LED()
  2. uart = Device.Uart()
  3. mem = PikaStdLib.MemChecker()
  4. print('hello wrold')
  5. uart.setName('com1')
  6. uart.send('My name is:')
  7. uart.printName()
  8. print('mem used max:')
  9. mem.max()
  10. print('mem used now:')
  11. mem.now()

The compiled pikaScriptInit() initialization function corresponds to the :

  1. PikaObj * pikaScriptInit(){
  2. PikaObj * pikaMain = newRootObj("pikaMain", New_PikaMain);
  3. obj_run(pikaMain,
  4. "\n"
  5. "led = Device.LED()\n"
  6. "uart = Device.Uart()\n"
  7. "mem = PikaStdLib.MemChecker()\n"
  8. "\n"
  9. "print('hello wrold')\n"
  10. "uart.setName('com1')\n"
  11. "uart.send('My name is:')\n"
  12. "uart.printName()\n"
  13. "print('mem used max:')\n"
  14. "mem.max()\n"
  15. "print('mem used now:')\n"
  16. "mem.now()\n"
  17. "\n"
  18. "\n");
  19. return pikaMain;
  20. }