Ligne 75 : |
Ligne 75 : |
| | | |
| == 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 91 : |
| 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 115 : |
| 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.''' Sélectionnez {{fname|File > Save}} (''Fichier > Sauver'') et choisissez un nom de fichier pour votre programme. |
| | | |
− | '''5.''' Select {{fname|File > Save}} and choose a file name for your program. | + | '''6.''' Sélectionnez ensuite {{fname|Run > Run module}} (''Executer > Executer module''). |
| | | |
− | '''6.''' Then select {{fname|Run > Run module}}.
| + | {{traduction}} |
| | | |
| '''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.''' 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. |