Hardware Reference
In-Depth Information
The time-out is specified as -1 in argument 3, so
poll(2)
will
wait forever, if necessary.
4.
When there is new data for the GPIO input,
poll(2)
returns
and
rc
will be greater than zero, breaking out of the loop.
5.
6.
The program must rewind to the beginning of the pseudo file
with
lseek(2)
(line 118).
Finally, the text is read from the
value
file in line 119.
7.
Step 6 can be omitted if you only need notification. However, to read the correct data,
a rewind to the start of the pseudo file is required.
The program shown also checks whether the signal handler was called. If it sees that
variable
is_signaled
has been set, the routine
gpio_poll()
returns -1 to indicate to the
caller that a program exit is needed (lines 112 to 113).
Test Run
A test was performed using a GPIO output pin (27) wired to the input pin (17). In one
session, GPIO output pin 27 was changed from 0 to 1 and back. The events were captured
in the other session, running
./evinput
.
■
If the reader compiles the programs using the included makefile for each
program, the programs are automatically built to use
setuid
root. Doing this allows them
to run with root privileges, without needing to use the
sudo
command.
Note
The following is a session output obtained from the
./evinput
run. The output
pauses after reporting the first line (line 4). Following that, new lines appear whenever
the input pins change state.
1 $ ./evinput 17
2 Monitoring for GPIO input changes:
3
4 GPIO 17 changed: 0
5 GPIO 17 changed: 1
6 GPIO 17 changed: 0
7 GPIO 17 changed: 1
8 ^C
9 $