-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinapi_timer.h
More file actions
256 lines (201 loc) · 9.37 KB
/
winapi_timer.h
File metadata and controls
256 lines (201 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**********************************************************************************
** ENVIROMENT VARIABILE
**********************************************************************************/
#ifndef WINAPI_TIMER_H_
#define WINAPI_TIMER_H_
//Enable the code that rounds the period to the nearest time step
//#define WINAPI_TIMER_ROUND
/**********************************************************************************
** GLOBAL INCLUDES
**********************************************************************************/
/**********************************************************************************
** DEFINES
**********************************************************************************/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
///--------------------------------------------------------------------------
/// Winapi_timer
///--------------------------------------------------------------------------
//Maximum number of concurrent timers
#define WINAPI_MAX_TIMERS 1
//Minimum timer resoltion. Also the time slice of the scheduler.
//A timer that is not a int multiple of this will have irregular period
#define WINAPI_TIME_STEP 5
///--------------------------------------------------------------------------
/// Winapi_chrono
///--------------------------------------------------------------------------
//maximum concurrent chrono reading
#define WINAPI_MAX_CHRONOS 4
//Error code
#define WINAPI_CHRONOS_ERR -1.0
/**********************************************************************************
** MACROS
**********************************************************************************/
/**********************************************************************************
** TYPEDEF
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: STRUCTURE
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: GLOBAL VARIABILE
**********************************************************************************/
/**********************************************************************************
** PROTOTYPE: CLASS
**********************************************************************************/
/****************************************************************************
** Class
** Winapi_timer
*****************************************************************************
** This class is meant to handle repeatable timers with callback function
****************************************************************************/
class Winapi_timer
{
//Visible to all
public:
///--------------------------------------------------------------------------
/// CONSTRUCTORS
///--------------------------------------------------------------------------
//Empty constructor
Winapi_timer( void );
//Frequency constructor
Winapi_timer( int freq, WAITORTIMERCALLBACK callback );
//Period Constructor
//Winapi_timer( float period, WAITORTIMERCALLBACK callback );
///--------------------------------------------------------------------------
/// DESTRUCTORS
///--------------------------------------------------------------------------
//Destructor
~Winapi_timer();
///--------------------------------------------------------------------------
/// SETTERS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// GETTERS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// TESTERS
///--------------------------------------------------------------------------
//TRUE: the tmer is up and running
//bool is_running( void );
///--------------------------------------------------------------------------
/// PUBLIC METHODS
///--------------------------------------------------------------------------
//Add a timer to the timer queue
//bool add_timer( int period_ms, WAITORTIMERCALLBACK callback_function );
///--------------------------------------------------------------------------
/// PUBLIC VARS
///--------------------------------------------------------------------------
//Visible to derived classes
protected:
///--------------------------------------------------------------------------
/// PROTECTED METHODS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// PROTECTED VARS
///--------------------------------------------------------------------------
//Visible only inside the class
private:
///--------------------------------------------------------------------------
/// PRIVATE METHODS
///--------------------------------------------------------------------------
//Create a timer queue
bool born_timer_queue( void );
//create a timer within a timer queue
bool born_timer( int period_ms, WAITORTIMERCALLBACK callback );
//calculate delay in mS rounded to the nearest resolution
int from_freq( int freq );
//calculate delay in mS from period in S. round to nearest resolution step
//int from_period( float period );
//release all timers
//bool release( void );
//Initialize class vars to thir default value
void init_vars( void );
//Here so that i can easly copy the code.
bool dummy( void );
///--------------------------------------------------------------------------
/// PRIVATE VARS
///--------------------------------------------------------------------------
//number of timers currently active
int g_num_timer;
//Timer queue. It's a special thread inside the kernel
HANDLE g_timer_queue;
HANDLE g_timer[WINAPI_MAX_TIMERS];
}; //End Class: Winapi_timer
/****************************************************************************
** Class
** Winapi_chrono
*****************************************************************************
** PARAMETER:
** RETURN:
** DESCRIPTION:
** This class is a wrapper to the performance counters.
** It provides chronometer functionality.
****************************************************************************/
class Winapi_chrono
{
//Visible to all
public:
///--------------------------------------------------------------------------
/// CONSTRUCTORS
///--------------------------------------------------------------------------
//Empty constructor
Winapi_chrono( void );
///--------------------------------------------------------------------------
/// DESTRUCTORS
///--------------------------------------------------------------------------
//Default constructor
~Winapi_chrono( void );
///--------------------------------------------------------------------------
/// SETTERS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// GETTERS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// TESTERS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// PUBLIC METHODS
///--------------------------------------------------------------------------
//Start time [index]
void start( int index );
//Stop time [index]. Get DeltaT in Seconds. Stop doesn't change start. Multiple stop refers to same start. Return 0.0 if no start
float stop( int index );
//Stop than start
float stop_start( int index );
//snap processor frequency. Automatically done by constructor. Can be done by user.
void get_freq( void );
///--------------------------------------------------------------------------
/// PUBLIC VARS
///--------------------------------------------------------------------------
//Visible to derived classes
protected:
///--------------------------------------------------------------------------
/// PROTECTED METHODS
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
/// PROTECTED VARS
///--------------------------------------------------------------------------
//Visible only inside the class
private:
///--------------------------------------------------------------------------
/// PRIVATE METHODS
///--------------------------------------------------------------------------
//Initialize all vars to safe value.
void init_vars( void );
//Here so that i can easly copy the code.
bool dummy( void );
///--------------------------------------------------------------------------
/// PRIVATE VARS
///--------------------------------------------------------------------------
//Processor frequency
LARGE_INTEGER g_freq;
//Start time. tme delta are referred to this
LARGE_INTEGER g_start[WINAPI_MAX_CHRONOS];
}; //End Class:
#endif