GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Timer.h
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #ifndef GRT_TIMER_HEADER
22 #define GRT_TIMER_HEADER
23 
24 #include "../Util/GRTVersionInfo.h"
25 
26 //Include the platform specific time headers
27 #if defined(__GRT_WINDOWS_BUILD__)
28  #include <windows.h>
29  #include <mmsystem.h>
30 #endif
31 
32 #if defined(__GRT_OSX_BUILD__)
33  #include <sys/time.h>
34 #endif
35 
36 #if defined(__GRT_LINUX_BUILD__)
37  #include <sys/time.h>
38 #endif
39 
40 namespace GRT{
41 
42 class Timer{
43 public:
47  Timer(){}
48 
52  ~Timer(){}
53 
59  bool start(){
60  startTime = getSystemTime();
61  timerRunning = true;
62  timerMode = NORMAL_MODE;
63  return true;
64  }
65 
73  bool start(unsigned long countDownTime){
74  if( countDownTime > 0 ){
75  startTime = getSystemTime();
76  timerRunning = true;
77  timerMode = COUNTDOWN_MODE;
78  this->countDownTime = countDownTime;
79  return true;
80  }
81  return false;
82  }
83 
89  bool stop(){
90  timerRunning = false;
91  return true;
92  }
93 
100  signed long getMilliSeconds() const {
101  if( !timerRunning ) return 0;
102 
103  unsigned long now = getSystemTime();
104 
105  switch( timerMode ){
106  case NORMAL_MODE:
107  return (now-startTime);
108  break;
109  case COUNTDOWN_MODE:
110  return (countDownTime - (now-startTime));
111  break;
112  default:
113  return 0;
114  break;
115  }
116 
117  return 0;
118  }
119 
128  double getSeconds() const {
129  if( !timerRunning ) return 0;
130  return getMilliSeconds()/1000.0;
131  }
132 
138  bool running() const { return timerRunning; }
139 
145  bool getTimerReached() const {
146  if( !timerRunning ){
147  return false;
148  }
149  if( getMilliSeconds() > 0 ) return false;
150  return true;
151  }
152 
156  bool timerReached() const {
157  return getTimerReached();
158  }
159 
165  static unsigned long getSystemTime( ){
166 #ifdef __GRT_OSX_BUILD__
167  struct timeval now;
168  gettimeofday( &now, NULL );
169  return now.tv_usec/1000 + now.tv_sec*1000;
170 #endif
171 #ifdef __GRT_WINDOWS_BUILD__
172  SYSTEMTIME systemTime;
173  GetSystemTime(&systemTime);
174  return (systemTime.wHour*60*60*1000) + (systemTime.wMinute*60*1000) + (systemTime.wSecond*1000) + systemTime.wMilliseconds;
175 #endif
176 #ifdef __GRT_LINUX_BUILD__
177  struct timeval now;
178  gettimeofday( &now, NULL );
179  return now.tv_usec/1000 + now.tv_sec*1000;
180 #endif
181  return 0;
182  }
183 
184 protected:
185  unsigned long startTime;
186  unsigned long countDownTime;
187  unsigned int timerMode;
188  bool timerRunning;
189 
190  enum TimerModes{NORMAL_MODE=0,COUNTDOWN_MODE};
191 
192 };
193 
194 }; //End of namespace GRT
195 
196 
197 #endif //GRT_TIMER_HEADER
static unsigned long getSystemTime()
Definition: Timer.h:165
Definition: AdaBoost.cpp:25
double getSeconds() const
Definition: Timer.h:128
bool start(unsigned long countDownTime)
Definition: Timer.h:73
bool getTimerReached() const
Definition: Timer.h:145
bool start()
Definition: Timer.h:59
signed long getMilliSeconds() const
Definition: Timer.h:100
Timer()
Definition: Timer.h:47
bool running() const
Definition: Timer.h:138
bool timerReached() const
Definition: Timer.h:156
bool stop()
Definition: Timer.h:89
~Timer()
Definition: Timer.h:52