Reputation: 389
I want to set timer for inputting key and one timer for turning back light off but it use first timer I set and I can't set more than one timer and the second timer didn't work.
i use the code below
int timer1, timer2;
long events;
timer1 = set_timer(8000, EVT_TIMER);
timer2 = set_timer(5000, EVT_TIMER);
while(1){
events = wait_event();
if(events & EVT_KBD){
clr_timer(timer1);
break;
}
else if (events & EVT_TIMER)
{
printf("TIME_OUT");
break;
}
while(1){
events = wait_event();
if(events & EVT_KBD){
clr_timer(timer2);
break;
}
else if (events & EVT_TIMER)
{
printf("TIME_OUT2");
break;
}
}
Upvotes: 1
Views: 173
Reputation: 8116
Another approach is to keep your timers in your own data structure (say a sorted list ordered by the timer expiration time) and to use only one system timer for the first timer to expire (i.e. the first in the sorted list).
When you receive the EVT_TIMER
system event, you fire all the timers whose expiration time is in the past (removing them from the sorted list).
If there is any timer left in the list, you start a new system timer for the new first timer to expire.
There are (at least) two things to be aware of:
when adding a new timer you must check if it does not become the first timer to expire. If so you must cancel the existing system timer with clr_timer()
and set a new system timer for the new first timer to expire (the newly added timer which is now first in the sorted list). Skip the clr_timer()
call when adding a new timer to an empty list (as there should be no system timer active right now)
if you use the read_ticks()
call to calculate the timer expiration times (or for anything else) make sure to handle the case when it's value overflows back to zero (which happens every 49.7 days)
Upvotes: 1
Reputation: 4963
You need to use a different event mask (EVT_TIMER) if you want to capture them as different events. The tricky thing is that you need to be careful about which you use because it may trigger other actions. These events are defined in svc.h
(Note that the mask is a long
and a long
is defined as being 32 bits, so you really don't have anything left after all the standard events are used).
The good news is that set_timer
returns an ID (that's what timer1
and timer2
are in your code). You can then use the SVC_TICKS
API to determine which timer has expired. I wrote a wrapper called "timeRemains" to help me with that.
//First, define "timeRemains"
char timeRemains(long* timer)
{
return SVC_TICKS(0, timer);
}
//Here's what your code may look like:
if(!timeRemains(&timer1))
{
//timer1 has expired. Do whatever you wanted to do when that happens.
//NOTE: you don't know the state of timer2--it may also have expired,
// so you will need to deal with that
}
if(!timeRemains(&timer2))
{
//timer2 has expired. Do whatever you wanted to do when that happens.
//NOTE: even though we are PRETTY sure that timer1 has not yet expired,
// you can't just forget about it. If you are going to exit this polling loop,
// be sure to clear it first.
}
Upvotes: 1