Ligne 1 : |
Ligne 1 : |
| {{PiFace2-Manuel-NAV}} | | {{PiFace2-Manuel-NAV}} |
| | | |
− | {{traduction}}
| |
| == Premiers pas avec Python == | | == Premiers pas avec Python == |
− | PiFace Digital 2 can be controlled easily using Python. First open a terminal window and start the Python interpreter by typing: | + | PiFace Digital 2 peut être contrôlé facilement en utilisant Python. Pour commencer, ouvrez un terminal (ligne de commande) et démarrez un interpréteur en saisissant la commande suivante: |
| | | |
| python3 | | python3 |
| | | |
− | To use PiFace Digital 2 from Python you must import the pifacedigitalio module:
| + | {{ambox|text=Vous pouvez également utiliser l'éditeur "Idle" disponible dans l'environnement graphique}} |
| + | |
| + | Pour utiliser le PiFace Digital 2 depuis Python, il faut importer le module {{fname|pifacedigitalio}} : |
| | | |
| import pifacedigitalio | | import pifacedigitalio |
| | | |
− | Before use, the board must be initialised with a call to:
| + | Avant d'utiliser la carte PiFace, il faut initialiser la bibliothèque en faisant l'appel suivant: |
| | | |
| pifacedigitalio.init() | | pifacedigitalio.init() |
| | | |
− | Use the digital_read function to see if a button is pressed or not:
| + | === Lire une entrée === |
| + | |
| + | Si vous désirez savoir si un bouton est enfoncé ou non, il faut utiliser la fonction {{fname|digital_read}} : |
| + | |
| + | pifacedigitalio.digital_read( numero_de_broche ) |
| | | |
− | pifacedigitalio.digital_read(pin_number)
| + | La fonction retourne ''1'' si l'entrée indiquée dans ''numero_de_broche'' (1 pour le bouton poussoir S1). |
| | | |
− | This function returns ''1'' if the input numbered ''pin_number'' is connected to ground or else ''0''.
| + | Si le bouton est enfoncé ou l'entrée est connecté sur la masse (GND). Sinon la fonction retourne ''0''. |
| | | |
− | Set the output numbered ''pin_number'' to state ''0'' or ''1''. State ''1'' turns the LED on and enables to open-collector to sink current.
| + | === Contrôler une sortie === |
| | | |
− | pifacedigitalio.digital_write(pin_number, state) | + | Si vous désirez contrôler une sortie, il faut utiliser la fonction {{fname|digital_write}} : |
| + | |
| + | pifacedigitalio.digital_write( numero_de_broche, etat ) |
| + | |
| + | La fonction permet de fixer l'état de la broche ''numero_de_broche'' sur l'état '''0''' (inactif) ou '''1''' (actif). |
| + | |
| + | L'état ''1'' active le collecteur ouvert (qui laisse passer le courant) et la LED correspondante. |
| + | |
| + | L'instruction suivante active la sortie 2 et la LED 2 correspondante: |
| + | |
| + | pifacedigitalio.digital_write( 2, 1 ) |
| | | |
| == Les pas suivants == | | == Les pas suivants == |
− | A more powerful and expressive way of controlling PiFace Digital 2 is using the PiFaceDigital object.
| + | Une façon plus puissante et plus explicite de contrôler le PiFace est de passer par un objet {{fname|PiFaceDigital}}. |
− | This will give you access to all PiFaceTM Digital 2’s the features (including pull-ups and input/output ports).
| + | |
| + | En créant un objet de type {{fname|PiFaceDigital}}, vous aurez accès à toutes les fonctionnalités du PiFace (incluant l'activation des pull-ups, l'input_port et output_port). |
| | | |
− | Start the Python interpreter with:
| + | Démarrer un interpréteur python à l'aide de la commande: |
| | | |
| python3 | | python3 |
| | | |
− | Then instantiate the ''PiFaceDigital'' object like this:
| + | Instanciez ensuite un objet ''PiFaceDigital'' à l'aide de l'instruction suivante: |
| | | |
| import pifacedigitalio | | import pifacedigitalio |
| pfd = pifacedigitalio.PiFaceDigital() | | pfd = pifacedigitalio.PiFaceDigital() |
| | | |
− | == Sorties == | + | === Sorties === |
− | Set the first output pin to ''1'' (the on state):
| + | Pour fixer la première sortie (celle qui porte le numéro 0) sur l'état ''1'' (pour activer la sortie): |
| | | |
| pfd.output_pins[0].value = 1 | | pfd.output_pins[0].value = 1 |
| | | |
− | Notice how the first relay clicks as it activates. Output pin ''0'' controls the first relay. You can
| + | Notez le petit cliquetis du relais lorsqu'il s'active. La broche de sortie ''0'' contrôle également le premier relais. |
− | turn the output pin (and the relay) off by setting it to ''0'':
| + | |
| + | Vous pouvez également désactiver la sortie numéro 0 (et le relais) en fixant la valeur de la sortie à ''0'': |
| | | |
| pfd.output_pins[0].value = 0 | | pfd.output_pins[0].value = 0 |
| | | |
− | == Entrées == | + | === Entrées === |
− | You can read the value of an input pin like this:
| + | Vous pouvez lire la valeur d'une entrée à l'aide de l'instruction suivante: |
| | | |
| pfd.input_pins[1].value | | pfd.input_pins[1].value |
| | | |
− | Try running the above command with the second switch pressed and then again with it unpressed and you should get different results.
| + | Essayez d'exécuter l'instruction ci-dessus en pressant le second bouton (qui porte le numéro 1) PUIS exécutée une seconde fois sans presser le bouton. |
| + | |
| + | Vous devriez voir deux résultats différents comme l'indique la capture ci-dessous. |
| | | |
− | == input_port et output_port == | + | <nowiki>>>> pfd.input_pins[1].value |
− | You can read the whole input port by reading the value of the input_port attribute:
| + | 0 |
| + | >>> pfd.input_pins[1].value |
| + | 1 |
| + | >>></nowiki> |
| + | |
| + | === input_port et output_port === |
| + | Vous pouvez lire toutes les entrées en une seule opération en utilisant l'attribut {{fname|input_port}} : |
| | | |
| pfd.input_port.value | | pfd.input_port.value |
| | | |
− | Or set the outputs for the whole port by setting the value of the output_port attribute:
| + | Input_port retourne une valeur numérique qui, une fois exprimée en valeur binaire, indique l'état de chaque entrée. |
| + | |
| + | Il est également possible de contrôler toutes les sorties en une opération en manipulant l'attribut {{fname|output_port}} : |
| | | |
| pfd.output_port.value = 0xAA | | pfd.output_port.value = 0xAA |
Ligne 66 : |
Ligne 93 : |
| == Exemples python == | | == Exemples python == |
| | | |
− | === Activer un relai === | + | === Activer un relais === |
| + | |
| + | Les relais du PiFace Digital sont respectivement connecté sur les deux premières entrées 0 et 1. Vous pouvez donc contrôler ces deux relais à l'aide de la fonction {{fname|digital_write()}}. |
| + | |
| + | Start a new Python interpreter and type the following: |
| + | |
| + | <syntaxhighlight lang="python"> |
| + | import pifaceio as pfio |
| + | pfio.init() |
| + | # mettre la sortie 0 a l etat 1 (activer) |
| + | pfio.digital_write(0,1) |
| + | </syntaxhighlight> |
| + | |
| + | === Clignoter une LED === |
| + | Un programme qui fait clignoter une LED doit l'allumer et l'éteindre de façon répétitive (avec un délai entre chaque activation/désactivation). |
| + | |
| + | <syntaxhighlight lang="python"> |
| + | from time import sleep |
| + | import pifaceio as pfio |
| + | pfio.init() |
| + | while(True): |
| + | # activer la sortie 0 = allumer la LED |
| + | pfio.digital_write(0,1) |
| + | # Attendre une seconde |
| + | sleep(1) |
| + | # désactiver la sortie 0 = eteindre la LED |
| + | pfio.digital_write(0,0) |
| + | sleep(1) |
| + | </syntaxhighlight> |
| + | |
| + | === Lire une entrée === |
| + | Il faut utiliser la fonction {{fname|digital_read(pin)}} pour lire l'état d'une entrée. La fonction retourne {{fname|True}} si le bouton est pressé (ou entrée activée) sinon elle retourne '{{fname|False}} . |
| + | |
| + | Démarrez un nouvel interpréteur Python et saisissez les instructions suivantes: |
| + | |
| + | <syntaxhighlight lang="python"> |
| + | import pifaceio as pfio |
| + | pfio.init() |
| + | # lecture de l entree 0 (correspond au premier bouton) |
| + | pfio.digital_read(0) |
| + | </syntaxhighlight> |
| + | |
| + | Python affiche {{fname|False}} . |
| + | |
| + | Maintenez le premier switch enfoncé (celui portant le libellé ''S0'') et saisissez une nouvelle fois la commande {{fname|digital_read(0)}} . |
| + | |
| + | Cette fois Python devrait afficher {{fname|True}} . |
| + | |
| | | |
| {{PiFace2-Manuel-TRAILER}} | | {{PiFace2-Manuel-TRAILER}} |