Hardware Reference
In-Depth Information
writable files called export and unexport . You're going to need to use the export device. The syntax for this special
device is just to echo the pin number into it. You will need to run this command to export GPIO-04:
# echo 4 > /sys/class/gpio/export
Now take a look at /sys/class/gpio again. You should now see a directory called gpio4 . This directory is how
you will control the GPIO pin. Take a look at Figure 5-10 and you can see the contents of my GPIO-04 directory.
Figure 5-10. The contents of my GPIO-04 directory
You can now work on doing something with your GPIO pin. This directory contains the real business end of the
GPIO subsystem. I what to give you a quick overview of each of the main special files in this directory and what value
types they may accept. Table 5-1 contains the descriptions of some of the most useful special files.
Table 5-1. A listing of the most useful special files
Name
Accepted Value
Description/Use
Direction
In (Input), out (Output)
Sets the GPIO pin as input or output
Value
0 (low), 1 (high)
Sets the initial pin state high or low and reads the current state
Edge
none, rising, falling
Sets the signal edge type
Active_low
0 (false), 1 (true)
Indicates whether the pin is active when driven low
Given that you need to sense the GPIO pin's state, you're going to need to set it up as an input. From Table 5-1
you can easily see that you need to use the direction special file. To set the direction, run this command:
# echo in > /sys/class/gpio/gpio4/direction
Once you have done that, you can read the state of the GPIO pin. Once again by looking at Table 5-1 you can see
that you may want to take a look at the value special file. You can do this if you run the following command:
# cat /sys/class/gpio/gpio4/value
Now if all has gone well this should echo “1” to the terminal. Why 1? Take a look at Table 5-1 again and remember
that if the GPIO pin is high its value will be 1. The GPIO pin is high because you have the pull-up resistor pulling the pin
up to a high state. Now when you step on your pressure mat you complete the circuit and the GPIO pin is brought into a
low state or “0”. Take a look at Figure 5-11 . In the first reading of the value file you can see it is outputting 1 because you
are not standing on the mat. Then stand on the mat and re-read the value file: now you can see it's outputting 0.
 
 
Search WWH ::




Custom Search