PiFace2-Manuel-Python
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
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.