7.8. Module clipping

PikaPython 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.


7.8.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:


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.


Unimport the PikaPiZero module


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.


Then cancel the import of the STM32G0 module


Code size reduced to 36K


7.8.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.


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


Then change import STM32G0 in main.py to import myDevice


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