Git Product home page Git Product logo

cppgpio's Introduction

cppgpio

C++ embeddded GPIO library (supports sysfs, memmap register)

Devices supported:

  1. Raspberry Pi
    1. memory mapped register access (/dev/gpiomem)
    2. sysfs with C++ stream (/sys/class/gpio)
    3. sysfs with classic C file IO (/sys/class/gpio)

Use example:

#include "memmapdevice.h"
typedef dgpio::pin<dgpio::memmapdevice> pin;

pin p21 = pin(21);
p21.init(pin::dir::out, pin::status::off);
p21.set(pin::status::on);

p21.init(pin::dir::in);
pin::status status = pin::status::on;
while (status != pin::status::off) {
	status = p21.get();
}

p21.deinit();

Use the include and typedef for the implementation you want to use. Your code will be the same regardless of the choice of GPIO driver selected. In this example, the memmap device driver is used. The library will talk directly to the ARM CPU's registers to control the pins.

// #include <sysfsdevice_filehandler.h>
// typedef dgpio::pin<dgpio::sysfsdevice_filehandler> pin;

// #include "sysfsdevice_stream.h"
// typedef dgpio::pin<dgpio::sysfsdevice_stream> pin;

#include "memmapdevice.h"
typedef dgpio::pin<dgpio::memmapdevice> pin;

API:

namespace dgpio {
template <class D>
class pin {
public:

    enum class status : unsigned { off  = 0u, on };
    enum class dir { in, out };

    pin(unsigned gpio);
	void init(dir dir = dir::in, status status = status::off);
	void deinit();

	status get();
	void set(status status);
	
};	
}
  • constructor: pin(gpio)
  • init: init(dir, initial status)
  • deinit: deinit()
  • getter: get(): status
  • setter: set(status)

Add to your project:

Get the latest version: git clone https://github.com/jancumps/cppgpio.git
Add the src/lib folder to your include path
Add the relevant cpp source file(s) from src/lib to your build list. Ideally, only build and link the one that you're using. See section Small footprint below.
Compiler support: ISO C++ 17 or higher is required. You may actively need to set that (std=c++17) if your build errors.

Examples:

There are 2 examples in the src/ folder:

  • example.cpp: read or write a pin.
  • blinky.cpp: blink a pin

User requirements:

  1. Raspberry Pi
    On Raspberry OS, the user that runs your program has to be in the gpio group. When using Raspberry's tools to create an image, your user will be in that group.

Small footprint:

The code uses C++ constructs that are resolved at compile time.
To keep the firmware size as small as possible, only add the source file (.cpp) of the implementation that you use in the build.

Tested with:

  • Raspberry Pi 3 Model B, raspbian bullseye 32 bit lite, gcc version 10.2.1 20210110 (Raspbian 10.2.1-6+rpi1)

Related blog posts:

C++ gpio library for Raspberry Pi - Pt 1: Design and How To Use
C++ gpio library for Raspberry Pi - Pt 2: Plug In drivers for direct register GPIO, or file system based GPIO

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.