forked from leiferlab/internal.mindcontrolAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc_api_dll.c
302 lines (244 loc) · 7.14 KB
/
mc_api_dll.c
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* mc_api_dll.c
*
* Created on: Jan 27, 2011
* Author: andy
*/
#include <stdio.h>
#include "InterProcess/src/interprocess.h"
#include "mc_api_dll.h"
/*
* Start Server
*
* To Be Used Only by MindControl
*
* The server has some unique properties. It does not wait to acquire a mutex.
* If it tries to access shared memory, and the memory is busy, it will simply return
* that the memory is busy.
*
* The server also has no refractory period. It can read or write in sequence
* as often as it wants. If the server reads repeatedly in a loop it oculd theoretically
* lock out other processes from accessing the shared memory, but thats ok, because its the server.
*
* By default the client has a 7ms refractory period.
* When the client reads from shared memory, the process
* sleeps for 7ms. This ensures that a client cannot lock up the shared memory
* A client will also wait up to 4ms to acquire a mutex lock.
*
*
* Returns handle to shared memory object
* Returns NULL if there is an error
*
*
*/
SharedMemory_handle MC_API_StartServer(){
/* Create shared memory object */
SharedMemory_handle sm = ip_CreateSharedMemoryHost("mcMem");
ip_SetSharedMemoryLockWaitTime(sm, 0);
/** Set Refactory Period to 0 **/
ip_SetSharedMemoryReadRefractoryPeriodTimeDelay(sm,0);
/* Set Laser Controller to 0 */
int val=0;
ip_WriteValue(sm,"int_laserController",(void *) &val, sizeof(int));
return sm;
}
/*
* To Be Used Only by MindControl
*
* Returns MC_API_OK or
* Returns MC_API_ERROR otherwise
*
*/
int MC_API_StopServer(SharedMemory_handle sm){
return ip_CloseSharedMemory(sm);
}
/******** Start/Stop CLIENT ****/
/*
* Returns pointer to Shared Memory handle
* Returns NULL otherwise.
*
* By default the client has a 7ms refractory period.
* When the client reads from shared memory, the process
* sleeps for 7ms. This ensures that a client cannot lock up the shared memory
* A client will also wait up to 4ms to acquire a mutex lock.
*
*/
SharedMemory_handle MC_API_StartClient(){
SharedMemory_handle sm = ip_CreateSharedMemoryClient("mcMem");
return sm;
}
/*
* Returns MC_API_OK or
* Returns MC_API_ERROR otherwise
*
*/
int MC_API_StopClient(SharedMemory_handle sm){
if (ip_CloseSharedMemory(sm)!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*** Laser Power **/
/*
* Check to see if laser controller is present
* Returns MC_API_TRUE 1 if laser controller is present
* Returns MC_API_FALSE 0 if no laser controller is present
* Returns MC_API_ERROR -1 otherwise
*
*/
int MC_API_isLaserControllerPresent(SharedMemory_handle sm){
int val;
int ret=ip_ReadValue(sm,"int_laserController",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
if (val!=MC_API_FALSE && val!=MC_API_TRUE) return MC_API_ERROR;
return val;
}
/*
* Registers the presence of a laser controller software
* that will provide the power level of
* the blue or green lasers.
*
*/
int MC_API_RegisterLaserController(SharedMemory_handle sm){
/* Set Laser Controller to 1 */
int val=1;
int ret=ip_WriteValue(sm,"int_laserController",(void *) &val, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Unregisters Laser Controller
*
*/
int MC_API_UnRegisterLaserController(SharedMemory_handle sm){
/* Set Laser Controller to 1 */
int val=0;
int ret=ip_WriteValue(sm,"int_laserController",(void *) &val, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Set the Laser Power, an integer value between 1 and 100
*
*/
int MC_API_SetSecondLaserPower(SharedMemory_handle sm, int power){
int ret=ip_WriteValue(sm,"int_secondLaserPower",(void *) &power, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Set the Laser Power, an integer value between 1 and 100
*/
int MC_API_SetFirstLaserPower(SharedMemory_handle sm, int power){
int ret=ip_WriteValue(sm,"int_firstLaserPower",(void *) &power, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Get the laser power. An integer value between 1 and 100
* Returns MC_API_ERROR if the value cannot be acquired.
*/
int MC_API_GetFirstLaserPower(SharedMemory_handle sm){
int val=0;
int ret=ip_ReadValue(sm,"int_firstLaserPower",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
return val;
}
/*
* Get the laser power. An integer value between 1 and 100
* Returns MC_API_ERROR if the value cannot be acquired.
*/
int MC_API_GetSecondLaserPower(SharedMemory_handle sm){
int val=0;
int ret=ip_ReadValue(sm,"int_secondLaserPower",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
return val;
}
/*
* Set the laser name. 1 for blue, 2 for green, 3 for red.
*/
int MC_API_SetFirstLaserName(SharedMemory_handle sm, int name){
int ret=ip_WriteValue(sm,"int_firstLaserName",(void *) &name, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Set the laser name. 1 for blue, 2 for green, 3 for red.
*/
int MC_API_SetSecondLaserName(SharedMemory_handle sm, int name){
int ret=ip_WriteValue(sm,"int_secondLaserName",(void *) &name, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Get the laser name. 1 for blue, 2 for green, 3 for red.
* Returns MC_API_ERROR if the value cannot be acquired.
*/
int MC_API_GetFirstLaserName(SharedMemory_handle sm){
int val=0;
int ret=ip_ReadValue(sm,"int_firstLaserName",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
return val;
}
/*
* Get the laser name. 1 for blue, 2 for green, 3 for red.
* Returns MC_API_ERROR if the value cannot be acquired.
*/
int MC_API_GetSecondLaserName(SharedMemory_handle sm){
int val=0;
int ret=ip_ReadValue(sm,"int_secondLaserName",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
return val;
}
/** Current Frame **/
/*
* Set current frame
* Returns MC_API_OK
* Returns MC_API_ERROR if error.
*/
int MC_API_SetCurrentFrame(SharedMemory_handle sm, int frame){
int ret=ip_WriteValue(sm,"int_currFrame",(void *) &frame, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Get current frame
* Returns MC_API_OK
* Returns MC_API_ERROR if error.
*/
int MC_API_GetCurrentFrame(SharedMemory_handle sm){
int val=0;
int ret=ip_ReadValue(sm,"int_currFrame",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
return val;
}
/************* DLP is on / off ****************/
/*
* Set DLP on/off
* Returns MC_API_OK
* Returns MC_API_ERROR if error.
*/
int MC_API_SetDLPOnOff(SharedMemory_handle sm, int isOn){
int ret=ip_WriteValue(sm,"int_DLPisOn",(void *) &isOn, sizeof(int));
if (ret!=MC_API_OK) return MC_API_ERROR;
return MC_API_OK;
}
/*
* Get DLP on/off
* Returns 1 if on
* Returns 0 if off
* Returns MC_API_ERROR if error.
*/
int MC_API_GetDLPOnOff(SharedMemory_handle sm){
int val=0;
int ret=ip_ReadValue(sm,"int_DLPisOn",(void *) &val);
if (ret!=MC_API_OK) return MC_API_ERROR;
if (ret!=MC_API_TRUE && ret!=MC_API_FALSE) return MC_API_ERROR;
return val;
}
/*
* Returns MC_API_OK if server is running
* Returns MC_API_ERROR otherwise
*/
int MC_API_PingServer(SharedMemory_handle sm){
if (ip_GetSharedMemoryStatus(sm) >= 0) return MC_API_OK;
return MC_API_ERROR;
}