wiringPi數字量輸出

talk is cheap,show me your code.ios

#include<iostream>
#include<wiringPi.h>
#include <signal.h>

using namespace std;

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.
#define	LED	0

void setup();

const int LEDPin = 11;
const int ButtonPin = 1;

static void sig_usr(int);

int main(int argc,char** argv)
{
    int dly=-1;
    if(argc>1)
        dly=atoi(argv[1]);
    if(dly<0)
        dly=500;
        
    wiringPiSetup();
    pinMode(LED, OUTPUT);
    
    if(signal(SIGINT,sig_usr)==SIG_ERR)
    {
        cout<<"Can not catch SIGINT."<<endl;
        exit(1);
    }
    
    while(1)
    {
        digitalWrite (LED, HIGH);
        cout<<"LED ON"<<endl;
        delay (dly);
        digitalWrite (LED, LOW);
        cout<<"LED OFF"<<endl;
        delay (dly);
    }
    
    
    return 0;
}

static void sig_usr(int signo)
{  
    if(signo==SIGINT)
    {
        cout<<"Catch SIGINT, turn off LED and Exit."<<endl;
        digitalWrite (LED, LOW);
        exit(0);
    }
}

  不使用signal也是沒啥問題的,可是這樣的話,當使用Ctrl+C結束程序的時候,LED多是點亮的,也多是關閉的,這就有點不太好,最好是退出的時候作一些清理方面的工做。固然了,若是是使用Ctrl+\退出的,那就仍是老樣子。git

相關文章
相關標籤/搜索