/* Example that measures a GPIO pin 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 measure.cpp gpiocpp.cpp -o measure But you may also use the supplied Makefile: just type 'make'. Before running this executable, connect button between GPIO pin 17 and GND. No external pull-up/down resistor is necessary, an internal pull-up is set by software. Make sure the correct pin number and correct Pi model number are specified below! Play with the button after typing on the commandline: ./measure You should see zeros appearing while pressing it down, ones while leaving it released. */ #include // For cout. #include // For usleep(). #include "gpiocpp.h" // Our GPIOCPP class. int main() { const char buttonpin = 17; // Broadcom GPIO17 as input 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_read(buttonpin); // Configure as input pin. gpio.config_pull(buttonpin, PULL::UP); // Install pull-up R. std::cout << "Sampling GPIO" << (int)buttonpin << " at 50 Hz for 16 seconds:\n"; for (int t = 0; t < 16*50; t++) { usleep(20000); // 20 milliseconds. std::cout << gpio.readpin(buttonpin) << std::flush; } //gpio.config_pull(buttonpin, PULL::OFF); // Remove pull resistor. } catch (const char* errtxt) { // When something goes wrong the constructor std::cout << errtxt; // throws an exception describing the error. return 1; } std::cout << "\nDone.\n"; return 0; }