Modifications

Sauter à la navigation Sauter à la recherche
5 705 octets supprimés ,  20 février 2016 à 15:57
Contenu remplacé par « {{MicroPython-Hack-Prepare-NAV}} {{MicroPython-Hack-LED-core}} {{MicroPython-Hack-LED-TRAILER}} »
Ligne 1 : Ligne 1 :  
{{MicroPython-Hack-Prepare-NAV}}
 
{{MicroPython-Hack-Prepare-NAV}}
   −
{{traduction}}
+
{{MicroPython-Hack-LED-core}}
 
+
   
== Activer les LEDs et concepts de base ==
  −
{{bloc-etroit|text=L'une des choses les plus faciles à faire sur une carte PyBoard est est d'allumer les LEDs qui sont montés sur la carte. Connectez la carte, et connectez vous sur la carte en REPL comme décrit dans le tutoriel précédent. Nous allons commencer par allumer une LED dans l’interpréteur en tapant la commande suivante:}}
  −
 
  −
<nowiki>>>> myled = pyb.LED(1)
  −
>>> myled.on()
  −
>>> myled.off()</nowiki>
  −
 
  −
{{bloc-etroit|text=Ces commandes allume (''on'') et éteint (''off'') la LED.
  −
 
  −
Tout cela est très bien mais nous aimerions que ce processus soit automatisé. Ouvrez le fichier {{fname|main.py}} sur la carte PyBoard avec votre éditeur de texte favorit. Ecrivez (ou copiez/collez) les lignes suivantes dans le fichier. Si vous débutez en Python, assurez-vous de bien préserver l'indentation dans les différentes lignes, cela est vraiment très important!}}
  −
 
  −
<nowiki>led = pyb.LED(2)
  −
while True:
  −
    led.toggle()
  −
    pyb.delay(1000)</nowiki>
  −
 
  −
Lorsque vous sauvez le fichier, la LED rouge sur la carte doit s'allumer pendant environ une seconde. Pour exécuter le script, faite une réinitialisation logicielle (''soft reset'') en pressant (CTRL-D). La carte PyBoard va redémarrer et vous devriez voir la LED rouge clignoter continuellement. Ca marche? génial, vous venez de réaliser la première étape vous permettant de réaliser votre futur armée de petits robots! Lorsque vous serez lassé de voir la LED clignoter alors pressez la combinaison CTRL-C dans votre terminal pour interrompre l'exécution.
  −
 
  −
Que fait donc ce code? Pour commencer, nous avons besoin de faire le point sur la terminologie. Python est un langage orienté objet, donc presque tout les éléments en Python son des classe et lorsque vous créer une instance de classe, vous obtenez un objet. La classe représente la définition, l'objet instancié (depuis un classe) un "objet" manipulable. Une méthode (aussi appelée fonction membre) est utilisé pour interagir avec l'objet, ce qui permet de le contrôler.
  −
 
  −
La première ligne e code crée un objet appelé "led", instance de la classe LED. Lorsque l'objet est crée, celui-ci prend un seul paramètre dont la valeur varie entre 1 et 4, valeurs correspondants aux 4 LEDs disponibles sur la carte.
  −
 
  −
La classe pyb.LED dispose de 3 membres importants que nous allons utiliser:
  −
* on() pour allumer la LED,
  −
* off() pour éteindre la LED,
  −
* toggle() pour inverser l'état de la LED.
  −
L'autre fonction que nous allons utiliser est pyb.delay() qui attend un temps/délai définit exprimé en milliseconde.
  −
 
  −
Une fois que nous avons crée l'objet "led" (instance de la classe LED), la ligne {{fname|while True:}} crée une boucle infinie qui change l'état de la led (via l'appel de {{fname|toggle()}}) et attend une seconde (soit 1000 millisecondes).
  −
 
  −
Note: voyez aussi l'article "[http://www.commentcamarche.net/contents/807-classe-et-instance-d-objet classe et instance d'objet]" sur [http://www.commentcamarche.net commentcamarche.net]
  −
 
  −
=== Exercice 1 ===
  −
Essayez de changer le temps entre deux changement d'état de la LED.... et essayez d'allumer une autre LED.
  −
 
  −
=== Exercice 2 ===
  −
Connectez vous directement sur votre pyboard, créez un objet pyb.LED et allumer une LED en utilisant la méthode on().
  −
 
  −
== C'est Disco sur votre PyBoard ==
  −
So far we have only used a single LED but the pyboard has 4 available. Let’s start by creating an object for each LED so we can control each of them. We do that by creating a list of LEDS with a list comprehension.
  −
 
  −
<nowiki>leds = [pyb.LED(i) for i in range(1,5)]</nowiki>
  −
 
  −
If you call pyb.LED() with a number that isn’t 1,2,3,4 you will get an error message. Next we will set up an infinite loop that cycles through each of the LEDs turning them on and off.
  −
 
  −
<nowiki>n = 0
  −
while True:
  −
  n = (n + 1) % 4
  −
  leds[n].toggle()
  −
  pyb.delay(50)</nowiki>
  −
 
  −
Here, n keeps track of the current LED and every time the loop is executed we cycle to the next n (the % sign is a modulus operator that keeps n between 0 and 3.) Then we access the nth LED and toggle it. If you run this you should see each of the LEDs turning on then all turning off again in sequence.
  −
 
  −
One problem you might find is that if you stop the script and then start it again that the LEDs are stuck on from the previous run, ruining our carefully choreographed disco. We can fix this by turning all the LEDs off when we initialise the script and then using a try/finally block. When you press CTRL-C, Micro Python generates a VCPInterrupt exception. Exceptions normally mean something has gone wrong and you can use a try: command to “catch” an exception. In this case it is just the user interrupting the script, so we don’t need to catch the error but just tell Micro Python what to do when we exit. The finally block does this, and we use it to make sure all the LEDs are off. The full code is:
  −
 
  −
<nowiki>leds = [pyb.LED(i) for i in range(1,5)]
  −
for l in leds:
  −
    l.off()
  −
 
  −
n = 0
  −
try:
  −
  while True:
  −
      n = (n + 1) % 4
  −
      leds[n].toggle()
  −
      pyb.delay(50)
  −
finally:
  −
    for l in leds:
  −
        l.off()</nowiki>
  −
 
  −
== Les 4 LEDs spéciales ==
  −
The blue LED is special. As well as turning it on and off, you can control the intensity using the intensity() method. This takes a number between 0 and 255 that determines how bright it is. The following script makes the blue LED gradually brighter then turns it off again.
  −
 
  −
<nowiki>led = pyb.LED(4)
  −
intensity = 0
  −
while True:
  −
    intensity = (intensity + 1) % 255
  −
    led.intensity(intensity)
  −
    pyb.delay(20)</nowiki>
  −
 
  −
You can call intensity() on the other LEDs but they can only be off or on. 0 sets them off and any other number up to 255 turns them on.
   
{{MicroPython-Hack-LED-TRAILER}}
 
{{MicroPython-Hack-LED-TRAILER}}
29 918

modifications

Menu de navigation