i want to program in C a simple sequential control with the raspberry. The current task is: If a button is pressed, something (e.g. a LED) shall be switched on for 5 seconds and then go off itself. But if the button is pressed within the 5s window the LED should shut off immmediatley.
Because I need to do this for about 10 buttons, while also doing other stuff, my first idea was to create a thread for each waiting. This seems fairly easy for the 5second waiting part. But how do I achieve the stopping on the next press of the button?
So far the code is roughly as follows:
#include "wiringPi.h"
#define BUTTON_PIN 15
#define OUTPUT_PIN 7
void buttonInterrupt(void){
// do some anti jitter
if (buttonPressedFirstTime){
piThreadCreate(waitingThread);
}
if (buttonPressedSecondTime){
// do what???
}
}
PI_THREAD (waitingThread){
digitalWrite(OUTPUT_PIN,HIGH);
delay(5000);
digitalWrite(OUTPUT_PIN_LOW);
return 0;
}
int main(Void){
wiringPiSetup();
pinMode(BUTTON_PIN,INPUT);
pinMode(OUTPUT_PIN,OUTPUT);
wiringPiISR(BUTTON_PIN,INT_EDGE_RISING,buttonInterrupt);
while(1){
//do other regular stuff
}
return 0;
}
But how do I handle the second press of the button?
I also plan to get away from the wiriingPi pthread interface. I will use pthread myself, because this enables me to provide arguments to the started thread. So I could start the thread with the PIN it should use as argument.
But interrupting the thread on the second press of the button and switching the corresponding pin off is not solved, yet. I was thinking of maybe using libev, replacing the delay by a ev_timer. But I am not sure, whether I could skip the rest of the timer on the second button press.
So do you have an idea how to achieve this very simple sequential control? Thank you in advance.
select
– Eddy_Em Jan 14 '16 at 08:20