6.3. Module clipping

PikaScript module, except PikaStdLib standard library, all other modules support one-click cropping

As shown in the CH32V103 driver module in the figure below, the modules that are not needed can be directly cut out. If there are several classes in a module that need to be used, can fine-cutting be done by class? This is also possible, which will be introduced later.

https://user-images.githubusercontent.com/88232613/171089804-09420efd-4185-48c4-a61a-8eb82abb56a1.pngimage

6.3.1. Cut by module

It is very simple to trim according to the module. Just delete the import statement in main.py, and the modules that are not imported will be automatically trimmed by the precompiler.

Taking the stm32g030c8 project as an example, the default main.py is as follows:

_images/1639281734520-d63c222e-6c15-46f6-9349-2aae5e3a22a1.png

The first line is to import the base object. The base object is provided by the kernel, does not occupy the module space, and does not need to be clipped. The second line is the standard library and cannot be trimmed. The third row of STM32G0 chip modules and the fourth row of the on-board resource modules of the PikaPiZero development board can be cut. Compile and run, and see that the code size is 48k+3k, about 51K.

_images/1639281670087-0edcd6a5-c1ec-4eb7-aa13-b4af6f856774.png

Unimport the PikaPiZero module

_images/1639281716434-51e185a4-bbbc-4fe6-8867-9834b0973da1.png

Then precompile and compile the result: It can be seen that the code size has been reduced to 46K, indicating that the module has been successfully cut.

_images/1639281815845-56657902-ab07-409e-9a3f-f0197370c37c.png

Then cancel the import of the STM32G0 module

_images/1639281843772-333666fe-d348-4616-8f39-5ab63500e3c7.png

Code size reduced to 36K

_images/1639281899582-c0e013e7-9e75-4faf-8c68-d6c2193a030f.png

6.3.2. Cut by class

Using the inheritance function of the module, you can fine-tune according to the class. Modules that are directly imported in main.py are in a runtime ready state, so all classes will be added to the project. For modules that are indirectly imported by other files, precompiled can determine which ones will not be used, so only used classes will be added to the project.

In this way, we can create a new module, inherit the required classes from the modules that need to be used, and then only import the newly created module, you can cut out the classes that are not needed in the module. .

For example, there are 7 classes of GPIO, Time, ADC, UART, PWM, IIC, and lowLevel in STM32G0, and I only use the GPIO class.

_images/1639282316919-0450c010-004a-4d84-891a-14a6d0537e11.png

You can create a new myDevice module, and then inherit only the GPIO class from STM32G0.

_images/1639282420803-e73a65a4-2cd2-4bec-bb88-2cb2eae8cede.png

Then change import STM32G0 in main.py to import myDevice

_images/1639282444794-6ca987a5-05f6-4377-af99-718985fae914.png

It can be seen that compared to using the complete STM32G0 module, the code size is reduced to 43K

_images/1639282647369-def83056-27c9-4d35-82f8-2720d9ae0767.png