Usage of module
What is module?
As the number of code is growing,the code in the file will be longer and longer,harder and harder to understand.
To compile code that can be protected,we divide many functions into groups putting into different files.In Python,a file .py is called a module.
What’s the profit of module?It is simple to copy code.If I write a module,the same is to you,thus we have two modules.We put these modules together,everyone of us can write much less code.
How to use module?
import pyb
red_led = pyb.LED(1)
red_led.on()
import pyb is to import module pyb.Through sentence import,you can import this module.
And sentence from xxx import ooo means import class ooo through module xxx,or import function ooo through module xxx.For example,the procedure above can be written like this:
from pyb import LED
red_led = LED(1)
red_led.on()
This is importing class LED through module pyb.
How to add self-defining module?
We mentioned it before,OpenMV has file system.
The root directory of file system has main.py,current directory run by code is root directory.
Thus we copy the file of module to USB disk of OpenMV.As is shown in the picture.
Pid.py here is just an example.
I copied pid.py to the root directory of USB disk.In that way,in the procedure
import pid
We can import module pid.Or through
from pid import PID
This is importing class PID.
So how to compile a module like this?See DIY a module