FEATHER-CHARGER-MODULE-FROZEN

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

Dans la section précédentes, nous avons appris a créer et charger des modules et paquet/package Python sur notre carte MicroPython. C'est une façon facile et rapide de charger des modules, cependant, il y a également des limitations importantes concernant le chargement et l'exécution de module et paquet depuis le système de fichier. La plus grande limitation concerne concerne l'interpréteur Python qui doit charger le code et en faire l'analyse syntaxique (Wikipedia - aussi dit "parsing" en anglais). Cette opération requière du temps mais aussi de la mémoire, mémoire très limitée sur les microcontrôleurs. Vous pourriez même trouver des codes sources tellement grand qu'ils ne pourraient pas être chargé par l'interpréteur de la carte MicroPython!

Il existe une façon de pré-compiler les modules pour qu'ils soient "inclus" dans le firmware MicroPython et qu'ils utilisent moins de mémoire. MicroPython peut comprimer presque tous les éléments du langage Python dans quelques kilooctets de mémoire en "gelant" le code Python. Cela permet de stocker un code sous un format plus efficace appelé byte-code (du Pyhton déja interprété). Une autre approche permet même de compiler ce code en instruction native (pour le microcontrôleur de la carte). Ces deux dernières approches étant beaucoup plus efficace que des fichiers Python.

Une fois le code gelé ("frozen" en anglais), il peut être rapidement chargé et interprété par MicroPython sans besoin de mémoire et temps de traitement supplémentaire.

Avec MicroPython, vous pouvez geler vos propres modules et les ajouter au firmware en réalisant une compilation personnalisée du Firmware MicroPython (pour votre carte).

This allows you to write more complex modules than you might be able to run directly from .py Python code files as previously shown.

Be aware that freezing modules is a bit advanced and requires you to make a custom build of MicroPython's firmware. For some boards like the ESP8266 it's relatively easy to build MicroPython firmware, but for others it might require more advanced knowledge of setting up and using compiler toolchains, etc.

Before you look at freezing modules make sure you can compile MicroPython firmware for your board. For the ESP8266 check out this handy guide on how to compile MicroPython firmware using a special Linux-based virtual machine. For other boards like the pyboard, WiPy, etc. for now you'll need to check the MicroPython source to see how to setup their toolchains and compile firmware.

Also be aware freezing modules is somewhat new for boards like the ESP8266 and the process might change over time. If the steps below have issues, check the latest documentation, code on GitHub, and MicroPython forums to see if more up to date information on freezing modules is availble.

For this guide we'll look specifically at the ESP8266 and how to add a frozen module to the board's MicroPython firmware. Start by looking at the source of the MicroPython ESP8266 firmware. If you're using the Vagrant virtual machine from the previously mentioned firmware building guide then you'll want to enter the VM with SSH and navigate to the ~/micropython/esp8266 folder. Notice there are two subdirectories, scripts and modules:

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

There's an important difference between these two folders. The scripts folder is for Python code that will be 'baked-in' to the firmware but not frozen into more efficient and smaller code. Everything in the scripts folder is just stored as the raw Python code in the board's flash memory. This saves you from having to copy that code onto the board's filesystem, but doesn't save a lot of memory or processing time.

The modules folder is for Python code that will be frozen into more efficient bytecode. This is where you want to place scripts that you'd like freeze to save memory.

Try adding a new frozen module as a quick test. Inside the modules folder create a test.py file and add the following code to it:

def add(a, b):
        return a + b

def subtract(a, b):
        return a - b

Now rebuild the ESP8266 MicroPython firmware. From inside the VM's ~/micropython/esp8266 folder run:

make

Notice in the output the MPY modules/test.py line, this shows the test.py module was picked up by the build and frozen into bytecode:

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

If you don't see this line for your module then try running the make clean command and then make command again.

Now copy out the newly built firmware and flash it to the ESP8266. Remember you're now using a development build so debug output will be enabled and features like the WebREPL must be manually started.

Connect to the REPL of the board with the new firmware and try using the test module's functions by running:

import test
test.add(1, 1)
test.subtract(1, 1)

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com

You should see the module import and functions work just like if the .py file was on the filesystem!

You can also freeze Python packages inside the modules folder. Just create a directory for each package and put inside it an __init__.py and other source files for the package.

Using the scripts folder to save Python module sources in flash memory is just like using the modules folder above. However note with the scripts folder you cannot put packages inside it, only single .py source files will currently work.

Remember the scripts folder is only for scripts that will be saved as-is to flash memory. The modules folder is for freezing modules and packages so they're in flash memory and more efficient to load and store. As you develop your own complex MicroPython scripts and modules consider freezing them in the MicroPython firmware to reduce memory usage and increase performance!


Source: MicroPython Basics: Loading Modules
Créé par Tony DiCola pour AdaFruit Industries.

Traduit par Meurisse D. pour MCHobby

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com