src/Timer.cc
author terom
Mon, 15 Dec 2008 14:41:49 +0000
changeset 373 2d36696d9121
parent 300 417183866f35
child 423 947ab54de4b7
permissions -rw-r--r--
use a template (doxygen.conf), plus mainpage docs in kishna-glista.dox

#include "Timer.hh"

Timer::Timer (TimeMS interval) :
    interval(interval), 
    ticks(0), 
    enabled(false),
    last_tick(0)
{

}

TickCount Timer::get_ticks (void) {
    return ticks;
}

void Timer::start (void) {
    last_tick = CL_System::get_time();

    enabled = true;
    single_shot = false;
}
        
void Timer::fire_once (void) {
    last_tick = CL_System::get_time();

    enabled = true;
    single_shot = true;
}

void Timer::keep_alive (void) {
    // ignore if disabled
    if (!enabled)
        return;

    // get current time
    TimeMS now = CL_System::get_time();

    // handle overflows
    if (last_tick > now)
        last_tick = now;
    
    // is the tick over?
    if (last_tick + interval < now) {
        // the length of this tick
        TimeMS tick_length = now - last_tick;
        
        // trigger our signal
        _sig_tick(tick_length);
        
        // update state
        last_tick = now;
        ticks += tick_length / interval;

        // if it was single-shot, disable
        if (single_shot)
            enabled = false;
    }
}