int count;

void alarm_wakeup(int i)

{
struct itimerval d_wait;
d_wait.it_interval.tv_sec = 0;
d_wait.it_interval.tv_usec = 0;
d_wait.it_value.tv_sec = 1; /* 10 seconds timer */
d_wait.it_value.tv_usec = 0;
if ( ##condition## )
{
setitimer(ITIMER_REAL, &d_wait,0);
count++;

/*put a condition or do some action when counter reaches at particular number or amount of delay has been passed*/
}
else
{
count=0;
}
}

void function_p()
{
struct itimerval d_wait;

d_wait.it_interval.tv_sec = 0;
d_wait.it_interval.tv_usec = 0;
d_wait.it_value.tv_sec = 1; /* 10 seconds timer */
d_wait.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &d_wait,0);

signal(SIGALRM,alarm_wakeup); /* set the Alarm signal capture */

}

Call the function “function_p” :
It will initiate a timer and register an alarm signal for “alarm_wakeup” function, so when timer expired by its time, SIGALRM will be raised and alarm_wakeup will be called.

To handle the debouncing condition:
-On detected state re-initiate the timer and increment counter, if detected state is not there (means state is changed) leave the function without re-initiate the timer.

Advertisements