An exercise to build a C++ library for the GPIO pins. The programmer can decide what underlying mechanism is used to talk to the pins. In this 3rd post, finally, a Blinky! #include "memmapdevice.h" pin p = pin(21); |
Example program
What happended before?
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
I created a C++ library for the Pi. With a very simple API: A single class pin. With (de)initialisation functions, and a getter + setter. But: can it blink?
The answer is yes. Here is example code:
#include "pin.h" #include "memmapdevice.h" typedef dgpio::pin<dgpio::memmapdevice> pin; #include <stdlib.h> // atoi #include <unistd.h> // usleep int main(int argc, char *const *argv) { pin p = pin(21); p.init(pin::dir::out, pin::status::off); while(true) { p.set(p.get() == pin::status::off ? pin::status::on : pin::status::off); usleep(1000000); } p.deinit(); }
Want to try it yourself? You can help me to improve instructions, by trying it out. See the readme.
github repo: https://github.com/jancumps/cppgpio