Page 1 of 1
NXC: start/stop timer?
Posted: 22 Feb 2011, 20:45
by HaWe
hi,
I can't find anything about the keyword "timer":
how to start / stop / reset timers?
how many different timers can I handle in 1 program?
(the somewhat strange-sounding keyword CurrentTick I could find, but nothing else, unfortunately) :(
Re: NXC: start/stop timer?
Posted: 22 Feb 2011, 20:58
by h-g-t
As far as I can see you only get FirstTick, which is the value when the program began running and CurrentTick which is the present value. I think you have no control over the system timer and can only read it.
The only 'timer' you get is the wait statement.
The only other one I have seen so far is the sleep timer.
Re: NXC: start/stop timer?
Posted: 22 Feb 2011, 21:27
by mattallen37
Because you can know the Tick of when the program starts, as well as the current Tick, you can calculate almost any time/timer value from that. You could even "reset" it, by simply subtracting all future readings by the current tick (as it is at that one point).
Re: NXC: start/stop timer?
Posted: 22 Feb 2011, 21:40
by HaWe
hgt, matt,
thanks! I think I falsly recalled some old stuff of the old RCX times...
@afanofosc:
Why didn't you call the Timer Timer but Tick instead? Wouldn't it have been better to use the C keywords, e.g. Timer(0) ?
Re: NXC: start/stop timer?
Posted: 22 Feb 2011, 21:53
by HaWe
ps
any smart proposals how to easily handle 8 different timers in a program?
Sth like 8 independent different stop watchs:
StartTimer[n]
SetTimer[n]
HoldTimer[n]
ResetTimer[n]
(it's been hard day... )
Re: NXC: start/stop timer?
Posted: 23 Feb 2011, 00:25
by muntoo
For Doc, master of calculating FFTs and Gamma Loophole Phi Uber Functions in his feet (his head is not worthy of such pathetically easy stuff), but is too busy finding THE Question to bother himself with such a simple program:
Code: Select all
struct T
{
bool stopped;
unsigned long strt;
unsigned long lastStop;
unsigned long total;
} t;
unsigned long T_get()
{
if(t.stopped)
return(t.total);
return(CurrentTick() - t.lastStop + t.total);
}
void T_stop()
{
if(!(t.stopped))
{
t.total = T_get();
t.stopped = true;
}
}
void T_start()
{
if(t.stopped)
{
t.stopped = false;
t.lastStop = CurrentTick();
}
}
void T_set(unsigned long time)
{
t.total = time;
t.lastStop = CurrentTick();
}
void T_init()
{
t.stopped = false;
t.strt = CurrentTick();
t.lastStop = CurrentTick();
t.total = 0;
}
void T_reset()
{
T_init();
}
Just fix this up. (There may be bugs, though I doubt it.)
Re: NXC: start/stop timer?
Posted: 23 Feb 2011, 04:38
by afanofosc
Muntoo offers some great code. Alternatively you can simply use 8 global unsigned long variables. Store CurrentTick into which ever one you want to "clear" and then its value becomes the difference between CurrentTick() and its current value. You can wrap that in functions if you like or just do the math inline. Macros could also be used to hide the trivial amount of code that clears or reads a timer value.
Code: Select all
unsigned long __timer_global_0, __timer_global_1, __timer_global_2, __timer_global_3;
unsigned long __timer_global_4, __timer_global_5, __timer_global_6, __timer_global_7;
#define CLEAR_TIMER(_n) asm { \
compchk EQ, isconst(_n), TRUE \
compchk LT, _n, 8 \
compchk GTEQ, _n, 0 \
gettick __timer_global_##_n \
}
#define READ_TIMER(_n) asm { \
compchk EQ, isconst(_n), TRUE \
compchk LT, _n, 8 \
compchk GTEQ, _n, 0 \
gettick __URETVAL__ \
sub __URETVAL__, __URETVAL__, __timer_global_##_n \
}
John Hansen
Re: NXC: start/stop timer?
Posted: 23 Feb 2011, 09:59
by HaWe
muntoo, John, thank you very much! Exactly what I needed! :)
And muntoo: yes, you're oh so right (making fun of me)
(it's really been a hard day's night... ) ;)