00001 // Copyright CERN 2012 - Developed in collaboration with GSI 00002 /* 00003 * Piece of code adapted for the FESA Framework. 00004 * Original version references: 00005 * 00006 * Portable Agile C++ Classes (PACC) 00007 * Copyright (C) 2004 by Marc Parizeau 00008 * http://manitou.gel.ulaval.ca/~parizeau/PACC 00009 * 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 * 00024 * Contact: 00025 * Laboratoire de Vision et Systemes Numeriques 00026 * Departement de genie electrique et de genie informatique 00027 * Universite Laval, Quebec, Canada, G1K 7P4 00028 * http://vision.gel.ulaval.ca 00029 * 00030 */ 00031 #ifndef TIME_STAMP_COUNTER_H_ 00032 #define TIME_STAMP_COUNTER_H_ 00033 00034 #include <unistd.h> 00035 #include <sys/time.h> 00036 00037 #include <stdint.h> 00038 00039 #define S_UNIT 1.0 //express delay in second 00040 #define MS_UNIT 1.e3 //express delay in milli-second 00041 #define YS_UNIT 1.e6 //express delay in micro-second 00042 #define NS_UNIT 1.e9 //express delay in nano-second 00043 namespace fesa 00044 { 00045 00046 class TsCounter 00047 { 00048 00049 public: 00050 TsCounter(bool inHardware = true) : 00051 mHardware(inHardware) 00052 { 00053 if (mPeriod == 0) 00054 calibrateCountPeriod(); 00055 mValueCount = 0; 00056 //reset(); 00057 } 00058 00059 void calibrateCountPeriod(uint32_t inDelay = 50/*ms*/, uint32_t inTimes = 10); 00060 static double getCountPeriod(void) 00061 { 00062 return mPeriod; 00063 } 00064 static void setCountPeriod(double period) 00065 { 00066 mPeriod = period; 00067 } 00068 00069 uint64_t getCount(void) const; 00070 double getCount(double unit) const 00071 { 00072 return (double) getCount() * mPeriod * unit; 00073 } 00074 double getValue(double unit) const 00075 { 00076 return (double) (getCount() - mValueCount) * mPeriod * unit; 00077 } 00078 double getDelay(double unit) 00079 { 00080 uint64_t count = getCount(); 00081 double delay = (double) (count - mDelayCount) * mPeriod * unit; 00082 mDelayCount = count; 00083 return delay; 00084 } 00085 void reset(void) 00086 { 00087 mValueCount = getCount(); 00088 mDelayCount = 0; 00089 } 00090 double getTimeOfDay(double unit) 00091 { 00092 timeval lCurrent; 00093 ::gettimeofday(&lCurrent, 0); 00094 return ((static_cast<double>(lCurrent.tv_sec) * YS_UNIT + static_cast<double>(lCurrent.tv_usec)) * (unit / YS_UNIT)); 00095 } 00096 00097 protected: 00098 bool mHardware; 00099 uint64_t mValueCount; //duration from start 00100 uint64_t mDelayCount; //duration from previous getCount 00101 static double mPeriod; 00102 }; 00103 00104 } //namespace Diagnostic 00105 00106 #endif // TIME_STAMP_COUNTER_H_