#BeagleBone Black C++ GPIO library for beginners

0. List of contents

1. Requirements

  • Beagle Bone Black
  • Basic C++ knowledge
  • Basic python knowledge

2. BBB GPIO Pins

If you have ever tried to connect some electronics (i.e sensors) to your BBB, you probably noticed that the names of pins differ. For instance GPIO 38 is under P8_03 head pin, some of pins are used by default and some are not.
It would be convenient to have a tool/library which takes pin head name and return the pin number (needed in order to use gpio sysfs).
With that library (BeagleBoneBlack-GPIO) your work will be a bit easier. Searching and handling on pins is automated.

Great! That's not enough, you will also need to know to which port connect the cable 🙂
Below is presented schema of P8 & P9 in BBB (taken from here):
resizedimage600667-hwio-beaglebone-ports2

Library is mostly based on detailed specification made by derek molloy (good job!). Documents are available for download below (click on image P9, P8) or from here:

Beaglebone Black P9 HeaderTableBeaglebone Black P8 HeaderTable

3. Example: Switching on/off the led

Adafruit published an article how to control led via GPIO. As you will see they are using their own python library which does mostly the same as BeagleBoneBlack-GPIO.

Python example:

import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_10", GPIO.OUT)

while True:
    GPIO.output("P8_10", GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output("P8_10", GPIO.LOW)
    time.sleep(0.5)

C++ example:

#include
#include "GPIO/GPIOManager.h"
#include "GPIO/GPIOConst.h"

int main() {
  GPIO::GPIOManager* gp = GPIO::GPIOManager::getInstance();
  int pin = GPIO::GPIOConst::getInstance()->getGpioByKey("P8_10");

  gp->setDirection(pin, GPIO::OUTPUT);

  while(true){
	  gp->setValue(pin, GPIO::HIGH);
	  sleep(5);
	  gp->setValue(pin, GPIO::LOW);
	  sleep(5);
  }

  gp->~GPIOManager();

  return 0;
}

4. Important methods

1. Operations on names

    • GPIOConst::getGpioByKey - i.e "P8_10"
    • GPIOConst::getGpioByName - i.e "GPIO0_7"
    • GPIOConst::getGpioNameByPin - i.e "12"
    • GPIOConst::isPinAllocatedByDefault - refering to derek molloy docs

2. Operations on GPIO sysfs descriptors:

    • GPIOManager::exportPin - Export pin
      (equivalent to i.e echo "68" > /sys/class/gpio/export)
    • GPIOManager::unexportPin - Unexport pin
      (equivalent to i.e echo "68" > /sys/class/gpio/unexport)
    • GPIOManager::setDirection - Set direction
      (equivalent to i.e echo "in" > /sys/class/gpio68/direction)
    • GPIOManager::getDirection - Get direction
      (equivalent to i.e cat /sys/class/gpio68/direction)
    • GPIOManager::setValue - Set value
      (equivalent to i.e echo "1" > /sys/class/gpio68/value)
    • GPIOManager::getValue - Get value
      (equivalent to i.e cat /sys/class/gpio68/value)
    • GPIOManager::setEdge - Set edge
      (equivalent to i.e echo "rising" > /sys/class/gpio68/edge)
    • GPIOManager::getEdge - Get edge
      (equivalent to i.e cat /sys/class/gpio68/edge)
    • GPIOManager::waitForEdge - Wait for edge event
    • GPIOManager::countExportedPins - Count already exported pins
    • GPIOManager::clean - Unexport all of already exported pins

5. Download

The latest version is on github: https://github.com/mkaczanowski/BeagleBoneBlack-GPIO