ThisIsAreku

wiringPi example

Jul 24th, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <csignal>
  4. #include <cstring>
  5. #include <wiringPi.h>
  6.  
  7. using namespace std;
  8.  
  9. void my_handler(int s){
  10.     cout << endl << "Signal " << s << ", exit" << endl;
  11.     digitalWrite(14, LOW);
  12.     exit(1);
  13. }
  14.  
  15. int main(int argc, char* argv[])
  16. {
  17.     bool debug(false);
  18.     for(int i(0); i < argc; i++)
  19.     {
  20.         if(strncmp(argv[i], "-d", 2) == 0)
  21.             debug = true;
  22.     }
  23.     if(debug)
  24.         cout << "[D] Setup.." << endl;
  25.     if (wiringPiSetup() == -1)
  26.     {
  27.         cerr << "Erreur" << endl;
  28.         return 1;
  29.     }
  30.    
  31.     if(debug)
  32.         cout << "[D] reg signal.." << endl;
  33.     struct sigaction sigIntHandler;
  34.  
  35.     sigIntHandler.sa_handler = my_handler;
  36.     sigemptyset(&sigIntHandler.sa_mask);
  37.     sigIntHandler.sa_flags = 0;
  38.  
  39.     sigaction(SIGINT, &sigIntHandler, NULL);
  40.    
  41.     pinMode(14, OUTPUT);
  42.    
  43.     if(debug)
  44.         cout << "[D] Input.." << endl;
  45.     int delayValue(0);
  46.     cout << "Entrez le délai (en ms): ";
  47.     if (!(cin >> delayValue))
  48.     {
  49.         cout << "Valeur incorrecte, utilisation de 100ms" << endl;
  50.         delayValue = 100;
  51.     }
  52.    
  53.     if(debug)
  54.         cout << "[D] Start.." << endl;
  55.     while(true)
  56.     {
  57.         digitalWrite(14, LOW);
  58.         if(debug)
  59.             cout << "[D] LOW" << endl;
  60.         delay(delayValue);
  61.         digitalWrite(14, HIGH);
  62.         if(debug)
  63.             cout << "[D] HIGH" << endl;
  64.         delay(delayValue);
  65.     }
  66.    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment