| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- // sucycletimer.h :
- //
- #if !defined(AGD_SUCYCLETIMER_H__C9DBE0F2_F287_4369_8033_7209E0C6C839__INCLUDED_)
- #define AGD_SUCYCLETIMER_H__C9DBE0F2_F287_4369_8033_7209E0C6C839__INCLUDED_
- #include <time.h>
- #include <string.h>
- /////////////////////////////////////////////////////////////////////////////
- // sucycletimer.h - Declarations:
- #define _MSEC_PER_SEC 1000LL
- #define _MSEC_PER_MIN (_MSEC_PER_SEC * 60LL)
- #define _MSEC_PER_HOUR (_MSEC_PER_MIN * 60LL)
- #define _USEC_PER_MSEC 1000LL
- #define _USEC_PER_SEC (_USEC_PER_MSEC * 1000LL)
- #define _USEC_PER_MIN (_USEC_PER_SEC * 60LL)
- #define _USEC_PER_HOUR (_USEC_PER_MIN * 60LL)
- #define _NSEC_PER_USEC 1000LL
- #define _NSEC_PER_MSEC (_NSEC_PER_USEC * 1000LL)
- #define _NSEC_PER_SEC (_NSEC_PER_MSEC * 1000LL)
- #define _NSEC_PER_MIN (_NSEC_PER_SEC * 60LL)
- #define _NSEC_PER_HOUR (_NSEC_PER_MIN * 60LL)
- /////////////////////////////////////////////////////////////////////////////
- typedef long long cy_time_t;
- /////////////////////////////////////////////////////////////////////////////
- #ifdef __cplusplus
- /////////////////////////////////////////////////////////////////////////////
- class CCycleTimer
- {
- public:
- CCycleTimer(unsigned int nMaxCycleIntervalMilliSec1, unsigned int nMaxCycleIntervalMilliSec2 = 0, unsigned int nMaxCycleIntervalMilliSec3 = 0);
- CCycleTimer(const struct timespec *pts1, const struct timespec *pts2 = NULL, const struct timespec *pts3 = NULL);
- virtual ~CCycleTimer(void);
- public:
- static cy_time_t GetNanoTick(struct timespec *pts);
- inline static cy_time_t GetMicroTick(void){
- return CCycleTimer::GetNanoTick(NULL) / _NSEC_PER_USEC;}
- inline static cy_time_t GetMilliTick(void){
- return CCycleTimer::GetNanoTick(NULL) / _NSEC_PER_MSEC;}
- inline static cy_time_t Timespec2NanoSec(const struct timespec &rts){
- return (cy_time_t)rts.tv_sec * _NSEC_PER_SEC + (cy_time_t)rts.tv_nsec;}
- inline static void NanoSec2Timespec(cy_time_t n, struct timespec &rts){
- rts.tv_sec = (time_t)(n / _NSEC_PER_SEC);
- rts.tv_nsec = (__syscall_slong_t)(n % _NSEC_PER_SEC);
- }
- inline static cy_time_t CompareTimespec(const struct timespec &rts1, const struct timespec &rts2){
- if(rts1.tv_sec != rts2.tv_sec)
- return (cy_time_t)rts1.tv_sec - (cy_time_t)rts2.tv_sec;
- else
- return (cy_time_t)rts1.tv_nsec - (cy_time_t)rts2.tv_nsec;
- }
- inline static cy_time_t TimespecDiffNanoSec(const struct timespec &rts1, const struct timespec &rts2){
- cy_time_t s = 0;
- if(rts1.tv_sec != rts2.tv_sec)
- s = ((cy_time_t)rts1.tv_sec - (cy_time_t)rts2.tv_sec) * _NSEC_PER_SEC;
- return s + (cy_time_t)rts1.tv_nsec - (cy_time_t)rts2.tv_nsec;
- }
-
- inline void Trigger(void){
- ::clock_gettime(CLOCK_MONOTONIC, &m_ts);
- }
-
- cy_time_t GetNanoSecElapsed(const struct timespec *ptsComparand = NULL) const;
- inline cy_time_t GetMicroSecElapsed(const struct timespec *ptsComparand = NULL) const {
- return GetNanoSecElapsed(ptsComparand) / _NSEC_PER_USEC;
- }
- inline cy_time_t GetMilliSecElapsed(const struct timespec *ptsComparand = NULL) const {
- return GetNanoSecElapsed(ptsComparand) / _NSEC_PER_MSEC;
- }
- inline cy_time_t GetRemainingCycleNanoSec1(const struct timespec *ptsComparand = NULL) const {
- return m_nMaxCycleNanoSec1 - GetNanoSecElapsed(ptsComparand);
- }
- inline cy_time_t GetRemainingCycleNanoSec2(const struct timespec *ptsComparand = NULL) const {
- return m_nMaxCycleNanoSec2 - GetNanoSecElapsed(ptsComparand);
- }
- inline cy_time_t GetRemainingCycleNanoSec3(const struct timespec *ptsComparand = NULL) const {
- return m_nMaxCycleNanoSec3 - GetNanoSecElapsed(ptsComparand);
- }
-
- inline void SetMaxCycleInterval11(unsigned int nMilliSec){
- m_nMaxCycleNanoSec1 = nMilliSec * _NSEC_PER_MSEC;
- }
-
- inline void SetMaxCycleInterval12(unsigned int nMilliSec){
- m_nMaxCycleNanoSec2 = nMilliSec * _NSEC_PER_MSEC;
- }
-
- inline void SetMaxCycleInterval13(unsigned int nMilliSec){
- m_nMaxCycleNanoSec3 = nMilliSec * _NSEC_PER_MSEC;
- }
-
- static void TimespecAddSubNanoSec(struct timespec &rts, cy_time_t nNanoSec);
- inline int Sleep1(bool bResumeOnIntr = false) const {
- return Sleep(m_nMaxCycleNanoSec1, bResumeOnIntr);
- }
- inline int Sleep2(bool bResumeOnIntr = false) const {
- return Sleep(m_nMaxCycleNanoSec2, bResumeOnIntr);
- }
- inline int Sleep3(bool bResumeOnIntr = false) const {
- return Sleep(m_nMaxCycleNanoSec3, bResumeOnIntr);
- }
- private:
- int Sleep(const cy_time_t &rnMaxCycleNanoSec, bool bResumeOnIntr) const;
- private:
- struct timespec m_ts;
- struct timespec m_res;
- cy_time_t m_nMaxCycleNanoSec1;
- cy_time_t m_nMaxCycleNanoSec2;
- cy_time_t m_nMaxCycleNanoSec3;
- };
- /////////////////////////////////////////////////////////////////////////////
- #endif // __cplusplus
- /////////////////////////////////////////////////////////////////////////////
- #endif // !defined(AGD_SUCYCLETIMER_H__C9DBE0F2_F287_4369_8033_7209E0C6C839__INCLUDED_)
|