//////////////////////////////////////////////////////////////////////
//
// MicroTime.cpp: implementation of the MicroTime class.
//
// This module is a part of the PMCA COM package. It contains
// precesion timer service routines
//
// Amptek Inc. 2000
//
//////////////////////////////////////////////////////////////////////

#include <windows.h>
#include "MicroTime.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MicroTimeType::MicroTimeType()
{
    LARGE_INTEGER frequency;
    int success = QueryPerformanceFrequency(&frequency);
    m_waiting = FALSE;
    m_microScale = 1000000.0 / (double) frequency.QuadPart;
    success = QueryPerformanceCounter(&m_startTime);
}

LARGE_INTEGER MicroTimeType::GetCounter(void)
{
    LARGE_INTEGER currTime;
    LARGE_INTEGER diff;
    int success = QueryPerformanceCounter(&currTime);
    diff.QuadPart = currTime.QuadPart - m_startTime.QuadPart;
    return (diff);
}

// Get time elapsed since MicroTime object instantiation (microseconds)
double MicroTimeType::Get(void)
{
    LARGE_INTEGER diff = GetCounter();
    double time = (double) diff.QuadPart * m_microScale;
    return time;
}

double MicroTimeType::Wait(double microSec)
{
    double startWait= Get();
    double elapsedTime = Get() - startWait;
    m_waiting = TRUE;
    while ((elapsedTime < microSec) && m_waiting)
        elapsedTime = Get() - startWait;
    m_waiting = FALSE;
    return elapsedTime;
}

void MicroTimeType::CancelWait(void)
{
    m_waiting = FALSE;
    return;
}

