-
Notifications
You must be signed in to change notification settings - Fork 4
/
timereval.cpp
223 lines (179 loc) · 7.14 KB
/
timereval.cpp
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
/* TIMEREVAL.C mex-file to schedule delayed evaluation of matlab
commands in MATLAB 'base' workspace
*/
#include <mex.h>
#ifdef WIN32
#pragma once
#include <windows.h>
#endif
static bool RUNNING = false;
static int NUMCALLS = 0;
struct TimerEvent
{ char* MatlabCallbackString;
UINT TimerID;
struct TimerEvent *Next;
struct TimerEvent *Prev;
};
static struct TimerEvent *EventListHead = NULL;
// Search linked list for the structure whos TimerID field matches the input TimerID
struct TimerEvent *FindEventByTimerID( UINT TimerID, struct TimerEvent *Head)
{ struct TimerEvent *ThisOne = Head;
while(ThisOne)
{ if(ThisOne->TimerID == TimerID)
return(ThisOne);
else
ThisOne = ThisOne->Next;
}
return(ThisOne);
}
int AddToList(UINT TimerID, char* cbs)
{ struct TimerEvent *NewOne;
NewOne = (struct TimerEvent *)mxMalloc(sizeof(struct TimerEvent));
if(!NewOne)
return(0);
mexMakeMemoryPersistent(NewOne);
NewOne->MatlabCallbackString = cbs;
NewOne->TimerID = TimerID;
NewOne->Prev = NULL;
NewOne->Next = EventListHead;
if(EventListHead)
EventListHead->Prev = NewOne;
EventListHead = NewOne;
return(1);
}
void RemoveFromList(struct TimerEvent * EventToRemove)
{ if(!EventToRemove->Prev) // EventToRemove is the Head
{ if(!EventToRemove->Next) // EventToRemove is also the last element
{ EventListHead = NULL;
}
else // There are elements after EventToRemove
{ EventListHead = EventToRemove->Next;
(EventToRemove->Next)->Prev = NULL;
}
}
else // EventToRemove is NOT the Head
{ if(!EventToRemove->Next) // EventToRemove is the last element
{ (EventToRemove->Prev)->Next = NULL;
}
else // There are elements before and after EventToRemove - relink
{ (EventToRemove->Next)->Prev = EventToRemove->Prev;
(EventToRemove->Prev)->Next = EventToRemove->Next;
}
}
mxFree(EventToRemove->MatlabCallbackString);
mxFree(EventToRemove);
}
static void ClearEventList()
{ while(EventListHead)
{ KillTimer(NULL,EventListHead->TimerID);
RemoveFromList(EventListHead);
}
}
void PrintEventList()
{ struct TimerEvent *ThisOne = EventListHead;
while(ThisOne)
{ mexPrintf("%u %s\n", ThisOne->TimerID,ThisOne->MatlabCallbackString);
ThisOne = ThisOne->Next;
}
}
void __stdcall CallbackFncnSingle( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{ // Scheduled to execute by the call to SetTimer
// It runs only once: after completion it destroys the timer it is
// associated with and removes itself from the linked list of scheduled events
struct TimerEvent *PEvent;
// Search global linked list of events for the matching TimerID
PEvent = FindEventByTimerID( idEvent, EventListHead);
// Execute commands stored in MatlabCallbackString field in MATLAB 'base' workspace
mexEvalString(PEvent->MatlabCallbackString);
// After completion - KillTimer and remove the Event from the list
KillTimer(NULL,idEvent);
RemoveFromList(PEvent);
}
void __stdcall CallbackFncnMultiple( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{ // Scheduled to execute by the call to SetTimer
// It runs infinite number of times.
struct TimerEvent *PEvent;
// Search global linked list of events for the matching TimerID
PEvent = FindEventByTimerID( idEvent, EventListHead);
// Execute commands stored in MatlabCallbackString field in MATLAB 'base' workspace
mexEvalString(PEvent->MatlabCallbackString);
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{ UINT thandle,buflen,tofind;
int ms_delay,commandswitch,option;
char* cbs;
struct TimerEvent* PEvent;
mexAtExit(ClearEventList);
commandswitch = (int)mxGetScalar(prhs[0]);
switch(commandswitch)
{ case 1: //Add single call timer
ms_delay = (int)mxGetScalar(prhs[1]);
buflen = (mxGetM(prhs[2]) * mxGetN(prhs[2]) * sizeof(mxChar)) + 1;
cbs = (char*)mxMalloc(buflen);
mxGetString(prhs[2], cbs ,buflen);
mexMakeMemoryPersistent(cbs);
thandle=SetTimer(NULL,NULL,ms_delay,(TIMERPROC) &CallbackFncnSingle);
AddToList(thandle,cbs);
plhs[0]=mxCreateDoubleScalar(thandle);
break;
case 2: //Add multiple call timer
ms_delay = (int)mxGetScalar(prhs[1]);
buflen = (mxGetM(prhs[2]) * mxGetN(prhs[2]) * sizeof(mxChar)) + 1;
cbs = (char*)mxMalloc(buflen);
mxGetString(prhs[2], cbs ,buflen);
mexMakeMemoryPersistent(cbs);
thandle=SetTimer(NULL,NULL,ms_delay,(TIMERPROC) &CallbackFncnMultiple);
AddToList(thandle,cbs);
plhs[0]=mxCreateDoubleScalar(thandle);
break;
case 3:
// Kill all existing timers before starting a new one
// timereval(3,type,delay,callback);
ClearEventList();
option = (int)mxGetScalar(prhs[1]);
ms_delay = (int)mxGetScalar(prhs[2]);
buflen = (mxGetM(prhs[3]) * mxGetN(prhs[3]) * sizeof(mxChar)) + 1;
cbs = (char*)mxMalloc(buflen);
mxGetString(prhs[3], cbs ,buflen);
mexMakeMemoryPersistent(cbs);
if(option==1)
thandle=SetTimer(NULL,NULL,ms_delay,(TIMERPROC) &CallbackFncnSingle);
else if(option==2)
thandle=SetTimer(NULL,NULL,ms_delay,(TIMERPROC) &CallbackFncnMultiple);
AddToList(thandle,cbs);
plhs[0]=mxCreateDoubleScalar(thandle);
break;
case 4:
PrintEventList();
break;
case 5:
tofind = (int)mxGetScalar(prhs[1]);
PEvent = FindEventByTimerID( tofind, EventListHead);
if(PEvent)
{ KillTimer(NULL,PEvent->TimerID);
RemoveFromList(PEvent);
}
break;
case 6:
// timereval(6,OldTimerID,type,delay,callback);
tofind = (int)mxGetScalar(prhs[1]);
PEvent = FindEventByTimerID( tofind, EventListHead);
if(PEvent)
{ KillTimer(NULL,PEvent->TimerID);
RemoveFromList(PEvent);
}
option = (int)mxGetScalar(prhs[2]);
ms_delay = (int)mxGetScalar(prhs[3]);
buflen = (mxGetM(prhs[4]) * mxGetN(prhs[4]) * sizeof(mxChar)) + 1;
cbs = (char*)mxMalloc(buflen);
mxGetString(prhs[4], cbs ,buflen);
mexMakeMemoryPersistent(cbs);
if(option==1)
thandle=SetTimer(NULL,NULL,ms_delay,(TIMERPROC) &CallbackFncnSingle);
else if(option==2)
thandle=SetTimer(NULL,NULL,ms_delay,(TIMERPROC) &CallbackFncnMultiple);
AddToList(thandle,cbs);
plhs[0]=mxCreateDoubleScalar(thandle);
break;
}
}