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

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche
(Page créée avec « {{FEATHER-CHARGER-FICHIER-MICROPYTHON-NAV}} {{traduction}} {{FEATHER-CHARGER-FICHIER-MICROPYTHON-TRAILER}} »)
 
Ligne 2 : Ligne 2 :
  
 
{{traduction}}
 
{{traduction}}
 +
 +
{{ambox|text=Before using ampy with the ESP8266 be sure you've disabled debug output if necessary: https://learn.adafruit.com/micropython-basics-load-files-and-run-code/disable-esp8266-debug-output}}
 +
 +
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!
 +
 +
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.
 +
 +
For example create a file '''test.py''' on your computer and save inside it the following Python code:
 +
 +
<syntaxhighlight lang="python">
 +
print('Hello world! I can count to 10:')
 +
for i in range(1,11):
 +
    print(i)
 +
</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:
 +
 +
<nowiki>ampy --port /serial/port run test.py<nowiki>
 +
 +
Where '''/serial/port''' is the path or name of the serial port connected to the MicroPython board.
 +
 +
  {{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.}}
 +
 +
You should see the output of the code after it was run on the board:
 +
 +
{{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.
 +
 +
'''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]].
 +
 +
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:
 +
 +
<syntaxhighlight lang="python">
 +
import time
 +
print('Hello world! I can count:')
 +
i = 1
 +
while True:
 +
    print(i)
 +
    i += 1
 +
    time.sleep(1.0)  # Delay for 1 second.
 +
</syntaxhighlight>
 +
 +
Then run it with the '''--no-output''' option and notice it immediately returns:
 +
 +
<nowiki>ampy --port /serial/port run --no-output test.py</nowiki>
 +
 +
However open the board's serial REPL and watch it count numbers every second!
 +
 +
{{ADFImage|FEATHER-CHARGER-FICHIER-MICROPYTHON-RUNCODE-01.png}}
 +
 +
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:
 +
 +
<syntaxhighlight lang="python">
 +
###########################################################################
 +
# 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.
 +
</syntaxhighlight>
 +
 +
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!
  
 
{{FEATHER-CHARGER-FICHIER-MICROPYTHON-TRAILER}}
 
{{FEATHER-CHARGER-FICHIER-MICROPYTHON-TRAILER}}

Version du 8 novembre 2016 à 22:35


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.

Using 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!

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.

For example create a file test.py on your computer and save inside it the following Python code:

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

In a terminal in the same directory as test.py run the following ampy command to execute the script on a connected MicroPython board:

ampy --port /serial/port run test.py<nowiki>

Where '''/serial/port''' is the path or name of the serial port connected to the MicroPython board.

  {{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.}}

You should see the output of the code after it was run on the board:

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

'''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]].

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:

 <syntaxhighlight lang="python">
import time
print('Hello world! I can count:')
i = 1
while True:
    print(i)
    i += 1
    time.sleep(1.0)  # Delay for 1 second.
 </syntaxhighlight>

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

 <nowiki>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