Modifications

Sauter à la navigation Sauter à la recherche
5 636 octets ajoutés ,  22 décembre 2015 à 21:45
aucun résumé de modification
Ligne 2 : Ligne 2 :     
{{traduction}}
 
{{traduction}}
 +
 +
== Utiliser la carte PyBoard ==
 +
{{bloc-etroit|text=When plugged into a USB port of a computer, the pyboard appears as a disk. You can edit the main.py file on this disk, then eject it, and press the RST button on the pyboard to reboot it and execute main.py.
 +
 +
However, there is a more interactive way of playing with the pyboard which has a REPL (Read-Eval-Print Loop). For this, you need to connect to the /dev/ttyACM0 (Linux) or /dev/tty.usbmodemXXXX (MacOS) using a serial communication program. The Unix screen command works fine, but I prefer to use [https://www.archlinux.org/packages/community/i686/minicom/ minicom].}}
 +
 +
== pyterm.py ==
 +
{{bloc-etroit|text=The following script, which I use to call {{fname|pyterm.py}}, will connect to a plugged pyboard using minicom: }}
 +
 +
<nowiki>#!/usr/bin/python
 +
import sys
 +
import os
 +
import fnmatch
 +
 +
"""
 +
Usage: pyterm [device]
 +
Calls minicom on "device". If device is not given, looks for /dev/ttyACM* devices
 +
(the name of the pyboard under Linux) or /dev/tty.usbmodem* devices (name of the
 +
pyboard under MacOS).
 +
"""
 +
def main():
 +
    if len(sys.argv) > 1:
 +
        ttymodems = [sys.argv[1]]
 +
    else:
 +
        ttymodems = fnmatch.filter(os.listdir('/dev'), 'ttyACM*')
 +
        if len(ttymodems) == 0:
 +
            ttymodems = fnmatch.filter(os.listdir('/dev'), 'tty.usbmodem*')
 +
        if len(ttymodems) == 0:
 +
            print('Error: no pyboard found.')
 +
            sys.exit(1)
 +
        ttymodems[0] = '/dev/' + ttymodems[0]
 +
    os.system('minicom -D '+ttymodems[0])
 +
 +
if __name__ == "__main__":
 +
    main()</nowiki>
 +
 +
Avec le script {{fname|pyterm.py}}, vous pouvez démarrer une session avec la commande {{fname|./pyterm.py /dev/ttyACM1}} ou /dev/ttyACM1 est le périphérique correspondant à votre pyboard.
 +
 +
Once connected, you can type Python commands and discover many possibilities of the pyboard. For instance:
 +
 +
<nowiki>led = pyb.LED(1)
 +
led.on()</nowiki>
 +
 +
will turn on the red LED on the pyboard.
 +
 +
<nowiki>led = pyb.LED(1)
 +
def blink(timer):
 +
  led.toggle()
 +
tim = pyb.Timer(4, freq=2, callback=blink)</nowiki>
 +
 +
will make the red LED blink at 1Hz (it is toggled on/off at 2Hz).
 +
 +
== Charger du code sur le PyBoard ==
 +
For writing more complex code, there are two possibilities:
 +
 +
# edit the files on the pyboard, and load them into the REPL using import
 +
# edit the files on your computer, and paste them into the REPL
 +
 +
The first solution works fine and is the only way to work with code that is split into different modules.
 +
 +
The second solution is perhaps easier, but you cannot paste too much text in the REPL without getting errors. You have to switch to the raw REPL by hitting Ctrl-A, pasting your code, and hitting Ctrl-D to finish the transfer. Your code will be running on the pyboard. Hit Ctrl-C to kill it if it does note terminate by itself, then hit Ctrl-B to exit the raw REPL and switch back to the friendly REPL. You can perform a soft reset of the pyboard by hitting Ctrl-D.
 +
 +
Note than when using minicom, you have to hit Ctrl-A twice to send a Ctrl-A to the REPL because Ctrl-A is by default the escape key of minicom. You can use the paste file command in minicom to send a file to the REPL. For this:
 +
 +
# hit Ctrl-A twice to enter the raw REPL
 +
# hit Ctrl-A Y to execute the paste file minicom command
 +
# navigate to your file in the file selection screen that appears and select it
 +
# hit Ctrl-D to finish the transfer
 +
 +
Your file is then loaded. If it contains just definitions, hit Ctrl-B to exit the rawREPL and use the definitions in the regular REPL.
 +
 +
== pyboard.py ==
 +
 +
Another way of executing code on the pyboard is to use the {{fname|pyboard.py}} script available in the tools directory of the MicroPython git repository. Exit minicom with Ctrl-A X, then load your file onto the pyboard with pyboard.py file_to_load.py. You can connect to the REPL by launching minicom again.
 +
 +
You can modify the pyboard.py script to find the pyboard device automatically by changing the beginning of the main function as follows:
 +
 +
<nowiki>def main():
 +
    import argparse
 +
    import os
 +
    import fnmatch
 +
    ttymodems = fnmatch.filter(os.listdir('/dev'), 'ttyACM*')
 +
    if len(ttymodems) == 0:
 +
        ttymodems = fnmatch.filter(os.listdir('/dev'), 'tty.usbmodem*')
 +
    if len(ttymodems) == 0:
 +
        print('Error: no pyboard found.')
 +
        sys.exit(1)
 +
    cmd_parser = argparse.ArgumentParser(description='Run scripts on the pyboard.')
 +
    cmd_parser.add_argument('--device', default='/dev/' + ttymodems[0], help='the serial device of the pyboard')</nowiki>
 +
 +
An already patched version (may not be up to date with respect to the git repository) [http://wdi.supelec.fr/boulanger/downloads/MicroPython/pyboard.py is available].
 +
 +
Si vous desirez utiliser le script original du script, la commande à utiliser pour charger votre fichier mon_fichier.py sur ma pyboard est {{fname|./pyboard --device=/dev/ttyACM1 mon_fichier.py}} . /dev/ttyACM1 correspond à la connexion série associée à votre PyBoard.
 +
 +
== Element Fritzing ==
 +
I use [http://fritzing.org/ Fritzing] to draw my sketches. I have made a [http://wdi.supelec.fr/boulanger/downloads/MicroPython/pyboard.fzpz Fritzing part for the pyboard]. You can also get the SVG pictures I made for the [http://wdi.supelec.fr/boulanger/downloads/MicroPython/pyboard_PCB.svg PCB], the [http://wdi.supelec.fr/boulanger/downloads/MicroPython/pyboard_schem.svg schematics] and the [http://wdi.supelec.fr/boulanger/downloads/MicroPython/pyboard.svg icon].
 +
 +
You can see how it looks on the example below:
    
== Source et autorisation de traduction ==
 
== Source et autorisation de traduction ==
Ligne 7 : Ligne 105 :     
Vous pouvez retrouver l'article original sur [http://wdi.supelec.fr/boulanger/MicroPython/ http://wdi.supelec.fr/boulanger/MicroPython/]
 
Vous pouvez retrouver l'article original sur [http://wdi.supelec.fr/boulanger/MicroPython/ http://wdi.supelec.fr/boulanger/MicroPython/]
 +
 +
[Fichier:MicroPython-Hack-Outil-01.jpg]]<small><br />Source: [http://wdi.supelec.fr/boulanger/MicroPython/ Supelec.fr]</small>
    
{{MicroPython-Hack-Prepare-TRAILER}}
 
{{MicroPython-Hack-Prepare-TRAILER}}
29 879

modifications

Menu de navigation