Skip to content
Giacomo Trudu edited this page Nov 22, 2013 · 2 revisions

Home > [Getting started](Getting started) [(Italiano)](Per cominciare)

Raspberry Pi's GPIO connector

The Raspberry Pi's GPIO connector provides 17 I/O pins:

Raspberry Pi's GPIOs

You can find more information about Raspberry Pi's hardware on wiki at http://elinux.org.

Creating your first program

The below program is an "Hello world" example: a simple software to blink a LED. It controls the LED through the GPIO #11 using the singleton rpihw::gpio, the simplest interface to the GPIO connector.

// Include some header files
#include <rpi-hw.hpp>
#include <rpi-hw/time.hpp>
#include <rpi-hw/gpio.hpp>

// Use Rpi-hw namespace
using namespace rpihw;

int
main( int argc, char *args[] ) {

    // GPIO controller interface
    gpio &io = gpio::get();

    // Set the output GPIO to the LED
    io.setup( 11, OUTPUT );

    // Main loop
    for ( ;; ) {

        // Turn it on
        io.write( 11, HIGH );

        // Wait some time
        time::sleep(1);

        // Turn it off
        io.write( 11, LOW );

        // Wait some time
        time::sleep(1);
    }

    return 0;
}

To compile this example (or any other programs using the library), simply run the following command:

$ g++ `pkg-config --libs --cflags rpi-hw` <SOURCE> -o <TARGET>

Obviously you have to replace <SOURCE> and <TARGET> with the path to the source file and the name of the executable you want to build.

Now, you can run the created executable, making sure that it has permissions to access /dev/mem:

$ sudo ./example

Building your Rpi-hw project with CMake

A best approach to building large projects is using CMake. For this reason, Rpi-hw provides its own CMake module to include the library into your project. Here's an example of CMakeLists.txt:

# CMake's configuration

# Set the minimum required version of CMake for this project
cmake_minimum_required( VERSION 2.6 )

# Find Rpi-hw library on the operating system
find_package( RpiHw )

# Check if the library was found
if ( RpiHw_FOUND )

    # Set the path to the header files
    include_directories( ${RpiHw_INCLUDE_DIRS} )

    # Compile the project
    add_executable( <TARGET> ${CMAKE_SOURCE_DIR}/<SOURCE> )

    # Link the library to the project
    target_link_libraries( <TARGET> ${RpiHw_LIBRARIES} )

endif()

Also in this case you have to replace <SOURCE> and <TARGET> with the path to the source file and the name of the executable you want to build.