/***************************************************************
 * PrTimer.h
 *
 * This class provides the interface to the timer.
 *
 ****************************************************************/

/*
 * modification history
 *----------------------
 *
 * 02/16/03: Weyn Ong: Version for QNX 6
 * 10/26/97: K.C. Chang: created.
 */

#ifndef _PrTimer_h
#define _PrTimer_h

#include "PrGlobalDefn.h"
#include <sys/types.h>

#define PR_TIMER_OK              1
#define PR_TIMER_ERROR           0

#define PR_TIMER_PERIOD_DEFAULT       1.0E-3
#define PR_TIMER_PERIOD_MAX           100.0    // limit ?
#define PR_TIMER_PERIOD_MIN           1.0E-5   // limit 838.095345 ns

#define PR_TIMER_FREQUENCY_DEFAULT    1/PR_TIMER_PERIOD_DEFAULT
#define PR_TIMER_FREQUENCY_MAX        1/PR_TIMER_PERIOD_MIN
#define PR_TIMER_FREQUENCY_MIN        1/PR_TIMER_PERIOD_MAX

class PrUtility;

class PrTimer
{
 public:
    PrTimer(double frequency = PR_TIMER_FREQUENCY_DEFAULT);

    ~PrTimer();

    int flag() { return flag_; }

    // current frequency
    double period() { return period_; }

    // set the local time_ = 0;
    int zero();     

    // returns the time in sec since the last time zero() was called
    double time();  

    // returns the time in sec since 1/1/1970
    double timeGlobal();  

    // starts the timer
    int start();

    // blocks until the next tick: returns the number of ticks missed.
    int wait();

    // self test routine
    int selfTest(int loop);

 private:
    int flag_;
    double period_;
    int pulseId_;
    int id_;
    double time_;
    PrUtility *utility_;

    // initialize the timer
    int Initialize();

    // error message and returns error flag
    int Error(char *mesg);

    // quit
    int Quit();

    PrTimer(const PrTimer &) {}
};

#endif // _PrTimer_h
