Hardware Reference
In-Depth Information
Reading a Switch
For a computer to respond to its environment, it needs to be able to read inputs. First you
will use the graphical emulator to display the status of the inputs.
Showing Input Status Graphically
In the emulator, click the Keep Inputs Updated check box as shown in Figure 1-3. The inter-
val sets how often the inputs are read; for most cases, it is fine to leave it on 500ms.
Figure 1-3:
Enable the Keep
Inputs Updated
check box to
show the status
of inputs.
Test the input by pressing one of the buttons on the bottom left of PiFace. As shown in Figure
1-4, the on-screen representation changes to indicate the switch that has been pressed.
Figure 1-4:
The status of
inputs are
shown in the
emulator.
You can close the emulator for now by clicking the cross in the top-right corner of the window.
Reading Inputs with Code
As you saw earlier, you can control outputs using the function digital_write . Logically,
as you might expect, to get the value of inputs, you use the digital_read function. Let's
write a simple example:
Type the following Python code interactively:
import piface.pfio as pfio
pfio.init()
pfio.digital_read(0)
Python prints 0 .
Hold down button number S1 and type pfio.digital_read(0) again.
Python prints 1 .
Now read the next button along (S2). Type pfio.digital_read(1) . Notice how
the argument of the function specifies which input to read.
Search WWH ::




Custom Search