Skip to content

Latest commit

 

History

History
111 lines (62 loc) · 7.48 KB

CppRpiExample1.md

File metadata and controls

111 lines (62 loc) · 7.48 KB

 

 

 

 

 

 

STLQt CreatorRaspbian

 

Raspberry Pi example 1: toggle all GPIO pins on and off is a Raspberry Pi example to toggle all GPIO pins on and off.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppRpiExample1/CppRpiExample1.pro

 


TEMPLATE = app CONFIG += console CONFIG -= qt QMAKE_CXXFLAGS += -std=c++0x SOURCES += main.cpp DEFINES += NDEBUG

 

 

 

 

 

./CppRpiExample1/main.cpp

 


#include <cassert> #include <iostream> #include <boost/lexical_cast.hpp> #include <boost/timer.hpp> //Toggles all GPIO pins every second //Thanks to http://www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/#step-six int main() {   const std::vector<int> gpios = { 2,3,4,7,8,9,10,11,14,15,17,18,22,23,24,25,27 };   while (1)   {     //Set all GPIO pins high     for(int i: gpios)     {       {         const std::string cmd = "echo \"" + boost::lexical_cast<std::string>(i) + "\" > /sys/class/gpio/export";         std::clog << cmd << '\n';         const int error = std::system(cmd.c_str());         assert(!error);       }       {         const std::string cmd = "echo \"out\" > /sys/class/gpio/gpio" + boost::lexical_cast<std::string>(i)+ "/direction";         std::clog << cmd << '\n';         const int error = std::system(cmd.c_str());         assert(!error);       }       {         const std::string cmd = "echo \"1\" > /sys/class/gpio/gpio" + boost::lexical_cast<std::string>(i)+ "/value";         std::clog << cmd << '\n';         const int error = std::system(cmd.c_str());         assert(!error);       }     }     //Wait a second     {       boost::timer t;       while (t.elapsed() < 1.0) {}     }     //Set all GPIO pins low     for(int i: gpios)     {       {         const std::string cmd = "echo \"0\" > /sys/class/gpio/gpio" + boost::lexical_cast<std::string>(i)+ "/value";         std::clog << cmd << '\n';         const int error = std::system(cmd.c_str());         assert(!error);       }       {         const std::string cmd = "echo \"" + boost::lexical_cast<std::string>(i) + "\" > /sys/class/gpio/unexport";         std::clog << cmd << '\n';         const int error = std::system(cmd.c_str());         assert(!error);       }     }     //Wait a second     {       boost::timer t;       while (t.elapsed() < 1.0) {}     }   } }