Ligne 135 : |
Ligne 135 : |
| pyb.delay(1) | | pyb.delay(1) |
| | | |
| + | pin_x4 = pyb.Pin('X4', pyb.Pin.IN, pyb.Pin.PULL_DOWN) |
| + | while True: |
| + | print( 'attendre pression bouton' ) |
| + | wait_pin_change(pin_x4) |
| + | print( 'bouton enfonce' ) |
| + | pyb.LED(4).toggle() |
| + | print( 'LED modifiee' ) |
| + | pyb.LED(2).on() |
| + | print( 'LED 2 allumee') |
| + | pyb.delay(2000) |
| + | pyb.LED(2).off() |
| + | print( 'LED 2 eteinte')</nowiki> |
| + | |
| + | == Attendre pour déboguer == |
| + | Nous avons vu que l'utilisation d'un terminal série et des instructions '''print()''' sont bien utiles pour déboguer un programme. |
| + | |
| + | Par contre, le programme dans {{fname|main.py}} est exécuté immédiatement... peut être trop vite pour avoir le temps de lancer un programme terminal et voir les premiers {{fname|print()}}. |
| + | |
| + | La petite astuce, c'est d'attendre que l'utilisateur presse le bouton USR (''Utilisateur'') de la carte PyBoard pour lancer le programme. |
| + | |
| + | Voici donc une première version de {{fname|debug_wait_start()}}, cette fonction: |
| + | * Est appelée en tout début de programme. |
| + | * debug_wait_start(): |
| + | ** Allume la LED(4), bleue, pour signaler que la fonction attend la pression du bouton USR<br />C'est maintenant qu'il faut ouvrir votre session terminal ;-) |
| + | ** Attend la pression du bouton USR |
| + | ** Eteind la LED(4) pour signaler que la fonction {{fname|debug_wait_start()}} s'achève. |
| + | ** Fait un premier {{fname|print()}} avec le message "PROGRAMME STARTED" (ou similaire). |
| + | |
| + | <nowiki># main.py -- put your code here! |
| + | |
| + | import pyb |
| + | |
| + | def wait_pin_change(pin): |
| + | # attendre que la broche change de valeur |
| + | cur_value = pin.value() |
| + | # Elle doit être stable pendant un temps continu de 20ms |
| + | active = 0 |
| + | while active < 20: |
| + | if pin.value() != cur_value: |
| + | active += 1 |
| + | else: |
| + | active = 0 |
| + | pyb.delay(1) |
| + | |
| + | def wait_user_button(): |
| + | pin_user = pyb.Pin('X17', pyb.Pin.IN, pyb.Pin.PULL_UP) |
| + | wait_pin_change( pin_user ) |
| + | |
| + | |
| + | def debug_wait_start(): |
| + | print( 'PRESS USR TO START' ) |
| + | # allumer la LED bleue |
| + | pyb.LED(4).on() |
| + | # attendre la pression du bouton user |
| + | wait_user_button() |
| + | # eteindre LED bleue |
| + | pyb.LED(4).off() |
| + | # Informer l'utilisateur |
| + | print( 'PROGRAMME STARTED' ) |
| + | |
| + | |
| + | debug_wait_start() |
| pin_x4 = pyb.Pin('X4', pyb.Pin.IN, pyb.Pin.PULL_DOWN) | | pin_x4 = pyb.Pin('X4', pyb.Pin.IN, pyb.Pin.PULL_DOWN) |
| while True: | | while True: |