Exemple #1
0
void
notmain()
{
	int timeout;

	flash_led(1, RED|GRN, 4);

	init_timeoutq();
    pfv_t faddr;
    faddr = &blink_led;

	create_timeoutq_event( ONE_SEC, 5 * ONE_SEC, blink_led, RED );
	create_timeoutq_event( 4 * ONE_SEC, 5 * ONE_SEC, blink_led, GRN );
	create_timeoutq_event( 7 * ONE_SEC , 5 * ONE_SEC, blink_led, GRN | RED);

	while (1) 
	{
		if (handle_timeoutq_event()) {
            //blink_led( RED );
			//continue;
		}
		timeout = bring_timeoutq_current();
		wait(timeout);
	}

}
//
// bring the time current
// check the list to see if anything expired
// if so, do it - and either return it to the freelist 
// or put it back into the timeoutq
//
// return whether or not you handled anything
//
int
handle_timeoutq_event( )
{
	struct event *ep;  
	//struct event *head_timeoutq;
	ep = (struct event *)LL_HEAD(timeoutq); //ep now points to the head of TimeoutQ
	 
	//if diff < 1 us then execute the function
	if(ep->timeout - then_usec < 1 )
	{
		//execute function
		pfv_t tmp = ep->go;
		(* tmp)(ep->data);

		//remove from the timeoutq and reinsert back to freelist
		LL_DETACH(timeoutq, ep);

		if(ep->repeat_interval != 0)
		{
			create_timeoutq_event( ep->repeat_interval, ep->repeat_interval, ep->go, ep->data);	
			//update then_usec
//			then_usec -= now_usec();
		}
		else
		{
			//push to freelist
			LL_PUSH(freelist,ep);
		}
		return 1;
	}
	else	
	return 0;
}