Différences entre versions de « FEATHER-CHARGER-FICHIER-MICROPYTHON-RUNCODE »

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
Ligne 3 : Ligne 3 :
 
{{traduction}}
 
{{traduction}}
  
{{ambox|text=Avant d'utiliser ampy avec un ESP8266, vous devez avoir désactivé la sortie de débogage (si nécessaire). Voyez la page [[FEATHER-CHARGER-FICHIER-MICROPYTHON-DEBUG|désact]]}}
+
{{ambox|text=Avant d'utiliser ampy avec un ESP8266, vous devez avoir désactivé la sortie de débogage (si nécessaire). Voyez la page [[FEATHER-CHARGER-FICHIER-MICROPYTHON-DEBUG|Désactiver la sortie
 +
DEBUG de l'ESP8266]]}}
  
Using {{fname|ampy}} you can take Python code written on your computer and run it on a connected MicroPython board. This gives you a simple workflow for exploring MicroPython. Write code on your computer in your favorite text editor, then use ampy's '''run''' command to run it on a board!
+
En utilisant {{fname|ampy}}, vous pouvez prendre du code python écrit sur votre odrinateur et l'exécuter sur sur un carte MicroPython connectée en USB-Série. Cela permet d'explorer MicroPython en utilisant un flux de traitement assez simple. Vous pouvez écrire vitre code sur votre ordinateur, dans votre éditeur de texte favoris, puis utiliser ampy pour l'exécuter sur la carte!}}
  
To use the '''run''' command just specify a path to a Python file on your computer.  Ampy will send the file to the board, wait for it to finish running, and print any output from the program.
+
Utilisez la commande '''{{fname|run}}''' (exécuter) et spécifiez simplement le chemin du fichier Python présent sur votre ordinateur, attendez la fin de son exécution, voyez les sorties (''instruction print')'') affichés par le programme.
  
For example create a file '''test.py''' on your computer and save inside it the following Python code:
+
Créez le fichier d'exemple '''test.py''' sur votre ordinateur et copiez/collez y le code Pyhton suivant:
  
 
  <syntaxhighlight lang="python">
 
  <syntaxhighlight lang="python">
Ligne 17 : Ligne 18 :
 
  </syntaxhighlight>
 
  </syntaxhighlight>
  
In a terminal in the same directory as test.py run the following ampy command to execute the script on a connected MicroPython board:
+
Ouvrez un terminal et placez vous dans le même répertoire que votre fichier test.py. Exécutez ensuite la commande ampy suivante pour exécuter le script sur votre carte MicroPython connectée en USB-Séerie:
  
  <nowiki>ampy --port /serial/port run test.py</nowiki>
+
  <nowiki>ampy --port /port/serie run test.py</nowiki>
  
Where '''/serial/port''' is the path or name of the serial port connected to the MicroPython board.
+
'''/port/serie''' est le chemin (ou nom) du port série correspondant à votre carte MicroPython connectée sur le port USB de votre ordinateur. Sur une machine Lunix (ex: Linux Mint, dérivé d'Ubuntu) cela se présente sous la forme /dev/ttyUSB0
  
   {{ambox|text=If you don't want to constantly specify the --port option you can set the AMPY_PORT environment variable in your terminal session and ampy will use it as the board's serial port.}}
+
   {{ambox|text=Si vous ne voulez pas préciser continuellement l'option --port dans la ligne de commande alors vous pouvez initialiser la variable d'envrionnement AMPY_PORT dans votre session terminal. Ampy utilisera automatiquement la valeur de la variable d'environnement si aucun port n'est précisé.}}
  
You should see the output of the code after it was run on the board:
+
Après exécution de la commande, vous devriez voir les sortie du scripts sur votre moniteur:
  
 
{{ADFImage|FEATHER-CHARGER-FICHIER-MICROPYTHON-RUNCODE-00.png}}
 
{{ADFImage|FEATHER-CHARGER-FICHIER-MICROPYTHON-RUNCODE-00.png}}
  
'''If you receive an error that ampy failed to receive the expected response be sure you disabled debug output as mentioned at the top of the page!'''  Also double check the board is connected to your computer and you are specifying the correct serial port for the board. Be sure the file test.py is in the same directory as you're running the ampy command too.
+
'''Si vous recevez une erreur stipulant que ampy n'a pas reçu la réponse attendue (''failed to receive the expected response'') c'est probablement parce que la sortie des message de débogage n'a pas été désactivé sur carte. Voyez le lien mentionné en haut de page!'''  Vérifiez également que votre carte est bien connéectée sur votre ordinateur et que vous avez spécifié correctement le port série de la carte. Finalement, assurez vous également que le fichier test.py se trouve bien dans le répertoire où vous vous trouvez (celui où vous allez exécuter ampy).
  
'''Be aware the run command is not a shell or tool that allows you to send input from your computer to the board!''' If you need to send input you'll want to connect to the board and [[FEATHER-CHARGER-MICROPYTHON-SERIAL-REPL|use its serial REPL]].
+
'''Attention: la commande {{fname|run}} n'est pas un shell ou un outil qui permet d'envoyer des entrées (données) depuis l'ordinateur vers la carte!''' Si vous avez besoin d'envoyer des entrées/données vers la carte alors vous aurez besoin de connecter la carte et [[FEATHER-CHARGER-MICROPYTHON-SERIAL-REPL|d'utiliser le REPL Série]].
 +
 
 +
{{traduction}}
  
 
By default the run command will wait for the script to finish running on the board before printing its output.  In some cases you don't want this behavior--for example if your script has a main or infinite loop that never returns you don't want ampy to sit around waiting forever for it to finish.  In this case add the '''--no-output''' option to the run command.  This flag tells ampy not to wait for any output and instead just start running the script and return.
 
By default the run command will wait for the script to finish running on the board before printing its output.  In some cases you don't want this behavior--for example if your script has a main or infinite loop that never returns you don't want ampy to sit around waiting forever for it to finish.  In this case add the '''--no-output''' option to the run command.  This flag tells ampy not to wait for any output and instead just start running the script and return.

Version du 13 novembre 2016 à 10:22


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.

En utilisant ampy, vous pouvez prendre du code python écrit sur votre odrinateur et l'exécuter sur sur un carte MicroPython connectée en USB-Série. Cela permet d'explorer MicroPython en utilisant un flux de traitement assez simple. Vous pouvez écrire vitre code sur votre ordinateur, dans votre éditeur de texte favoris, puis utiliser ampy pour l'exécuter sur la carte!}}

Utilisez la commande run (exécuter) et spécifiez simplement le chemin du fichier Python présent sur votre ordinateur, attendez la fin de son exécution, voyez les sorties (instruction print')) affichés par le programme.

Créez le fichier d'exemple test.py sur votre ordinateur et copiez/collez y le code Pyhton suivant:

print('Hello world! I can count to 10:')
for i in range(1,11):
    print(i)

Ouvrez un terminal et placez vous dans le même répertoire que votre fichier test.py. Exécutez ensuite la commande ampy suivante pour exécuter le script sur votre carte MicroPython connectée en USB-Séerie:

ampy --port /port/serie run test.py

/port/serie est le chemin (ou nom) du port série correspondant à votre carte MicroPython connectée sur le port USB de votre ordinateur. Sur une machine Lunix (ex: Linux Mint, dérivé d'Ubuntu) cela se présente sous la forme /dev/ttyUSB0

Après exécution de la commande, vous devriez voir les sortie du scripts sur votre moniteur:

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

Si vous recevez une erreur stipulant que ampy n'a pas reçu la réponse attendue (failed to receive the expected response) c'est probablement parce que la sortie des message de débogage n'a pas été désactivé sur carte. Voyez le lien mentionné en haut de page! Vérifiez également que votre carte est bien connéectée sur votre ordinateur et que vous avez spécifié correctement le port série de la carte. Finalement, assurez vous également que le fichier test.py se trouve bien dans le répertoire où vous vous trouvez (celui où vous allez exécuter ampy).

Attention: la commande run n'est pas un shell ou un outil qui permet d'envoyer des entrées (données) depuis l'ordinateur vers la carte! Si vous avez besoin d'envoyer des entrées/données vers la carte alors vous aurez besoin de connecter la carte et d'utiliser le REPL Série.

By default the run command will wait for the script to finish running on the board before printing its output. In some cases you don't want this behavior--for example if your script has a main or infinite loop that never returns you don't want ampy to sit around waiting forever for it to finish. In this case add the --no-output option to the run command. This flag tells ampy not to wait for any output and instead just start running the script and return.

For example modify the test.py script so that it counts numbers forever in an infinite loop:

import time
print('Hello world! I can count:')
i = 1
while True:
    print(i)
    i += 1
    time.sleep(1.0)  # Delay for 1 second.

Then run it with the --no-output option and notice it immediately returns:

ampy --port /serial/port run --no-output test.py

However open the board's serial REPL and watch it count numbers every second!

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

Remember the program is still running, ampy just didn't wait for it to stop!

The --no-output option is great for writing scripts that are like an Arduino sketch. In Arduino you have an explicit setup and loop function which you fill in with code that runs once (in setup) and code that runs forever (in loop). MicroPython doesn't have exactly the same concept, but you can create it yourself in your own Python scripts!

In fact look at the test.py above and notice all the code before the while True loop is like the setup function from an Arduino sketch, it's executed just once at the start of the program. Then the code inside the while True loop is like the loop function from Arduino, this code runs repeatedly as fast as possible. To make it a little more clear here's the test.py with comments that show where the setup code goes and where the loop code goes:

###########################################################################
# Setup code goes below, this is called once at the start of the program: #
###########################################################################
import time
print('Hello world! I can count:')
i = 1

while True:
    ###################################################################
    # Loop code goes inside the loop here, this is called repeatedly: #
    ###################################################################
    print(i)
    i += 1
    time.sleep(1.0)  # Delay for 1 second.

If you're coming to MicroPython with a background in Arduino, consider writing your MicroPython scripts in a similar style as the above. Put your setup code first and then a main loop that runs forever. Just be sure you add the --no-output option when running with ampy so it knows not to wait for the script to finish!


Source: MicroPython Basics: Load Files & Run Code
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