Rev 201 | Rev 207 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
203 | f9daq | 1 | #include <formatio.h> |
195 | f9daq | 2 | #include <utility.h> |
3 | #include <ansi_c.h> |
||
203 | f9daq | 4 | #include <cvirte.h> |
195 | f9daq | 5 | #include <userint.h> |
6 | #include "drs4.h" |
||
203 | f9daq | 7 | #include "drsread.h" |
195 | f9daq | 8 | |
203 | f9daq | 9 | #define MIKRO |
10 | |||
11 | #ifdef MIKRO |
||
12 | #include "MIKRO.h" |
||
13 | #endif |
||
14 | |||
195 | f9daq | 15 | static int daq_on; |
203 | f9daq | 16 | static int ph, p2,plothandle[4]= {0,0,0,0}; |
195 | f9daq | 17 | static int tfID; |
18 | static int controlID; |
||
19 | |||
203 | f9daq | 20 | #define MAX_THREADS 10 |
195 | f9daq | 21 | |
203 | f9daq | 22 | static CmtThreadPoolHandle poolHandle = 0; |
195 | f9daq | 23 | |
24 | int main (int argc, char *argv[]) { |
||
203 | f9daq | 25 | short port; |
26 | if (InitCVIRTE (0, argv, 0) == 0) |
||
27 | return -1; /* out of memory */ |
||
28 | if ((ph = LoadPanel (0, "drs4.uir", PANEL)) < 0) |
||
29 | return -1; |
||
30 | if ((p2 = LoadPanel (0, "drs4.uir", SCAN)) < 0) |
||
31 | return -1; |
||
32 | SetStdioPort (CVI_STDIO_WINDOW); |
||
33 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
34 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
35 | |||
36 | DisplayPanel (ph); |
||
37 | DisplayPanel (p2); |
||
38 | |||
39 | #ifdef MIKRO |
||
40 | GetCtrlVal(p2, SCAN_PORT, &port); |
||
41 | if (MIKRO_Open (port)) MessagePopup ("Error", "Mikro Port Not found !\n Change in the GUI") ; |
||
42 | MIKRO_Init(1,0); |
||
43 | MIKRO_Init(2,0); |
||
44 | #endif |
||
45 | |||
46 | RunUserInterface (); |
||
47 | DiscardPanel (ph); |
||
48 | DiscardPanel (p2); |
||
49 | CmtDiscardThreadPool (poolHandle); |
||
50 | MIKRO_Close (); |
||
51 | return 0; |
||
195 | f9daq | 52 | } |
53 | |||
54 | |||
55 | |||
56 | |||
57 | |||
58 | static void start_timer (double tout) { |
||
203 | f9daq | 59 | SetCtrlAttribute (ph, PANEL_TIMER, ATTR_INTERVAL, tout); |
60 | SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 1); |
||
195 | f9daq | 61 | } |
62 | |||
63 | static void stop_timer ( void ) { |
||
203 | f9daq | 64 | SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 0); |
65 | DRSSetTimeout(); |
||
195 | f9daq | 66 | } |
67 | |||
68 | |||
69 | |||
70 | |||
71 | void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle, |
||
203 | f9daq | 72 | CmtThreadFunctionID functionID, unsigned int event, |
73 | int value, void *callbackData ) { |
||
195 | f9daq | 74 | |
203 | f9daq | 75 | daq_on=0; |
76 | //SetDimming(0); |
||
77 | printf("End of Thread \n"); |
||
78 | return ; |
||
195 | f9daq | 79 | |
80 | } |
||
81 | |||
82 | |||
203 | f9daq | 83 | int CVICALLBACK daq(void *functionData) { |
195 | f9daq | 84 | |
85 | |||
203 | f9daq | 86 | int neve; |
87 | char filename[0xff]; |
||
88 | int imask[4]; |
||
89 | unsigned long mask; |
||
90 | int frequency; |
||
91 | double trgdelay; |
||
92 | double trglevel; |
||
93 | int trgtype; |
||
94 | int trgchannel; |
||
95 | int trgpolarity; |
||
96 | int verbose; |
||
97 | double range; |
||
98 | int pfreq; |
||
99 | int enabledoutput; |
||
100 | int neveold = 0; |
||
101 | double rate; |
||
102 | int *args = (int *) functionData; |
||
201 | f9daq | 103 | |
203 | f9daq | 104 | GetCtrlVal(ph, PANEL_FILENAME, filename ); |
105 | GetCtrlVal(ph, PANEL_CH0, &imask[0] ); |
||
106 | GetCtrlVal(ph, PANEL_CH1, &imask[1] ); |
||
107 | GetCtrlVal(ph, PANEL_CH2, &imask[2] ); |
||
108 | GetCtrlVal(ph, PANEL_CH3, &imask[3] ); |
||
109 | mask = 0; |
||
110 | for (int i=0; i<4; i++) { |
||
111 | if (imask[i]) mask |= (1<<i); |
||
201 | f9daq | 112 | } |
203 | f9daq | 113 | |
114 | GetCtrlVal(ph,PANEL_NEVE, &neve); |
||
115 | GetCtrlVal(ph,PANEL_DEBUG, &verbose); |
||
116 | GetCtrlVal(ph,PANEL_PFREQ, &pfreq); |
||
117 | GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput); |
||
118 | |||
119 | GetCtrlVal(ph,PANEL_FREQUENCY, &frequency); |
||
120 | GetCtrlVal(ph,PANEL_TRGDELAY, &trgdelay); |
||
121 | GetCtrlVal(ph,PANEL_TRGCHANNEL, &trgchannel); |
||
122 | |||
123 | GetCtrlVal(ph,PANEL_TRGTYPE, &trgtype); |
||
124 | GetCtrlVal(ph,PANEL_TRGLEVEL, &trglevel); |
||
125 | GetCtrlVal(ph,PANEL_TRGPOLARITY, &trgpolarity); |
||
126 | GetCtrlVal(ph,PANEL_RANGE, &range); |
||
127 | |||
128 | |||
129 | //printf("mask=0x%x\n",mask); |
||
130 | |||
131 | DRSSetMask( (unsigned char)( mask & 0xF ) ); |
||
132 | |||
133 | DRSSetFrequency( frequency ); |
||
134 | DRSSetTriggerDelay(trgdelay ); |
||
135 | DRSSetTriggerChannel(trgchannel ); |
||
136 | DRSSetTriggerType( trgtype ); |
||
137 | DRSSetTriggerLevel(trglevel); |
||
138 | DRSSetTriggerPolarity(trgpolarity); |
||
139 | |||
140 | DRSSetRange ( range ); |
||
141 | |||
142 | |||
143 | |||
144 | FILE *fp= NULL; |
||
145 | |||
146 | if (enabledoutput) { |
||
147 | if (args[0]) fp = fopen(filename,"wb"); |
||
148 | else fp = fopen(filename,"ab"); |
||
149 | } |
||
150 | |||
151 | static unsigned char *buffer; |
||
152 | |||
153 | int buffer_size = 0; |
||
154 | const int nBoards=1; |
||
155 | const int waveDepth=1024; |
||
156 | if (buffer_size == 0) { |
||
157 | buffer_size = 4 + nBoards * (4 + 4*(4+waveDepth*4)); |
||
158 | buffer_size += 24 + nBoards * (8 + 4*(4+waveDepth*2)); |
||
159 | buffer = (unsigned char *)malloc(buffer_size); |
||
160 | } |
||
161 | |||
162 | time_t t=0,told=0, tstart=0; |
||
163 | |||
164 | if (!DRSInit()) { |
||
165 | time(&tstart); |
||
166 | told=tstart; |
||
167 | int i=0; |
||
168 | for (i=0; i<neve; i++) { |
||
169 | start_timer(1);// 1 s timeout |
||
170 | int retval = DRSRead(0); |
||
171 | stop_timer(); |
||
172 | int nb = ( retval == 0 && fp ) ? DRSToBuffer( buffer , i ) : 0; |
||
173 | SetCtrlVal(ph,PANEL_CEVE,i); |
||
174 | if (retval) i--; |
||
175 | if (!daq_on) break; |
||
176 | time(&t); |
||
177 | if (t!=told ) { |
||
178 | rate = (i-neveold); |
||
179 | printf("%d events in %2.2f min (%d s) Rate %f Hz %s ",i+1, (double)(t-tstart)/60.,(t-tstart), rate , ctime(&t)); |
||
180 | GetCtrlVal(ph,PANEL_PFREQ, &pfreq); |
||
181 | neveold = i; |
||
182 | } |
||
183 | told=t; |
||
184 | // Save data |
||
185 | if (nb>0 && fp) fwrite(buffer, 1,nb ,fp); |
||
186 | // Plot Data |
||
187 | if (i % pfreq == 0) for (int k=0; k<4; k++) { |
||
188 | if (! (mask & ( 0x1<<k )) ) continue; |
||
189 | float *t=DRSGetTime(k); |
||
190 | float *x=DRSGetWave(k); |
||
191 | |||
192 | const int col[4]= {VAL_WHITE,VAL_RED,VAL_GREEN,VAL_BLUE}; |
||
193 | if (plothandle[k]) DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_IMMEDIATE_DRAW); |
||
194 | plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]); |
||
195 | |||
196 | for (int i=0 ; i<1024 ; i++) { |
||
197 | if (verbose) printf("[%d] %d. x= %3.2f y=%3.2f\n", k, i, t[i], x[i] ); |
||
198 | //h[k]->Fill( t[i], x[i]*1e-3); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | |||
203 | } |
||
204 | time(&t); |
||
205 | printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t)); |
||
206 | DRSEnd(); |
||
207 | } |
||
208 | |||
209 | if (fp) fclose(fp); |
||
210 | |||
211 | free(buffer); |
||
212 | |||
213 | return 0; |
||
214 | |||
195 | f9daq | 215 | } |
216 | |||
217 | |||
218 | |||
203 | f9daq | 219 | int CVICALLBACK scan(void *functionData) { |
195 | f9daq | 220 | |
203 | f9daq | 221 | int dx[2]; |
222 | int nx[2]; |
||
223 | int x0[2]; |
||
224 | int ix[2]; |
||
225 | int size; |
||
226 | |||
227 | char buffer[12]="POSR"; |
||
228 | int *n = (int *) &buffer[4]; |
||
229 | char filename[0xFF]; |
||
230 | int enabledoutput; |
||
231 | |||
232 | FILE *fp; |
||
233 | GetCtrlVal(ph, PANEL_FILENAME, filename ); |
||
234 | |||
235 | GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput); |
||
236 | |||
237 | if ( GetFileInfo(filename,&size) ) { |
||
238 | MessagePopup ("Warning", "File exist. Remove it first or choose another file"); |
||
239 | return 0; |
||
240 | } |
||
241 | GetCtrlVal(p2, SCAN_STEPX, &dx[0]); |
||
242 | GetCtrlVal(p2, SCAN_STEPY, &dx[1]); |
||
243 | GetCtrlVal(p2, SCAN_NSTEPSX, &nx[0]); |
||
244 | GetCtrlVal(p2, SCAN_NSTEPSY, &nx[1]); |
||
245 | GetCtrlVal(p2, SCAN_STARTX, &x0[0]); |
||
246 | GetCtrlVal(p2, SCAN_STARTY, &x0[1]); |
||
247 | for (int i=0; i<nx[0]; i++) { |
||
248 | |||
249 | ix[0]= x0[0]+i*dx[0]; |
||
250 | #ifdef MIKRO |
||
251 | MIKRO_MoveTo(1,ix[0]); |
||
252 | #endif |
||
253 | SetCtrlVal (p2, SCAN_IX, i); |
||
254 | for (int j=0; j<nx[1]; j++) { |
||
255 | |||
256 | SetCtrlVal (p2, SCAN_IY, j); |
||
257 | |||
258 | ix[1]= x0[1]+j*dx[1]; |
||
259 | #ifdef MIKRO |
||
260 | MIKRO_MoveTo(2,ix[1]); |
||
261 | |||
262 | MIKRO_GetPosition(1,&n[0]); |
||
263 | SetCtrlVal (p2, SCAN_XP, n[0]); |
||
264 | MIKRO_GetPosition(2,&n[1]); |
||
265 | SetCtrlVal (p2, SCAN_YP, n[1]); |
||
266 | if (enabledoutput) { |
||
267 | fp = fopen(filename,"ab"); |
||
268 | if (fp) { |
||
269 | fwrite(buffer, 1,12 ,fp); |
||
270 | fclose(fp); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | #endif |
||
275 | daq(functionData); |
||
276 | if (!daq_on) break; |
||
277 | } |
||
278 | if (!daq_on) break; |
||
279 | } |
||
280 | |||
281 | return 0; |
||
195 | f9daq | 282 | } |
283 | |||
203 | f9daq | 284 | |
195 | f9daq | 285 | int CVICALLBACK StartCB (int panel, int control, int event, |
203 | f9daq | 286 | void *callbackData, int eventData1, int eventData2) { |
287 | ThreadFunctionPtr mythread = NULL; |
||
288 | switch (event) { |
||
289 | |||
290 | case EVENT_COMMIT: |
||
291 | |||
292 | controlID=0; |
||
293 | if (panel == ph && control == PANEL_START) { |
||
294 | mythread = daq; |
||
295 | controlID= control; |
||
296 | } |
||
297 | if (panel == p2 && control == SCAN_SCAN) mythread = scan; |
||
298 | if (mythread!=NULL) { |
||
299 | printf("New Thread panel=%d button=%d\n", panel, control); |
||
300 | |||
301 | // SetDimming(1); |
||
302 | |||
303 | daq_on=1; |
||
304 | CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID, |
||
305 | DEFAULT_THREAD_PRIORITY, |
||
306 | EndOfThread, |
||
307 | EVENT_TP_THREAD_FUNCTION_END, |
||
308 | NULL, RUN_IN_SCHEDULED_THREAD, |
||
309 | &tfID); |
||
310 | } |
||
311 | break; |
||
312 | } |
||
313 | return 0; |
||
195 | f9daq | 314 | } |
315 | |||
316 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
203 | f9daq | 317 | void *callbackData, int eventData1, int eventData2) { |
318 | switch (event) { |
||
319 | case EVENT_COMMIT: |
||
320 | daq_on=0; |
||
321 | break; |
||
322 | } |
||
323 | return 0; |
||
195 | f9daq | 324 | } |
325 | |||
326 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
203 | f9daq | 327 | void *callbackData, int eventData1, int eventData2) { |
328 | switch (event) { |
||
329 | case EVENT_COMMIT: |
||
330 | QuitUserInterface (0); |
||
331 | break; |
||
332 | } |
||
333 | return 0; |
||
195 | f9daq | 334 | } |
203 | f9daq | 335 | |
336 | int CVICALLBACK MoveStageCB (int panel, int control, int event, |
||
337 | void *callbackData, int eventData1, int eventData2) { |
||
338 | int axis=0, step=1000, direction=1, n; |
||
339 | switch (event) { |
||
340 | case EVENT_COMMIT: |
||
341 | |||
342 | if (panel == p2) { |
||
343 | switch (control) { |
||
344 | case SCAN_BR : |
||
345 | axis = 1; |
||
346 | direction = 1; |
||
347 | GetCtrlVal(p2, SCAN_STEPX, &step); |
||
348 | break; |
||
349 | case SCAN_BL : |
||
350 | axis = 1; |
||
351 | direction = -1; |
||
352 | GetCtrlVal(p2, SCAN_STEPX, &step); |
||
353 | break; |
||
354 | case SCAN_BU : |
||
355 | axis = 2; |
||
356 | direction = 1; |
||
357 | GetCtrlVal(p2, SCAN_STEPY, &step); |
||
358 | break; |
||
359 | case SCAN_BD : |
||
360 | axis = 2; |
||
361 | direction = -1; |
||
362 | GetCtrlVal(p2, SCAN_STEPY, &step); |
||
363 | break; |
||
364 | } |
||
365 | #ifdef MIKRO |
||
366 | MIKRO_MoveFor(axis, direction*step ); |
||
367 | MIKRO_GetPosition(axis,&n); |
||
368 | if (axis == 1) SetCtrlVal (p2, SCAN_XP, n); |
||
369 | if (axis == 2) SetCtrlVal (p2, SCAN_YP, n); |
||
370 | #endif // MIKRO |
||
371 | } |
||
372 | |||
373 | break; |
||
374 | } |
||
375 | return 0; |
||
376 | } |
||
377 | |||
378 | |||
379 | |||
380 | |||
381 | |||
382 | int CVICALLBACK GoXCB (int panel, int control, int event, |
||
383 | void *callbackData, int eventData1, int eventData2) { |
||
384 | int n2; |
||
385 | switch (event) { |
||
386 | case EVENT_COMMIT: |
||
387 | GetCtrlVal (p2, SCAN_XG, &n2); |
||
388 | #ifdef MIKRO |
||
389 | MIKRO_MoveTo(1,n2); |
||
390 | MIKRO_GetPosition(1,&n2); |
||
391 | |||
392 | #endif |
||
393 | SetCtrlVal (p2, SCAN_XP, n2); |
||
394 | break; |
||
395 | } |
||
396 | return 0; |
||
397 | } |
||
398 | |||
399 | int CVICALLBACK GoYCB (int panel, int control, int event, |
||
400 | void *callbackData, int eventData1, int eventData2) { |
||
401 | int n2; |
||
402 | switch (event) { |
||
403 | case EVENT_COMMIT: |
||
404 | GetCtrlVal (p2, SCAN_YG, &n2); |
||
405 | #ifdef MIKRO |
||
406 | MIKRO_MoveTo(2,n2); |
||
407 | MIKRO_GetPosition(2,&n2); |
||
408 | |||
409 | #endif |
||
410 | SetCtrlVal (p2, SCAN_YP, n2); |
||
411 | break; |
||
412 | } |
||
413 | return 0; |
||
414 | } |
||
415 | |||
416 | int CVICALLBACK GetCurrentPositionCB (int panel, int control, int event, |
||
417 | void *callbackData, int eventData1, int eventData2) { |
||
418 | |||
419 | int n[2]; |
||
420 | switch (event) { |
||
421 | case EVENT_COMMIT: |
||
422 | #ifdef MIKRO |
||
423 | MIKRO_GetPosition(1,&n[0]); |
||
424 | SetCtrlVal (p2, SCAN_XP, n[0]); |
||
425 | MIKRO_GetPosition(2,&n[1]); |
||
426 | SetCtrlVal (p2, SCAN_YP, n[1]); |
||
427 | #endif |
||
428 | break; |
||
429 | } |
||
430 | return 0; |
||
431 | } |
||
432 | |||
433 | int CVICALLBACK HomeCB (int panel, int control, int event, |
||
434 | void *callbackData, int eventData1, int eventData2) { |
||
435 | switch (event) { |
||
436 | case EVENT_COMMIT: |
||
437 | #ifdef MIKRO |
||
438 | MIKRO_ReferenceMove(1); |
||
439 | MIKRO_ReferenceMove(2); |
||
440 | GetCurrentPositionCB(panel, control, event, NULL, 0, 0); |
||
441 | #endif |
||
442 | break; |
||
443 | } |
||
444 | return 0; |
||
445 | } |