FEATHER-M0-MicroPython-bibliotheque

De MCHobby - Wiki
Révision datée du 13 décembre 2017 à 21:02 par Admin (discussion | contributions) (Page créée avec « {{FEATHER-M0-EXPRESS-NAV}} {{traduction}} Part of what makes CircuitPython so awesome is its ability to store code separately from the firmware itself. Storing code sepa... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
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.

Part of what makes CircuitPython so awesome is its ability to store code separately from the firmware itself. Storing code separately from the firmware makes it easier to update both the code you write and the libraries you depend. So, instead of compiling libraries in, you simply place them into your lib directory on the CIRCUITPY drive.

Your board may ship with a lib folder already, its in the base directory of the drive. If not, simply create the folder yourself.

FEATHER-M0-MicroPython-bibliotheque-01.png
Crédit: AdaFruit Industries www.adafruit.com

CircuitPython libraries work in the same was as regular Python modules so the <a href="https://docs.python.org/3/tutorial/modules.html">Python docs</a> are a great reference for how it all should work. In Python terms, we can place our library files in the lib directory because its part of the Python path by default.

One downside of this approach of separate libraries is that they are not built in. To use them, one needs to copy them to the CIRCUITPY drive before they can be used. Fortunately, we provide a bundle full of our libraries.

Our bundle and releases also feature optimized versions of the libraries with the .mpy file extension. These files take less space on the drive and have a smaller memory footprint as they are loaded.

<a href="#installing-the-bundle" class="anchor-link"></a>Installing the bundle

We're constantly updating and improving our libraries, so we don't (at this time) ship our CircuitPython boards with the full library bundle. Instead, you may find example code that depends on (import) libraries. Some of these libraries may be available from us at Adafruit, some may be written by community members!

Either way, once you get past the most basic blink scripts, you'll want to know how to get libraries on board. So, lets take a look at this silly example below which uses a SI7021 I2C temperature sensor.

<code class="prettyprint lang-python">import adafruit_si7021
import busio
import board

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_si7021.SI7021(i2c)
print("Temperature:", sensor.temperature)
print("Humidity:", sensor.relative_humidity)
</code>

After saving that as code.py on the drive we see the status NeoPixel flashes that an error has occurred. Opening up the serial console we see that an ImportError has occurred.

FEATHER-M0-MicroPython-bibliotheque-02.png
Crédit: AdaFruit Industries www.adafruit.com

It says that no module exists named adafruit_si7021. Thats the library we need to download! Since we bought the sensor from Adafruit its likely there is a library for in <a href="https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/latest">the official Adafruit bundle</a>. If its not an Adafruit part or its missing, we can also check <a href="https://github.com/adafruit/CircuitPython_Community_Bundle/releases/latest">the Community bundle</a> which has libraries contributed by the community.

Visiting the bundle release page will show us information on the latest release including a list of all the versions of the included drivers. Scrolling to the bottom of the page will reveal the downloads. We'll download the first zip file which starts with adafruit-circuitpython-bundle.

FEATHER-M0-MicroPython-bibliotheque-03.png
Crédit: AdaFruit Industries www.adafruit.com

FEATHER-M0-MicroPython-bibliotheque-04.png
Crédit: AdaFruit Industries www.adafruit.com

After downloading the zip, extract its contents. This is usually done by double clicking on the zip. On Mac OSX as I'm using, it places the file in the same directory as the zip. I usually sort my Downloads by file data so the lib directory that was contained in the zip ends up next to the zip file.

FEATHER-M0-MicroPython-bibliotheque-05.png
Crédit: AdaFruit Industries www.adafruit.com

Express Boards

If you are using a Feather M0 Express, Metro M0 Express or Circuit Playground Express (e.g. "Express" board) your CircuitPython board comes with 2 MB of Flash storage. This is plenty of space for all of our library files so we recommend you just install them all! (If you have a Gemma M0 or Trinket M0 or other non-Express board, skip down to the next section)

On Express boards, the lib directory can be copied directly to the CIRCUITPY drive.

Just drag the entire lib folder into the CIRCUITPY drive, and 'replace' any old files if your operating system prompts you

You're done! You can go back to coding your project :) The rest of this page is for non-Express boards

Non-Express Boards

If you have a Gemma M0 or Trinket M0, your internal storage is from the chip itself. So, this board doesn't have enough space for all of the libraries. If you try to copy over the entire lib folder you'll overwhelm your store.

Instead, we'll copy over just what we need as we need it. In the lib folder there is an adafruit_si7021.mpy file. That matches the missing module! Python imports modules based on the filename so they will always match up. Lets drag it over. If this works, skip to <a href="https://learn.adafruit.com/adafruit-gemma-m0/installing-libraries#continuing-after-copy">the next section</a>. Keep reading if you have an error.

FEATHER-M0-MicroPython-bibliotheque-06.png
Crédit: AdaFruit Industries www.adafruit.com

<a href="#non-express-boards-out-of-space" class="anchor-link"></a>Non-Express boards - Out of space?

The file system on the board is very tiny. (Smaller than an ancient floppy disk.) So, its likely you'll run out of space but don't panic! There are a couple ways to free up space.

The board ships with the Windows 7 serial driver too! Feel free to delete that if you don't need it or have already installed it. Its ~12KiB or so.

Delete something!

The simplest way of freeing up space is to delete files from the drive. Perhaps there are libraries in the lib that you aren't using anymore or test code that isn't in use.

Use tabs

One unique feature of Python is that the indentation of code matters. Usually the recommendation is to indent code with four spaces for every indent. In general, we recommend that too. However, one trick to storing more human-readable code is to use a single tab character for indentation. This approach uses 1/4 of the space for indentation and can be significant when we're counting bytes.

Mac OSX loves to add extra files.

FEATHER-M0-MicroPython-bibliotheque-07.png
Crédit: AdaFruit Industries www.adafruit.com

Luckily you can disable some of the extra hidden files that Mac OSX adds by running a few commands to disable search indexing and create zero byte placeholders. Follow the steps below to maximize the amount of space available on OSX:

Prevent & Remove Mac OSX Hidden Files

First find the volume name for your board.  With the board plugged in run this command in a terminal to list all the volumes:

ls -l /Volumes</pre>

Look for a volume with a name like CIRCUITPY (the default for CircuitPython).  The full path to the volume is the /Volumes/CIRCUITPY path.

Now follow the <a href="http://apple.stackexchange.com/questions/6707/how-to-stop-os-x-from-writing-spotlight-and-trash-files-to-memory-cards-and-usb/7135#7135">steps from this question</a> to run these terminal commands that stop hidden files from being created on the board:

mdutil -i off /Volumes/CIRCUITPY
cd /Volumes/CIRCUITPY
rm -rf .{,_.}{fseventsd,Spotlight-V*,Trashes}
mkdir .fseventsd
touch .fseventsd/no_log .metadata_never_index .Trashes
cd -

Replace /Volumes/CIRCUITPY in the commands above with the full path to your board's volume if it's different.  At this point all the hidden files should be cleared from the board and some hidden files will be prevented from being created.

However there are still some cases where hidden files will be created by Mac OSX.  In particular if you copy a file that was downloaded from the internet it will have special metadata that Mac OSX stores as a hidden file.  Luckily you can run a copy command from the terminal to copy files without this hidden metadata file.  See the steps below:

Copy Files on Mac OSX Without Creating Hidden Files

Once you've disabled and removed hidden files with the above commands on Mac OSX you need to be careful to copy files to the board with a special command that prevents future hidden files from being created.  Unfortunately you cannot use drag and drop copy in Finder because it will still create these hidden extended attribute files in some cases (for files downloaded from the internet, like Adafruit's modules).

To copy a file or folder use the -X option for the cp command in a terminal.  For example to copy a foo.mpy file to the board use a command like:

cp -X foo.mpy /Volumes/CIRCUITPY</pre>

Or to copy a folder and all of its child files/folders use a command like:

cp -rX folder_to_copy /Volumes/CIRCUITPY</pre>

Other Mac OSX Tips

If you'd like to see the amount of space used on the drive and manually delete hidden files here's how to do so.  First list the amount of space used on the CIRCUITPY drive with the df command:

FEATHER-M0-MicroPython-bibliotheque-08.png
Crédit: AdaFruit Industries www.adafruit.com

Lets remove the ._ files first.

FEATHER-M0-MicroPython-bibliotheque-09.png
Crédit: AdaFruit Industries www.adafruit.com

Whoa! We have 13Ki more than before! Lets continue!

<a href="#continuing-after-copy" class="anchor-link"></a>Continuing after copy

Woohoo! Everything copied over just fine. Lets check the serial terminal to see how things are going.

FEATHER-M0-MicroPython-bibliotheque-10.png
Crédit: AdaFruit Industries www.adafruit.com

Oops! Another ImportError! Libraries can depend on other libraries so copying one file over may not be enough. Looking in the bundle, there is an adafruit_bus_device directory. In Python terms this is a package. It contains module files. Lets copy the folder over to make sure we get everything.

FEATHER-M0-MicroPython-bibliotheque-11.png
Crédit: AdaFruit Industries www.adafruit.com

If that fails due to out of space, <a href="https://learn.adafruit.com/adafruit-gemma-m0/installing-libraries#out-of-space">see above</a>. If not, continue on.

Lets check the serial connection again. Looks like it worked! We don't have any more ImportErrors and we can see the temperature (in Celsius) and the relative humidity.

FEATHER-M0-MicroPython-bibliotheque-12.png
Crédit: AdaFruit Industries www.adafruit.com



Source: Adafruit Feather M0 Express - Designed for CircuitPython créé par LadyAda, Tony DiCola, Scorr Shawcroft, Dan Halbert pour AdaFruit Industries. Crédit AdaFruit Industries

Traduit par Meurisse D. pour MCHobby.be

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

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.