-
Notifications
You must be signed in to change notification settings - Fork 10
The GPIO controller interface
Home > Documentation > [The GPIO controller interface](The GPIO controller interface) [(Italiano)](L'interfaccia del controller GPIO)
The simplest interface to the GPIO connector is the singleton rpihw::gpio. It is a structure that accurately reproduces the connector behavior, without any kind of abstraction. Once instantiated, you have several methods to manage the GPIOs of your Raspberry Pi.
- Set the mode of a GPIO
- Set the state of an ouput pin
- Read the state of an input pin
- Enable pull-up/down resistor of a input pin
The method setup allows you to set the working mode of a GPIO, by deciding if it must be used as output or input pin. The needed arguments are the GPIO number and the new working mode to be set.
// Use the Rpi-hw namespace
using namespace rpihw;
// Get the GPIO controller interface
gpio &io = gpio::get();
// Set the GPIO #11 as output pin
io.setup( 11, gpio::OUTPUT );
// Set the GPIO #9 as input pin
io.setup( 9, gpio::INPUT );
You can change the logic state of a output pin with the method write. It, in addition to the number of GPIO, takes an boolean parameter representing the new logic state.
// Set the GPIO #11 to the high level
io.write( 11, gpio::HIGH );
// Set the GPIO #11 to the low level
io.write( 11, gpio::LOW );
// Set the GPIO #11 to the high level
io.write( 11, 1 );
// Set the GPIO #11 to the low level
io.write( 11, 0 );
With the method read you can read the logic state of an input pin.
// Read the logic state of the GPIO #9
bool state = io.read(9);
Raspberry Pi's GPIOs have internal pull-up or pull-down resistor used to ensure the logic level of an input pin in case of open circuit or high-impedance. You can enable this resistor on an input pin using the method setPullUpDown.
// Use the pull-up resistor on the GPIO #9
io.setPullUpDown( 9, gpio::PULL_UP );
// Use the pull-down resistor on the GPIO #9
io.setPullUpDown( 9, gpio::PULL_DOWN );
// Disable the pull-up/down resistors on the GPIO #9
io.setPullUpDown( 9, gpio::PUD_OFF );