Timer.cpp
Go to the documentation of this file.
1 //File: Timer.cpp
2 //Brief: A CRT::Timer approximates the time represented by a CRT timestamp and the elapsed time since an initial time it was given.
3 //Author: Andrew Olivier aolivier@ur.rochester.edu
4 
5 //Include header
6 #include "Timer.h"
7 
8 //Include definition of CRT::Trigger for interface
10 
11 namespace CRT
12 {
14  {
15  }
16 
17  //TODO: Fill in constructor
18  Timer::Timer(const uint64_t timestamp): fLastUNIXSecond(Timestamp(timestamp).upper), fNsOfLastUNIXSecond(Timestamp(timestamp).lower),
19  fNsOfLastSync(0), fPrevNs(Timestamp(timestamp).lower), fFirstTime(GetSeconds(timestamp))
20  {
21  }
22 
24  {
25  return seconds(trigger.Timestamp());
26  }
27 
28  double Timer::seconds(const uint64_t timestamp)
29  {
30  return GetSeconds(timestamp);
31  }
32 
34  {
35  return elapsed(trigger.Timestamp());
36  }
37 
38  double Timer::elapsed(const uint64_t timestamp)
39  {
40  return GetSeconds(timestamp) - fFirstTime;
41  }
42 
43  double Timer::GetSeconds(const uint64_t timestamp)
44  {
45  //Extract upper and lower parts of timestamp
46  Timestamp time(timestamp);
47 
48  //If a sync pulse arrived between this timestamp and the previous one seen. This could skip Sync pulses entirely.
49  if(time.lower < fPrevNs)
50  {
51  fNsOfLastSync = fPrevNs; //Estimate time in ns of previous Sync as previous timestamp seen
52  fPrevNs = time.lower;
53  return time.upper+(time.lower+fNsOfLastSync)*1e-9;
54  }
55 
56  //If the UNIX timestamp has moved to a new value
57  if(time.upper > fLastUNIXSecond)
58  {
59  fLastUNIXSecond = time.upper;
61  }
62 
63  return time.upper+(time.lower-fNsOfLastUNIXSecond)*1e-9;
64  }
65 
66  double Timer::elapsed()
67  {
68  const uint64_t lastTimestamp = ((uint64_t)fLastUNIXSecond << 32) | fPrevNs;
69  return elapsed(lastTimestamp);
70  }
71 }
uint32_t fPrevNs
Definition: Timer.h:60
const double fFirstTime
Definition: Timer.h:61
Timer(const CRT::Trigger &trigger)
Definition: Timer.cpp:13
uint32_t lower
Definition: Timer.h:31
double GetSeconds(const uint64_t timestamp)
Definition: Timer.cpp:43
double seconds(const CRT::Trigger &trigger)
Definition: Timer.cpp:23
unsigned long long Timestamp() const
Definition: CRTTrigger.h:96
uint32_t fNsOfLastSync
Definition: Timer.h:58
const double e
unsigned __int64 uint64_t
Definition: stdint.h:136
uint32_t fLastUNIXSecond
Definition: Timer.h:56
uint32_t upper
Definition: Timer.h:32
uint32_t fNsOfLastUNIXSecond
Definition: Timer.h:57
double elapsed()
Definition: Timer.cpp:66