/****************************************************************************/
/*    Timer.h : header file for the CPA Timer library.						*/
/*    Author : F.JENTEY 													*/
/*    Last update : 04/03/97										        */
/****************************************************************************/
 
#ifndef _TIMER_H
#define _TIMER_H
 
 
/* Define a 16 bits unsigned type */
#ifndef u_short
#define u_short	unsigned short
#endif
 
/* Define a 32 bits unsigned type */
#ifndef u_long
#define u_long	unsigned long
#endif
 
 
#include "Struct.h"
#include "specif/counter.h"
 
/* Define prototype for event callback  declaration */
typedef void (td_fn_vTimerEventCallback)();
typedef td_fn_vTimerEventCallback* td_p_fn_vTimerEventCallback;
 
 
 
/* Number of timer available */
#define     C_uwTimerMaxCount       16
 
/* Define the timer state */
#define     C_wTimerUnused          0
#define     C_wTimerReady           1
#define     C_wTimerRunning         2
#define     C_wTimerStop            3
#define     C_wTimerPaused          4
 
#define     C_wTimerEvent           5
 
 
/*
    tdsTimer : Contains informations for each timer.
*/
typedef struct _tdstTimer
{
    /* Define the type of the timer */
    short           m_wTimerType;
 
    /* Define the timer state */
    short           m_wTimerState;
 
    /* Timer init value */
    stTimerCount    m_stTimerCount_InitValue;
 
    /* Timer last access value */
    stTimerCount    m_stTimerCount_LastValue;
 
    /* Timer total value */
    stTimerCount    m_stTimerCount_TotalValue;
 
    /* Event timer Id if the timer is an event timer. */
    u_long          m_ulEventId;
 
    /* A pointer to the callback function of the event */
    td_p_fn_vTimerEventCallback  m_p_fn_vEventCallback;
 
} tdstTimer;
 
 
/* All functions return a short value that should be compared with this constant */
#define		C_wTimerError			      32767
#define		C_wTimerTrue			      32766
#define		C_wTimerFalse			      32765
 
/* Define timer accuracy. Use this value with wTimerType parameter */
#define		C_wTimerFrequencyLow	  0
#define		C_wTimerFrequencyMedium	1
#define		C_wTimerFrequencyHigh	  2
 
/* Value to use with the fn_wTimerCreateEvent function to specified if the	*/
/* callback function must be called one time only or periodically.			*/
#define		C_ulTimerEventOneTime	  0
#define		C_ulTimerEventPeriodic	1
 
 
 
/**************************************/
#ifndef CPA_EXPORT
#if defined(CPA_WANTS_IMPORT)
#define CPA_EXPORT __declspec(dllimport)
#elif defined(CPA_WANTS_EXPORT)
#define CPA_EXPORT __declspec(dllexport)
#else
#define CPA_EXPORT
#endif
#endif
/**************************************/
 
 
/******************************************************/
/* Macro for old prototypes compatibilty              */
/******************************************************/
 
#define fn_wTimerInitLibrary           TMR_fn_wInitLibrary
#define fn_wTimerExitLibrary           TMR_fn_wExitLibrary
#define fn_wTimerCreate(A)             TMR_fn_wCreateTimer(A)
#define fn_wTimerStart(A)              TMR_fn_wStartTimer(A)
#define fn_wTimerStop(A)               TMR_fn_wStopTimer(A)
#define fn_wTimerRead(A,B)             TMR_fn_wReadTimer(A,B)
#define fn_wTimerReset(A)              TMR_fn_wResetTimer(A)
#define fn_wTimerDestroy(A)            TMR_fn_wDestroyTimer(A)
#define fn_wTimerPause(A)              TMR_fn_wPauseTimer(A)
#define fn_wTimerGetFrequency(A,B)     TMR_fn_wGetTimerFrequency(A,B)
#define fn_wTimerDelay(A)              TMR_fn_wWait(A)
#define fn_ulTimerDelayUnit(A)         TMR_fn_ulGetWaitTickPerSecond(A)
#define fn_wTimerCreateEvent(A,B,C,D)  TMR_fn_wCreateEvent(A,B,C,D)
#define fn_ulTimerFrameCycleNumber     TMR_fn_ulFrameCycleNumber
#define fn_ulTimerMonitorFrequency     TMR_fn_ulMonitorFrequency
 
 
 
 
 
 
 
/************************************************************************/
/* All functions return a short type value that must be compared with   */
/* C_wTimerError. If there is an error, you can read the error code		*/
/* with the error management functions.									*/
/************************************************************************/
 
#if defined(__cplusplus)
extern "C"
{
 
/*
  Makes some internal initializations.
  In	- nothing.
  Ou	- C_wTimerError if the function has failed and zero otherwise.
*/
CPA_EXPORT short __stdcall TMR_fn_wInitLibrary( void );
 
 
/*
  Release the ressource the library has used.
  In	- nothing.
  Out	- nothing.
*/
CPA_EXPORT short __stdcall TMR_fn_wExitLibrary( void );
 
 
/*
  This function create a timer and return an handle to this timer.The parameter
  wTimerType define the resolution of the timer and must set to one of the
  C_wTimerType Flag.
  In	- timer precision in wTimerType (must be one of the timer type flag).
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wCreateTimer( short wTimerType );
 
 
/*
  Start a timer.
  In	- the handle of the timer to start in wTimerType.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wStartTimer( short wTimerHandle );
 
 
/*
  Stop a Timer.
  In	- the handle of the timer to stop in wTimerType.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wStopTimer( short wTimerHandle );
 
 
/*
  Read a timer count.
  In	- the handle of the timer to read in wTimerType.
		- a pointer to a stTimerCount structure to be filled with the value
		  of the timer in p_ulFrequency.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wReadTimer( short wTimerHandle, stTimerCount *p_stTimerState );
 
 
/*
  Reset a timer.
  In	- the handle of the timer to reset in wTimerType.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wResetTimer( short wTimerHandle );
 
 
/*
  Set a timer with a specific value. Afeter a call of this function the Timer is paused.
  In	- the handle of the timer to reset in wTimerType.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wSetTimerValue( short wTimerHandle, stTimerCount *p_stValue );
/*
  Pause a timer.
  In	- the handle of the timer to reset in wTimerType.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wPauseTimer( short wTimerHandle );
 
 
/*
  Destroy a timer.
  In	- the handle of the timer to destroy in wTimerType.TMR_fn_wPauseTimer
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wDestroyTimer( short wTimerHandle );
 
 
/*
  Give the precision of a timer.
  In	- the handle of the  timer to query in wTimerType.
		- a pointer to an u_long to be filled with the timer frequency in
		  p_ulFrequency.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wGetTimerFrequency( short wTimerHandle , u_long *p_ulFrequency );
 
 
/*
  This function waits a given length. Th time unit should be the thousandth of second
  but check it with the next function.
  In	- time to wait in ulTimeToWait
  Out	- Nothing.
*/
CPA_EXPORT short __stdcall TMR_fn_wWait( u_long ulTimeToWait );
 
 
/*
  This function returns the time unit of the fn_wTimerDelay function.
  In	- a pointer to an u_long to be filled with the time unit.
  Out	- nothing.
*/
CPA_EXPORT short __stdcall TMR_fn_ulGetWaitTickPerSecond(u_long *p_ulWaitFrequency);
 
 
/*   
  This function create a callback timer.
  In	- the period at the end of which the callback function will be called
		  in ulPeriod.
		- a pointer to the callback function in p_fn_vEventCallback.
		- the timer accuracy in wTimerType.
		- tell if the callback function must be called periodically or just
		  one time in ulTimerEventType.
  Out	- C_wTimerError or zero.
*/
CPA_EXPORT short __stdcall TMR_fn_wCreateEvent(
                                             u_long ulPeriod,
							                 td_p_fn_vTimerEventCallback p_fn_vEventCallback,
							                 short wTimerType,
							                 u_long ulTimerEventType
							                );
 
 
/*
  This function return the length of a frame. The units length is the tick
  of a C_wTimerFrequencyHigh timer type (so the return value is the number CPU cycle).
  Return 0 if an error occured.
  In	- nothing.
  Out	- an u_long value containing the number of cpu cycle of a frame.
*/
CPA_EXPORT u_long __stdcall TMR_fn_ulFrameCycleNumber( void );
 
 
/*
  Return the monitor frequency multiples by 100. Return 0 the function can't
  find the refresh rate.
  In	- nothing.
  Out	- an u_long value containing the video display mode vertical frequency.
*/
CPA_EXPORT u_long __stdcall TMR_fn_ulMonitorFrequency( void );
 
}
 
 
 
#else	/* C prototype */
 
extern CPA_EXPORT short __stdcall TMR_fn_wInitLibrary( void );
extern CPA_EXPORT short __stdcall TMR_fn_wExitLibrary( void );
extern CPA_EXPORT short __stdcall TMR_fn_wCreateTimer( short wTimerType );
extern CPA_EXPORT short __stdcall TMR_fn_wStartTimer( short wTimerHandle );
extern CPA_EXPORT short __stdcall TMR_fn_wStopTimer( short wTimerHandle );
extern CPA_EXPORT short __stdcall TMR_fn_wReadTimer( short wTimerHandle, stTimerCount *p_stTimerState );
extern CPA_EXPORT short __stdcall TMR_fn_wResetTimer( short wTimerHandle );
extern CPA_EXPORT short __stdcall TMR_fn_wSetTimerValue( short wTimerHandle, stTimerCount *p_stValue );
extern CPA_EXPORT short __stdcall TMR_fn_wDestroyTimer( short wTimerHandle );
extern CPA_EXPORT short __stdcall TMR_fn_wPauseTimer( short wTimerHandle );
extern CPA_EXPORT short __stdcall TMR_fn_wGetTimerFrequency( short wTimerHandle , u_long *p_ulFrequency );
extern CPA_EXPORT short __stdcall TMR_fn_wWait( u_long ulTimeToWait );
extern CPA_EXPORT short __stdcall TMR_fn_ulGetWaitTickPerSecond(u_long *p_ulWaitFrequency);
extern CPA_EXPORT short __stdcall TMR_fn_wCreateEvent( u_long ulPeriod,
							                           td_p_fn_vTimerEventCallback p_fn_vEventCallback,
							                           short wTimerType,
							                           u_long ulTimerEventType
							                         );
extern CPA_EXPORT u_long __stdcall TMR_fn_ulFrameCycleNumber( void );
extern CPA_EXPORT u_long __stdcall TMR_fn_ulMonitorFrequency( void );
 
#endif 
 
#endif
