Rev 207 | Rev 265 | 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 | |||
210 | f9daq | 55 | char strbuf[0xFF]; |
195 | f9daq | 56 | |
210 | f9daq | 57 | int gLog=0; |
195 | f9daq | 58 | |
210 | f9daq | 59 | int printf(const char *format, ...) { |
60 | va_list aptr; |
||
61 | int ret; |
||
62 | FILE *flog; |
||
195 | f9daq | 63 | |
210 | f9daq | 64 | va_start(aptr, format); |
65 | ret = vsprintf(strbuf, format, aptr); |
||
66 | va_end(aptr); |
||
67 | SetCtrlVal(ph,PANEL_STDIO,strbuf); |
||
68 | |||
69 | if (gLog) { |
||
70 | flog = fopen ("stdio.log", "a"); |
||
71 | fprintf (flog, "%s", strbuf); |
||
72 | fclose (flog); |
||
73 | } |
||
74 | return(ret); |
||
75 | } |
||
76 | |||
77 | |||
78 | |||
195 | f9daq | 79 | static void start_timer (double tout) { |
203 | f9daq | 80 | SetCtrlAttribute (ph, PANEL_TIMER, ATTR_INTERVAL, tout); |
81 | SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 1); |
||
195 | f9daq | 82 | } |
83 | |||
84 | static void stop_timer ( void ) { |
||
203 | f9daq | 85 | SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 0); |
86 | DRSSetTimeout(); |
||
195 | f9daq | 87 | } |
88 | |||
89 | |||
90 | |||
91 | |||
92 | void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle, |
||
203 | f9daq | 93 | CmtThreadFunctionID functionID, unsigned int event, |
94 | int value, void *callbackData ) { |
||
195 | f9daq | 95 | |
203 | f9daq | 96 | daq_on=0; |
97 | //SetDimming(0); |
||
98 | printf("End of Thread \n"); |
||
99 | return ; |
||
195 | f9daq | 100 | |
101 | } |
||
102 | |||
103 | |||
203 | f9daq | 104 | int CVICALLBACK daq(void *functionData) { |
195 | f9daq | 105 | |
106 | |||
203 | f9daq | 107 | int neve; |
108 | char filename[0xff]; |
||
109 | int imask[4]; |
||
110 | unsigned long mask; |
||
111 | int frequency; |
||
112 | double trgdelay; |
||
113 | double trglevel; |
||
114 | int trgtype; |
||
115 | int trgchannel; |
||
116 | int trgpolarity; |
||
117 | int verbose; |
||
118 | double range; |
||
119 | int pfreq; |
||
120 | int enabledoutput; |
||
121 | int neveold = 0; |
||
122 | double rate; |
||
123 | int *args = (int *) functionData; |
||
201 | f9daq | 124 | |
203 | f9daq | 125 | GetCtrlVal(ph, PANEL_FILENAME, filename ); |
126 | GetCtrlVal(ph, PANEL_CH0, &imask[0] ); |
||
127 | GetCtrlVal(ph, PANEL_CH1, &imask[1] ); |
||
128 | GetCtrlVal(ph, PANEL_CH2, &imask[2] ); |
||
129 | GetCtrlVal(ph, PANEL_CH3, &imask[3] ); |
||
130 | mask = 0; |
||
131 | for (int i=0; i<4; i++) { |
||
132 | if (imask[i]) mask |= (1<<i); |
||
201 | f9daq | 133 | } |
203 | f9daq | 134 | |
135 | GetCtrlVal(ph,PANEL_NEVE, &neve); |
||
136 | GetCtrlVal(ph,PANEL_DEBUG, &verbose); |
||
137 | GetCtrlVal(ph,PANEL_PFREQ, &pfreq); |
||
138 | GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput); |
||
139 | |||
140 | GetCtrlVal(ph,PANEL_FREQUENCY, &frequency); |
||
141 | GetCtrlVal(ph,PANEL_TRGDELAY, &trgdelay); |
||
142 | GetCtrlVal(ph,PANEL_TRGCHANNEL, &trgchannel); |
||
143 | |||
144 | GetCtrlVal(ph,PANEL_TRGTYPE, &trgtype); |
||
145 | GetCtrlVal(ph,PANEL_TRGLEVEL, &trglevel); |
||
146 | GetCtrlVal(ph,PANEL_TRGPOLARITY, &trgpolarity); |
||
147 | GetCtrlVal(ph,PANEL_RANGE, &range); |
||
148 | |||
149 | |||
150 | //printf("mask=0x%x\n",mask); |
||
151 | |||
152 | DRSSetMask( (unsigned char)( mask & 0xF ) ); |
||
153 | |||
154 | DRSSetFrequency( frequency ); |
||
155 | DRSSetTriggerDelay(trgdelay ); |
||
156 | DRSSetTriggerChannel(trgchannel ); |
||
157 | DRSSetTriggerType( trgtype ); |
||
158 | DRSSetTriggerLevel(trglevel); |
||
159 | DRSSetTriggerPolarity(trgpolarity); |
||
160 | |||
161 | DRSSetRange ( range ); |
||
162 | |||
163 | |||
164 | |||
165 | FILE *fp= NULL; |
||
166 | |||
167 | if (enabledoutput) { |
||
168 | if (args[0]) fp = fopen(filename,"wb"); |
||
169 | else fp = fopen(filename,"ab"); |
||
170 | } |
||
171 | |||
172 | static unsigned char *buffer; |
||
173 | |||
174 | int buffer_size = 0; |
||
175 | const int nBoards=1; |
||
176 | const int waveDepth=1024; |
||
207 | f9daq | 177 | |
203 | f9daq | 178 | if (buffer_size == 0) { |
207 | f9daq | 179 | buffer_size = 8; // file header + time header |
180 | buffer_size += nBoards * (4 + 4*(4+waveDepth*4)); // bin widths |
||
181 | buffer_size += 24 + nBoards * (8 + 4*(8+waveDepth*2)); |
||
182 | buffer = (unsigned char *)malloc(buffer_size); |
||
183 | } |
||
184 | |||
203 | f9daq | 185 | time_t t=0,told=0, tstart=0; |
186 | |||
187 | if (!DRSInit()) { |
||
188 | time(&tstart); |
||
189 | told=tstart; |
||
190 | int i=0; |
||
191 | for (i=0; i<neve; i++) { |
||
192 | start_timer(1);// 1 s timeout |
||
193 | int retval = DRSRead(0); |
||
194 | stop_timer(); |
||
195 | int nb = ( retval == 0 && fp ) ? DRSToBuffer( buffer , i ) : 0; |
||
196 | SetCtrlVal(ph,PANEL_CEVE,i); |
||
197 | if (retval) i--; |
||
198 | if (!daq_on) break; |
||
199 | time(&t); |
||
200 | if (t!=told ) { |
||
201 | rate = (i-neveold); |
||
202 | printf("%d events in %2.2f min (%d s) Rate %f Hz %s ",i+1, (double)(t-tstart)/60.,(t-tstart), rate , ctime(&t)); |
||
203 | GetCtrlVal(ph,PANEL_PFREQ, &pfreq); |
||
204 | neveold = i; |
||
205 | } |
||
206 | told=t; |
||
207 | // Save data |
||
208 | if (nb>0 && fp) fwrite(buffer, 1,nb ,fp); |
||
209 | // Plot Data |
||
210 | if (i % pfreq == 0) for (int k=0; k<4; k++) { |
||
211 | if (! (mask & ( 0x1<<k )) ) continue; |
||
212 | float *t=DRSGetTime(k); |
||
213 | float *x=DRSGetWave(k); |
||
214 | |||
215 | const int col[4]= {VAL_WHITE,VAL_RED,VAL_GREEN,VAL_BLUE}; |
||
216 | if (plothandle[k]) DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_IMMEDIATE_DRAW); |
||
217 | plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]); |
||
218 | |||
219 | for (int i=0 ; i<1024 ; i++) { |
||
220 | if (verbose) printf("[%d] %d. x= %3.2f y=%3.2f\n", k, i, t[i], x[i] ); |
||
221 | //h[k]->Fill( t[i], x[i]*1e-3); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | |||
226 | } |
||
227 | time(&t); |
||
228 | printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t)); |
||
229 | DRSEnd(); |
||
230 | } |
||
231 | |||
232 | if (fp) fclose(fp); |
||
233 | |||
234 | free(buffer); |
||
235 | |||
236 | return 0; |
||
237 | |||
195 | f9daq | 238 | } |
239 | |||
240 | |||
241 | |||
203 | f9daq | 242 | int CVICALLBACK scan(void *functionData) { |
195 | f9daq | 243 | |
207 | f9daq | 244 | int dx[3]={0,0,0}; |
245 | int nx[3]={0,0,0}; |
||
246 | int x0[3]={0,0,0}; |
||
247 | int ix[3]={0,0,0}; |
||
248 | int idx[3]={0,0,0}; |
||
203 | f9daq | 249 | int size; |
207 | f9daq | 250 | char posrec[4]="POSR"; |
251 | char runbuf[4]="PRUN"; |
||
203 | f9daq | 252 | |
207 | f9daq | 253 | int n[3]; |
203 | f9daq | 254 | char filename[0xFF]; |
255 | int enabledoutput; |
||
256 | |||
257 | FILE *fp; |
||
258 | GetCtrlVal(ph, PANEL_FILENAME, filename ); |
||
259 | |||
260 | GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput); |
||
261 | |||
262 | if ( GetFileInfo(filename,&size) ) { |
||
263 | MessagePopup ("Warning", "File exist. Remove it first or choose another file"); |
||
264 | return 0; |
||
265 | } |
||
266 | GetCtrlVal(p2, SCAN_STEPX, &dx[0]); |
||
267 | GetCtrlVal(p2, SCAN_STEPY, &dx[1]); |
||
268 | GetCtrlVal(p2, SCAN_NSTEPSX, &nx[0]); |
||
269 | GetCtrlVal(p2, SCAN_NSTEPSY, &nx[1]); |
||
270 | GetCtrlVal(p2, SCAN_STARTX, &x0[0]); |
||
271 | GetCtrlVal(p2, SCAN_STARTY, &x0[1]); |
||
207 | f9daq | 272 | |
273 | |||
274 | if (enabledoutput) { |
||
275 | fp = fopen(filename,"ab"); |
||
276 | if (fp) { |
||
277 | size=36; |
||
278 | fwrite(runbuf, 1,4 ,fp); |
||
279 | fwrite(&size , 1,4 ,fp); |
||
280 | fwrite(x0 , 1,4*3 ,fp); |
||
281 | fwrite(dx , 1,4*3 ,fp); |
||
282 | fwrite(nx , 1,4*3 ,fp); |
||
283 | fclose(fp); |
||
284 | } |
||
285 | } |
||
286 | |||
203 | f9daq | 287 | for (int i=0; i<nx[0]; i++) { |
288 | |||
289 | ix[0]= x0[0]+i*dx[0]; |
||
290 | #ifdef MIKRO |
||
291 | MIKRO_MoveTo(1,ix[0]); |
||
292 | #endif |
||
293 | SetCtrlVal (p2, SCAN_IX, i); |
||
294 | for (int j=0; j<nx[1]; j++) { |
||
295 | |||
296 | SetCtrlVal (p2, SCAN_IY, j); |
||
297 | |||
298 | ix[1]= x0[1]+j*dx[1]; |
||
299 | #ifdef MIKRO |
||
300 | MIKRO_MoveTo(2,ix[1]); |
||
301 | |||
302 | MIKRO_GetPosition(1,&n[0]); |
||
303 | SetCtrlVal (p2, SCAN_XP, n[0]); |
||
304 | MIKRO_GetPosition(2,&n[1]); |
||
305 | SetCtrlVal (p2, SCAN_YP, n[1]); |
||
207 | f9daq | 306 | |
307 | if (enabledoutput) { |
||
203 | f9daq | 308 | fp = fopen(filename,"ab"); |
309 | if (fp) { |
||
207 | f9daq | 310 | idx[0]=i; |
311 | idx[1]=j; |
||
312 | size=24; |
||
313 | fwrite(posrec, 1,4 ,fp); |
||
314 | fwrite(&size , 1,4 ,fp); |
||
315 | fwrite(n , 1,4*3 ,fp); |
||
316 | fwrite(idx , 1,4*3 ,fp); |
||
203 | f9daq | 317 | fclose(fp); |
318 | } |
||
319 | } |
||
320 | |||
321 | #endif |
||
322 | daq(functionData); |
||
323 | if (!daq_on) break; |
||
324 | } |
||
325 | if (!daq_on) break; |
||
326 | } |
||
327 | |||
328 | return 0; |
||
195 | f9daq | 329 | } |
330 | |||
203 | f9daq | 331 | |
195 | f9daq | 332 | int CVICALLBACK StartCB (int panel, int control, int event, |
203 | f9daq | 333 | void *callbackData, int eventData1, int eventData2) { |
334 | ThreadFunctionPtr mythread = NULL; |
||
335 | switch (event) { |
||
336 | |||
337 | case EVENT_COMMIT: |
||
338 | |||
339 | controlID=0; |
||
340 | if (panel == ph && control == PANEL_START) { |
||
341 | mythread = daq; |
||
342 | controlID= control; |
||
343 | } |
||
344 | if (panel == p2 && control == SCAN_SCAN) mythread = scan; |
||
345 | if (mythread!=NULL) { |
||
346 | printf("New Thread panel=%d button=%d\n", panel, control); |
||
347 | |||
348 | // SetDimming(1); |
||
349 | |||
350 | daq_on=1; |
||
351 | CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID, |
||
352 | DEFAULT_THREAD_PRIORITY, |
||
353 | EndOfThread, |
||
354 | EVENT_TP_THREAD_FUNCTION_END, |
||
355 | NULL, RUN_IN_SCHEDULED_THREAD, |
||
356 | &tfID); |
||
357 | } |
||
358 | break; |
||
359 | } |
||
360 | return 0; |
||
195 | f9daq | 361 | } |
362 | |||
363 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
203 | f9daq | 364 | void *callbackData, int eventData1, int eventData2) { |
365 | switch (event) { |
||
366 | case EVENT_COMMIT: |
||
367 | daq_on=0; |
||
368 | break; |
||
369 | } |
||
370 | return 0; |
||
195 | f9daq | 371 | } |
372 | |||
373 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
203 | f9daq | 374 | void *callbackData, int eventData1, int eventData2) { |
375 | switch (event) { |
||
376 | case EVENT_COMMIT: |
||
377 | QuitUserInterface (0); |
||
378 | break; |
||
379 | } |
||
380 | return 0; |
||
195 | f9daq | 381 | } |
203 | f9daq | 382 | |
383 | int CVICALLBACK MoveStageCB (int panel, int control, int event, |
||
384 | void *callbackData, int eventData1, int eventData2) { |
||
385 | int axis=0, step=1000, direction=1, n; |
||
386 | switch (event) { |
||
387 | case EVENT_COMMIT: |
||
388 | |||
389 | if (panel == p2) { |
||
390 | switch (control) { |
||
391 | case SCAN_BR : |
||
392 | axis = 1; |
||
393 | direction = 1; |
||
394 | GetCtrlVal(p2, SCAN_STEPX, &step); |
||
395 | break; |
||
396 | case SCAN_BL : |
||
397 | axis = 1; |
||
398 | direction = -1; |
||
399 | GetCtrlVal(p2, SCAN_STEPX, &step); |
||
400 | break; |
||
401 | case SCAN_BU : |
||
402 | axis = 2; |
||
403 | direction = 1; |
||
404 | GetCtrlVal(p2, SCAN_STEPY, &step); |
||
405 | break; |
||
406 | case SCAN_BD : |
||
407 | axis = 2; |
||
408 | direction = -1; |
||
409 | GetCtrlVal(p2, SCAN_STEPY, &step); |
||
410 | break; |
||
411 | } |
||
412 | #ifdef MIKRO |
||
413 | MIKRO_MoveFor(axis, direction*step ); |
||
414 | MIKRO_GetPosition(axis,&n); |
||
415 | if (axis == 1) SetCtrlVal (p2, SCAN_XP, n); |
||
416 | if (axis == 2) SetCtrlVal (p2, SCAN_YP, n); |
||
417 | #endif // MIKRO |
||
418 | } |
||
419 | |||
420 | break; |
||
421 | } |
||
422 | return 0; |
||
423 | } |
||
424 | |||
425 | |||
426 | |||
427 | |||
428 | |||
429 | int CVICALLBACK GoXCB (int panel, int control, int event, |
||
430 | void *callbackData, int eventData1, int eventData2) { |
||
431 | int n2; |
||
432 | switch (event) { |
||
433 | case EVENT_COMMIT: |
||
434 | GetCtrlVal (p2, SCAN_XG, &n2); |
||
435 | #ifdef MIKRO |
||
436 | MIKRO_MoveTo(1,n2); |
||
437 | MIKRO_GetPosition(1,&n2); |
||
438 | |||
439 | #endif |
||
440 | SetCtrlVal (p2, SCAN_XP, n2); |
||
441 | break; |
||
442 | } |
||
443 | return 0; |
||
444 | } |
||
445 | |||
446 | int CVICALLBACK GoYCB (int panel, int control, int event, |
||
447 | void *callbackData, int eventData1, int eventData2) { |
||
448 | int n2; |
||
449 | switch (event) { |
||
450 | case EVENT_COMMIT: |
||
451 | GetCtrlVal (p2, SCAN_YG, &n2); |
||
452 | #ifdef MIKRO |
||
453 | MIKRO_MoveTo(2,n2); |
||
454 | MIKRO_GetPosition(2,&n2); |
||
455 | |||
456 | #endif |
||
457 | SetCtrlVal (p2, SCAN_YP, n2); |
||
458 | break; |
||
459 | } |
||
460 | return 0; |
||
461 | } |
||
462 | |||
463 | int CVICALLBACK GetCurrentPositionCB (int panel, int control, int event, |
||
464 | void *callbackData, int eventData1, int eventData2) { |
||
465 | |||
466 | int n[2]; |
||
467 | switch (event) { |
||
468 | case EVENT_COMMIT: |
||
469 | #ifdef MIKRO |
||
470 | MIKRO_GetPosition(1,&n[0]); |
||
471 | SetCtrlVal (p2, SCAN_XP, n[0]); |
||
472 | MIKRO_GetPosition(2,&n[1]); |
||
473 | SetCtrlVal (p2, SCAN_YP, n[1]); |
||
474 | #endif |
||
475 | break; |
||
476 | } |
||
477 | return 0; |
||
478 | } |
||
479 | |||
480 | int CVICALLBACK HomeCB (int panel, int control, int event, |
||
481 | void *callbackData, int eventData1, int eventData2) { |
||
482 | switch (event) { |
||
483 | case EVENT_COMMIT: |
||
484 | #ifdef MIKRO |
||
485 | MIKRO_ReferenceMove(1); |
||
486 | MIKRO_ReferenceMove(2); |
||
487 | GetCurrentPositionCB(panel, control, event, NULL, 0, 0); |
||
488 | #endif |
||
489 | break; |
||
490 | } |
||
491 | return 0; |
||
492 | } |