#include <unistd.h>
#include <sys/time.h>
#include "timer.h"


unsigned long stamp()
{
	struct timeval tv;

	gettimeofday( &tv, NULL );

	return tv.tv_sec * 1000000 + tv.tv_usec;
}


CTimer::CTimer( )
{
	prev = stamp();
}


void CTimer::init( )
{
	prev = stamp();
}


void CTimer::wake( unsigned long usec )
{
	long sleep_time = usec-(stamp()-prev);

	if ( sleep_time>0 )
		usleep( sleep_time );

	prev = stamp();
}


bool CTimer::timeout( unsigned long usec )
{
	return stamp()-prev > usec;
}


unsigned long CTimer::elapsed(  )
{
	return stamp()-prev;
}

