Différences entre versions de « PiFace2-Manuel-Python »
Ligne 77 : | Ligne 77 : | ||
pfio.digital_write(0,1) | pfio.digital_write(0,1) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Clignoter une LED ==== | ||
+ | A program to flash an LED must repeatedly turn an output on, wait for a period and then turn it off again. It is shown below: | ||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | from time import sleep | ||
+ | import piface.pfio as pfio | ||
+ | pfio.init() | ||
+ | while(True): | ||
+ | pfio.digital_write(0,1) #turn on | ||
+ | sleep(1) | ||
+ | pfio.digital_write(0,0) #turn off | ||
+ | sleep(1) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Lire une entrée === | ||
+ | To read the state of an input use the {{fname|pfio.digital_read(pin)}} function. If a button is pressed the function returns a ''1'', otherwise it returns a ''0''. | ||
+ | |||
+ | Start a new Python interpreter and type the following: | ||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | import piface.pfio as pfio | ||
+ | pfio.init() | ||
+ | pfio.digital_read(0) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Python prints ''0''. | ||
+ | |||
+ | Hold down the first switch, labelled ''S0'', and type {{fname|pfio.digital_read(0)}} again. | ||
+ | |||
+ | Python prints ''1''. | ||
+ | |||
{{PiFace2-Manuel-TRAILER}} | {{PiFace2-Manuel-TRAILER}} |
Version du 20 mai 2017 à 12:55
En cours de traduction/élaboration. |
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:
python3
To use PiFace Digital 2 from Python you must import the pifacedigitalio module:
import pifacedigitalio
Before use, the board must be initialised with a call to:
pifacedigitalio.init()
Use the digital_read function to see if a button is pressed or not:
pifacedigitalio.digital_read(pin_number)
This function returns 1 if the input numbered pin_number is connected to ground or else 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.
pifacedigitalio.digital_write(pin_number, state)
Les pas suivants
A more powerful and expressive way of controlling PiFace Digital 2 is using the PiFaceDigital object. This will give you access to all PiFaceTM Digital 2’s the features (including pull-ups and input/output ports).
Start the Python interpreter with:
python3
Then instantiate the PiFaceDigital object like this:
import pifacedigitalio pfd = pifacedigitalio.PiFaceDigital()
Sorties
Set the first output pin to 1 (the on state):
pfd.output_pins[0].value = 1
Notice how the first relay clicks as it activates. Output pin 0 controls the first relay. You can turn the output pin (and the relay) off by setting it to 0:
pfd.output_pins[0].value = 0
Entrées
You can read the value of an input pin like this:
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.
input_port et output_port
You can read the whole input port by reading the value of the input_port attribute:
pfd.input_port.value
Or set the outputs for the whole port by setting the value of the output_port attribute:
pfd.output_port.value = 0xAA
Exemples python
Activer un relai
The relays on PiFaceTM Digital 2 are connected to the first two outputs, so they can be controlled using the digital_write function.
Start a new Python interpreter and type the following:
import piface.pfio as pfio
pfio.init()
pfio.digital_write(0,1)
Clignoter une LED =
A program to flash an LED must repeatedly turn an output on, wait for a period and then turn it off again. It is shown below:
from time import sleep
import piface.pfio as pfio
pfio.init()
while(True):
pfio.digital_write(0,1) #turn on
sleep(1)
pfio.digital_write(0,0) #turn off
sleep(1)
Lire une entrée
To read the state of an input use the pfio.digital_read(pin) function. If a button is pressed the function returns a 1, otherwise it returns a 0.
Start a new Python interpreter and type the following:
import piface.pfio as pfio
pfio.init()
pfio.digital_read(0)
Python prints 0.
Hold down the first switch, labelled S0, and type pfio.digital_read(0) again.
Python prints 1.
Traduction basée sur le document Technical DataSheet produit par Farnell
Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : « Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.
L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.