wiringPi数字量输出
talk is cheap,show me your code.
#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+\退出的,那就还是老样子。