Ligne 1 : |
Ligne 1 : |
| {{RASP-SENSE-HAT-ASTRO-PI-NAV}} | | {{RASP-SENSE-HAT-ASTRO-PI-NAV}} |
− |
| |
− | {{traduction}}
| |
| | | |
| == Boutons de vols == | | == Boutons de vols == |
Ligne 75 : |
Ligne 73 : |
| | | |
| == Détecter un bouton (dans le code) == | | == Détecter un bouton (dans le code) == |
− | '''1.''' Open Python 3 from a terminal window en tant que super utilisateur avec {{fname|sudo}} by typing: | + | '''1.''' Ouvrir Python 3 dans une fenêtre terminal en tant que super utilisateur avec {{fname|sudo}} en tapant: |
| | | |
| <nowiki>sudo idle3 &</nowiki> | | <nowiki>sudo idle3 &</nowiki> |
| | | |
− | '''2.''' A Python Shell window will now appear. | + | '''2.''' Une fenêtre 'Python Shell' apparaîtra. |
| | | |
− | '''3.''' Select {{fname|File > New Window}}. | + | '''3.''' Sélectionnez le menu {{fname|File > New Window}} (''Fichier > Nouvelle fenêtre''). |
| | | |
− | Type in or copy/paste the following code:
| + | Tapez le code suivant (ou faite un copier/collr): |
| | | |
| <nowiki>import RPi.GPIO as GPIO | | <nowiki>import RPi.GPIO as GPIO |
Ligne 91 : |
Ligne 89 : |
| sense = SenseHat() | | sense = SenseHat() |
| | | |
− | UP = 26 | + | UP = 26 # Haut |
− | DOWN = 13 | + | DOWN = 13 # bas |
− | LEFT = 20 | + | LEFT = 20 # gauche |
− | RIGHT = 19 | + | RIGHT = 19 # droite |
| A = 16 | | A = 16 |
| B = 21 | | B = 21 |
| | | |
− | running = True | + | running = True # Variable pour 'en_cours_d_execution' |
| | | |
| + | # Fonction appelee lorsqu un bouton est active |
| def button_pressed(button): | | def button_pressed(button): |
| global running | | global running |
| global sense | | global sense |
| + | |
| + | # Afficher le bouton (la broche GPIO activee) |
| print(button) | | print(button) |
| sense.show_message(str(button)) | | sense.show_message(str(button)) |
| + | |
| + | # Bouton B: signaler l arret du programme |
| if button == B: | | if button == B: |
| running = False | | running = False |
Ligne 110 : |
Ligne 113 : |
| GPIO.setmode(GPIO.BCM) | | GPIO.setmode(GPIO.BCM) |
| | | |
| + | # Configurer les broches en entree |
| for pin in [UP, DOWN, LEFT, RIGHT, A, B]: | | for pin in [UP, DOWN, LEFT, RIGHT, A, B]: |
| GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) | | GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) |
| + | |
| + | # attacher un évenement sur le changement d etat |
| + | # d une broche GPIO. La fonction appelee est boutton_pressed |
| GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_pressed, bouncetime=100) | | GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_pressed, bouncetime=100) |
| | | |
| + | # Le programme principale de fait rien... il attend |
| + | # Tout se passe dans les événements |
| while running: | | while running: |
| time.sleep(1) | | time.sleep(1) |
| | | |
− | sense.show_message("Bye")</nowiki> | + | sense.show_message("Au revoir")</nowiki> |
| | | |
− | '''5.''' Select {{fname|File > Save}} and choose a file name for your program. | + | '''5.''' Sélectionnez {{fname|File > Save}} (''Fichier > Sauver'') et choisissez un nom de fichier pour votre programme. |
| | | |
− | '''6.''' Then select {{fname|Run > Run module}}. | + | '''6.''' Sélectionnez ensuite {{fname|Run > Run module}} (''Executer > Executer module''). |
| | | |
− | '''7.''' This program will just display the corresponding GPIO number every time a button is pressed. If you press the '''B''' button (bottom pair, right) then the program ends. This should allow you to test that your wiring is correct before proceeding. | + | '''7.''' Ce programme se contente d'afficher la broche GPIO correspondante chaque fois qu'un bouton est pressé. Le programme se termine si vous pressez le bouton '''B''' (la paire du bas, à droite). Cela vous permet de savoir si branchements sont correctes avant d'aller plus loin. |
| | | |
− | '''8.''' The code above makes the {{fname|button_pressed}} function run whenever any button is pressed. However, there are many different ways you can program the button detection. For instance, you might want to make your code wait until a button is pressed before doing something. Here is an example of how to do that using the '''UP''' button: | + | '''8.''' Le code ci-dessus exécute la fonction {{fname|button_pressed}} chaque fois qu'un bouton est pressé. Cependant, il y a de nombreuses autres façons de détecter la pression des boutons dans vos programmes. Vous pourriez, par exemple, faire attendre votre programme jusqu'au moment où vous détectez la pression d'un bouton... puis exécuter l'action correspondante. Voici un exemple montrant comment réaliser cela avec le bouton '''UP''' (vers le haut): |
| | | |
| <nowiki>import RPi.GPIO as GPIO | | <nowiki>import RPi.GPIO as GPIO |
Ligne 132 : |
Ligne 141 : |
| sense = SenseHat() | | sense = SenseHat() |
| | | |
− | UP = 26 | + | UP = 26 # Haut |
− | DOWN = 13 | + | DOWN = 13 # bas |
− | LEFT = 20 | + | LEFT = 20 # gauche |
− | RIGHT = 19 | + | RIGHT = 19 # droite |
− | A = 16 | + | A = 16 # bouton A |
− | B = 21 | + | B = 21 # bouton B |
| | | |
| GPIO.setmode(GPIO.BCM) | | GPIO.setmode(GPIO.BCM) |
| | | |
| + | # Definir les broche en entrée (avec option Pull-Up) |
| for pin in [UP, DOWN, LEFT, RIGHT, A, B]: | | for pin in [UP, DOWN, LEFT, RIGHT, A, B]: |
| GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) | | GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) |
| | | |
− | sense.show_message("Press UP to Start") | + | sense.show_message("Presser UP/HAUT pour démarrer le programme") |
| GPIO.wait_for_edge(UP, GPIO.FALLING) | | GPIO.wait_for_edge(UP, GPIO.FALLING) |
| sense.show_message("Nous y voici!")</nowiki> | | sense.show_message("Nous y voici!")</nowiki> |
| | | |
− | '''9.''' You might also want to test if a button is being held down and perhaps do something if it was held down for over 3 seconds. Here is another example: | + | '''9.''' Vous pourriez aussi avoir besoin de savoir si le bouton est maintenu enfoncé et --peut-être-- faire quelque-chose s'il est maintenu enfoncé pendant plus de 3 secondes. Voici un autre exemple: |
| | | |
| <nowiki>import RPi.GPIO as GPIO | | <nowiki>import RPi.GPIO as GPIO |
Ligne 156 : |
Ligne 166 : |
| sense = SenseHat() | | sense = SenseHat() |
| | | |
− | UP = 26 | + | UP = 26 # Haut |
− | DOWN = 13 | + | DOWN = 13 # bas |
− | LEFT = 20 | + | LEFT = 20 # gauche |
− | RIGHT = 19 | + | RIGHT = 19 # droite |
− | A = 16 | + | A = 16 # bouton A |
− | B = 21 | + | B = 21 # bouton B |
| | | |
| GPIO.setmode(GPIO.BCM) | | GPIO.setmode(GPIO.BCM) |
Ligne 168 : |
Ligne 178 : |
| GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) | | GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP) |
| | | |
− | while GPIO.input(A) == GPIO.HIGH: # wait while HIGH / not pressed | + | # attendre pendant HIGH / niveau haut / non pressé |
| + | while GPIO.input(A) == GPIO.HIGH: |
| time.sleep(0.01) | | time.sleep(0.01) |
| | | |
− | button_down_time = time.time() # record the time when the button went down | + | # enregistrer l'heure au moment où le bouton a été pressé |
| + | button_down_time = time.time() |
| | | |
| held_down = False | | held_down = False |
| | | |
| + | # pendant que LOW / niveau bas / Pressé |
| while GPIO.input(A) == GPIO.LOW: # wait while LOW / pressed | | while GPIO.input(A) == GPIO.LOW: # wait while LOW / pressed |
| time.sleep(0.01) | | time.sleep(0.01) |
− | if time.time() - button_down_time > 3: # has 3 seconds gone by? | + | |
− | held_down = True | + | # Y a t il plus de 3 secondes passée ? |
| + | if time.time() - button_down_time > 3: |
| + | held_down = True # indiquer que le bouton est pressé |
| break | | break |
| | | |
| if held_down: | | if held_down: |
− | print("Held down") | + | print("maintenu pressé") |
− | sense.show_message("Here we go!") | + | sense.show_message("Nous y voici!") |
| else: | | else: |
− | print("Not held down")</nowiki> | + | print("pas maintenu enfoncé")</nowiki> |
| | | |
| Lorsque vous maintenez le bouton '''A''' enfoncé pendant 3 secondes, vous devriez voir le message "Nous y voici". | | Lorsque vous maintenez le bouton '''A''' enfoncé pendant 3 secondes, vous devriez voir le message "Nous y voici". |
| | | |
| {{RASP-SENSE-HAT-ASTRO-PI-TRAILER}} | | {{RASP-SENSE-HAT-ASTRO-PI-TRAILER}} |