/* Example that blinks a LED on a Raspberry Pi using my GPIOCPP class. GPIOCPP version 0.09, march 18, 2017 COPYLEFT - Pieter Suurmond. To compile this example with GCC, use the '-std=c++11' option. For example: g++ -Wall -O2 -std=c++11 blink.cpp gpiocpp.cpp -o blink But you may also use the supplied Makefile: just type 'make'. Before running this blink executable, connect a LED, with series resistor!, from GPIO pin 27 to GND. Make sure the correct pin number and the correct Pi model number are specified below! Run with './blink' and watch for tiny glitches, short flashes, especially during the last phase (at 1000 Hz). These are possibly due to interrupts by other processes running on the Pi. */ #include // For cout. #include // For usleep(). #include "gpiocpp.h" // Our GPIOCPP class. int main() { const char ledpin = 27; // Broadcom GPIO27 as output pin. Using const or // constexpr here compiles to faster code! try { // First argument is Pi model number: 1 (for ARMv6) or 2 (for ARMv7). // Second argument is pathname to mem-map: "/dev/mem" or "/dev/gpiomem". GPIOCPP gpio(2, "/dev/gpiomem"); gpio.config_write(ledpin); // Configure as output pin. std::cout << "Blink LED on GPIO" << (int)ledpin << " 4 times.\n"; for (int t = 0; t < 4; t++) { usleep(500000); // 500 milliseconds. gpio.setpin(ledpin); // LED on. usleep(500000); gpio.clearpin(ledpin); // LED off. } std::cout << "Now approximately 10 Hz, for 4 seconds.\n"; for (int t = 0; t < 40; t++) { usleep(50000); // 50 milliseconds. gpio.setpin(ledpin); usleep(50000); gpio.clearpin(ledpin); } std::cout << "Now 100 Hz, for 4 seconds.\n"; for (int t = 0; t < 400; t++) { usleep(5000); // 5 milliseconds. gpio.setpin(ledpin); usleep(5000); gpio.clearpin(ledpin); } std::cout << "Finally 1000 Hz, duty cycle=10%.\n"; for (int t = 0; t < 4000; t++) { usleep(900); // 500 microseconds. gpio.setpin(ledpin); usleep(100); gpio.clearpin(ledpin); } } catch (const char* errtxt) { // When something goes wrong the constructor std::cout << errtxt; // throws an exception describing the error. return 1; } std::cout << "Done.\n"; return 0; }