GestureRecognitionToolkit  Version: 0.1.0
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 "GRTVersionInfo.h"
25 #include "GRTTypedefs.h"
26 
27 //Include the platform specific time headers
28 #if defined(__GRT_WINDOWS_BUILD__)
29  #include <windows.h>
30  #include <mmsystem.h>
31 #endif
32 
33 #if defined(__GRT_OSX_BUILD__)
34  #include <sys/time.h>
35 #endif
36 
37 #if defined(__GRT_LINUX_BUILD__)
38  #include <sys/time.h>
39 #endif
40 
41 GRT_BEGIN_NAMESPACE
42 
43 class Timer{
44 public:
48  Timer(){
49  timerMode = NORMAL_MODE;
50  timerState = NOT_RUNNING;
51  timerRunning = false;
52  }
53 
57  ~Timer(){}
58 
64  bool start(){
65  startTime = getSystemTime();
66  timerRunning = true;
67  timerMode = NORMAL_MODE;
68  timerState = RUNNING;
69  return true;
70  }
71 
81  bool start(unsigned long countDownTime,unsigned long prepTime = 0){
82  if( countDownTime > 0 ){
83  startTime = getSystemTime();
84  timerRunning = true;
85  this->countDownTime = countDownTime;
86  this->prepTime = prepTime;
87  timerMode = COUNTDOWN_MODE;
88 
89  if( prepTime > 0 ){
90  timerState = PREP_STATE;
91  }else{
92  timerState = COUNTDOWN_STATE;
93  }
94 
95  return true;
96  }
97  return false;
98  }
99 
105  bool stop(){
106  timerRunning = false;
107  timerState = NOT_RUNNING;
108  return true;
109  }
110 
117  signed long getMilliSeconds() {
118  if( !timerRunning ) return 0;
119 
120  unsigned long now = getSystemTime();
121 
122  switch( timerMode ){
123  case NORMAL_MODE:
124  return (now-startTime);
125  break;
126  case COUNTDOWN_MODE:
127  if( timerState == PREP_STATE ){
128  if( now-startTime >= prepTime ){
129  timerState = COUNTDOWN_STATE;
130  startTime = now;
131  return countDownTime;
132  }
133  return countDownTime;
134  }
135  if( timerState == COUNTDOWN_STATE ) return (countDownTime - (now-startTime));
136  break;
137  default:
138  return 0;
139  break;
140  }
141 
142  return 0;
143  }
144 
153  Float getSeconds() {
154  if( !timerRunning ) return 0;
155  return getMilliSeconds()/1000.0;
156  }
157 
163  bool running() { return getTimerRunning(); }
164 
171  if( !timerRunning ){
172  return false;
173  }
174  if( getMilliSeconds() > 0 ) return false;
175  return true;
176  }
177 
184  if( !timerRunning ){
185  return false;
186  }
187  if( getMilliSeconds() > 0 ) return false;
188  return true;
189  }
190 
191  bool getInPrepState() const {
192  return timerState == PREP_STATE;
193  }
194 
195  bool getInCountdownState() const {
196  return timerState == COUNTDOWN_STATE;
197  }
198 
202  bool timerReached() {
203  return getTimerReached();
204  }
205 
211  static unsigned long getSystemTime( ){
212 #ifdef __GRT_OSX_BUILD__
213  struct timeval now;
214  gettimeofday( &now, NULL );
215  return now.tv_usec/1000 + now.tv_sec*1000;
216 #endif
217 #ifdef __GRT_WINDOWS_BUILD__
218  SYSTEMTIME systemTime;
219  GetSystemTime(&systemTime);
220  return (systemTime.wHour*60*60*1000) + (systemTime.wMinute*60*1000) + (systemTime.wSecond*1000) + systemTime.wMilliseconds;
221 #endif
222 #ifdef __GRT_LINUX_BUILD__
223  struct timeval now;
224  gettimeofday( &now, NULL );
225  return now.tv_usec/1000 + now.tv_sec*1000;
226 #endif
227  return 0;
228  }
229 
230 protected:
231  enum TimerMode {NORMAL_MODE=0,COUNTDOWN_MODE};
232  enum TimerState {NOT_RUNNING=0,RUNNING,COUNTDOWN_STATE,PREP_STATE};
233 
234  unsigned long startTime;
235  unsigned long countDownTime;
236  unsigned long prepTime;
237  bool timerRunning;
238  TimerMode timerMode;
239  TimerState timerState;
240 
241 };
242 
243 GRT_END_NAMESPACE
244 
245 #endif //GRT_TIMER_HEADER
bool timerReached()
Definition: Timer.h:202
Timer()
Definition: Timer.h:48
Definition: Timer.h:43
bool getTimerRunning()
Definition: Timer.h:170
Float getSeconds()
Definition: Timer.h:153
signed long getMilliSeconds()
Definition: Timer.h:117
bool getTimerReached()
Definition: Timer.h:183
bool start()
Definition: Timer.h:64
bool stop()
Definition: Timer.h:105
bool running()
Definition: Timer.h:163
static unsigned long getSystemTime()
Definition: Timer.h:211
~Timer()
Definition: Timer.h:57
bool start(unsigned long countDownTime, unsigned long prepTime=0)
Definition: Timer.h:81