Rev 203 | Rev 210 | 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; |
||
207 | f9daq | 156 | |
203 | f9daq | 157 | if (buffer_size == 0) { |
207 | f9daq | 158 | buffer_size = 8; // file header + time header |
159 | buffer_size += nBoards * (4 + 4*(4+waveDepth*4)); // bin widths |
||
160 | buffer_size += 24 + nBoards * (8 + 4*(8+waveDepth*2)); |
||
161 | buffer = (unsigned char *)malloc(buffer_size); |
||
162 | } |
||
163 | |||
203 | f9daq | 164 | time_t t=0,told=0, tstart=0; |
165 | |||
166 | if (!DRSInit()) { |
||
167 | time(&tstart); |
||
168 | told=tstart; |
||
169 | int i=0; |
||
170 | for (i=0; i<neve; i++) { |
||
171 | start_timer(1);// 1 s timeout |
||
172 | int retval = DRSRead(0); |
||
173 | stop_timer(); |
||
174 | int nb = ( retval == 0 && fp ) ? DRSToBuffer( buffer , i ) : 0; |
||
175 | SetCtrlVal(ph,PANEL_CEVE,i); |
||
176 | if (retval) i--; |
||
177 | if (!daq_on) break; |
||
178 | time(&t); |
||
179 | if (t!=told ) { |
||
180 | rate = (i-neveold); |
||
181 | printf("%d events in %2.2f min (%d s) Rate %f Hz %s ",i+1, (double)(t-tstart)/60.,(t-tstart), rate , ctime(&t)); |
||
182 | GetCtrlVal(ph,PANEL_PFREQ, &pfreq); |
||
183 | neveold = i; |
||
184 | } |
||
185 | told=t; |
||
186 | // Save data |
||
187 | if (nb>0 && fp) fwrite(buffer, 1,nb ,fp); |
||
188 | // Plot Data |
||
189 | if (i % pfreq == 0) for (int k=0; k<4; k++) { |
||
190 | if (! (mask & ( 0x1<<k )) ) continue; |
||
191 | float *t=DRSGetTime(k); |
||
192 | float *x=DRSGetWave(k); |
||
193 | |||
194 | const int col[4]= {VAL_WHITE,VAL_RED,VAL_GREEN,VAL_BLUE}; |
||
195 | if (plothandle[k]) DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_IMMEDIATE_DRAW); |
||
196 | plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]); |
||
197 | |||
198 | for (int i=0 ; i<1024 ; i++) { |
||
199 | if (verbose) printf("[%d] %d. x= %3.2f y=%3.2f\n", k, i, t[i], x[i] ); |
||
200 | //h[k]->Fill( t[i], x[i]*1e-3); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | |||
205 | } |
||
206 | time(&t); |
||
207 | printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t)); |
||
208 | DRSEnd(); |
||
209 | } |
||
210 | |||
211 | if (fp) fclose(fp); |
||
212 | |||
213 | free(buffer); |
||
214 | |||
215 | return 0; |
||
216 | |||
195 | f9daq | 217 | } |
218 | |||
219 | |||
220 | |||
203 | f9daq | 221 | int CVICALLBACK scan(void *functionData) { |
195 | f9daq | 222 | |
207 | f9daq | 223 | int dx[3]={0,0,0}; |
224 | int nx[3]={0,0,0}; |
||
225 | int x0[3]={0,0,0}; |
||
226 | int ix[3]={0,0,0}; |
||
227 | int idx[3]={0,0,0}; |
||
203 | f9daq | 228 | int size; |
207 | f9daq | 229 | char posrec[4]="POSR"; |
230 | char runbuf[4]="PRUN"; |
||
203 | f9daq | 231 | |
207 | f9daq | 232 | int n[3]; |
203 | f9daq | 233 | char filename[0xFF]; |
234 | int enabledoutput; |
||
235 | |||
236 | FILE *fp; |
||
237 | GetCtrlVal(ph, PANEL_FILENAME, filename ); |
||
238 | |||
239 | GetCtrlVal(ph,PANEL_ENABLEDOUTPUT, &enabledoutput); |
||
240 | |||
241 | if ( GetFileInfo(filename,&size) ) { |
||
242 | MessagePopup ("Warning", "File exist. Remove it first or choose another file"); |
||
243 | return 0; |
||
244 | } |
||
245 | GetCtrlVal(p2, SCAN_STEPX, &dx[0]); |
||
246 | GetCtrlVal(p2, SCAN_STEPY, &dx[1]); |
||
247 | GetCtrlVal(p2, SCAN_NSTEPSX, &nx[0]); |
||
248 | GetCtrlVal(p2, SCAN_NSTEPSY, &nx[1]); |
||
249 | GetCtrlVal(p2, SCAN_STARTX, &x0[0]); |
||
250 | GetCtrlVal(p2, SCAN_STARTY, &x0[1]); |
||
207 | f9daq | 251 | |
252 | |||
253 | if (enabledoutput) { |
||
254 | fp = fopen(filename,"ab"); |
||
255 | if (fp) { |
||
256 | size=36; |
||
257 | fwrite(runbuf, 1,4 ,fp); |
||
258 | fwrite(&size , 1,4 ,fp); |
||
259 | fwrite(x0 , 1,4*3 ,fp); |
||
260 | fwrite(dx , 1,4*3 ,fp); |
||
261 | fwrite(nx , 1,4*3 ,fp); |
||
262 | fclose(fp); |
||
263 | } |
||
264 | } |
||
265 | |||
203 | f9daq | 266 | for (int i=0; i<nx[0]; i++) { |
267 | |||
268 | ix[0]= x0[0]+i*dx[0]; |
||
269 | #ifdef MIKRO |
||
270 | MIKRO_MoveTo(1,ix[0]); |
||
271 | #endif |
||
272 | SetCtrlVal (p2, SCAN_IX, i); |
||
273 | for (int j=0; j<nx[1]; j++) { |
||
274 | |||
275 | SetCtrlVal (p2, SCAN_IY, j); |
||
276 | |||
277 | ix[1]= x0[1]+j*dx[1]; |
||
278 | #ifdef MIKRO |
||
279 | MIKRO_MoveTo(2,ix[1]); |
||
280 | |||
281 | MIKRO_GetPosition(1,&n[0]); |
||
282 | SetCtrlVal (p2, SCAN_XP, n[0]); |
||
283 | MIKRO_GetPosition(2,&n[1]); |
||
284 | SetCtrlVal (p2, SCAN_YP, n[1]); |
||
207 | f9daq | 285 | |
286 | if (enabledoutput) { |
||
203 | f9daq | 287 | fp = fopen(filename,"ab"); |
288 | if (fp) { |
||
207 | f9daq | 289 | idx[0]=i; |
290 | idx[1]=j; |
||
291 | size=24; |
||
292 | fwrite(posrec, 1,4 ,fp); |
||
293 | fwrite(&size , 1,4 ,fp); |
||
294 | fwrite(n , 1,4*3 ,fp); |
||
295 | fwrite(idx , 1,4*3 ,fp); |
||
203 | f9daq | 296 | fclose(fp); |
297 | } |
||
298 | } |
||
299 | |||
300 | #endif |
||
301 | daq(functionData); |
||
302 | if (!daq_on) break; |
||
303 | } |
||
304 | if (!daq_on) break; |
||
305 | } |
||
306 | |||
307 | return 0; |
||
195 | f9daq | 308 | } |
309 | |||
203 | f9daq | 310 | |
195 | f9daq | 311 | int CVICALLBACK StartCB (int panel, int control, int event, |
203 | f9daq | 312 | void *callbackData, int eventData1, int eventData2) { |
313 | ThreadFunctionPtr mythread = NULL; |
||
314 | switch (event) { |
||
315 | |||
316 | case EVENT_COMMIT: |
||
317 | |||
318 | controlID=0; |
||
319 | if (panel == ph && control == PANEL_START) { |
||
320 | mythread = daq; |
||
321 | controlID= control; |
||
322 | } |
||
323 | if (panel == p2 && control == SCAN_SCAN) mythread = scan; |
||
324 | if (mythread!=NULL) { |
||
325 | printf("New Thread panel=%d button=%d\n", panel, control); |
||
326 | |||
327 | // SetDimming(1); |
||
328 | |||
329 | daq_on=1; |
||
330 | CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID, |
||
331 | DEFAULT_THREAD_PRIORITY, |
||
332 | EndOfThread, |
||
333 | EVENT_TP_THREAD_FUNCTION_END, |
||
334 | NULL, RUN_IN_SCHEDULED_THREAD, |
||
335 | &tfID); |
||
336 | } |
||
337 | break; |
||
338 | } |
||
339 | return 0; |
||
195 | f9daq | 340 | } |
341 | |||
342 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
203 | f9daq | 343 | void *callbackData, int eventData1, int eventData2) { |
344 | switch (event) { |
||
345 | case EVENT_COMMIT: |
||
346 | daq_on=0; |
||
347 | break; |
||
348 | } |
||
349 | return 0; |
||
195 | f9daq | 350 | } |
351 | |||
352 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
203 | f9daq | 353 | void *callbackData, int eventData1, int eventData2) { |
354 | switch (event) { |
||
355 | case EVENT_COMMIT: |
||
356 | QuitUserInterface (0); |
||
357 | break; |
||
358 | } |
||
359 | return 0; |
||
195 | f9daq | 360 | } |
203 | f9daq | 361 | |
362 | int CVICALLBACK MoveStageCB (int panel, int control, int event, |
||
363 | void *callbackData, int eventData1, int eventData2) { |
||
364 | int axis=0, step=1000, direction=1, n; |
||
365 | switch (event) { |
||
366 | case EVENT_COMMIT: |
||
367 | |||
368 | if (panel == p2) { |
||
369 | switch (control) { |
||
370 | case SCAN_BR : |
||
371 | axis = 1; |
||
372 | direction = 1; |
||
373 | GetCtrlVal(p2, SCAN_STEPX, &step); |
||
374 | break; |
||
375 | case SCAN_BL : |
||
376 | axis = 1; |
||
377 | direction = -1; |
||
378 | GetCtrlVal(p2, SCAN_STEPX, &step); |
||
379 | break; |
||
380 | case SCAN_BU : |
||
381 | axis = 2; |
||
382 | direction = 1; |
||
383 | GetCtrlVal(p2, SCAN_STEPY, &step); |
||
384 | break; |
||
385 | case SCAN_BD : |
||
386 | axis = 2; |
||
387 | direction = -1; |
||
388 | GetCtrlVal(p2, SCAN_STEPY, &step); |
||
389 | break; |
||
390 | } |
||
391 | #ifdef MIKRO |
||
392 | MIKRO_MoveFor(axis, direction*step ); |
||
393 | MIKRO_GetPosition(axis,&n); |
||
394 | if (axis == 1) SetCtrlVal (p2, SCAN_XP, n); |
||
395 | if (axis == 2) SetCtrlVal (p2, SCAN_YP, n); |
||
396 | #endif // MIKRO |
||
397 | } |
||
398 | |||
399 | break; |
||
400 | } |
||
401 | return 0; |
||
402 | } |
||
403 | |||
404 | |||
405 | |||
406 | |||
407 | |||
408 | int CVICALLBACK GoXCB (int panel, int control, int event, |
||
409 | void *callbackData, int eventData1, int eventData2) { |
||
410 | int n2; |
||
411 | switch (event) { |
||
412 | case EVENT_COMMIT: |
||
413 | GetCtrlVal (p2, SCAN_XG, &n2); |
||
414 | #ifdef MIKRO |
||
415 | MIKRO_MoveTo(1,n2); |
||
416 | MIKRO_GetPosition(1,&n2); |
||
417 | |||
418 | #endif |
||
419 | SetCtrlVal (p2, SCAN_XP, n2); |
||
420 | break; |
||
421 | } |
||
422 | return 0; |
||
423 | } |
||
424 | |||
425 | int CVICALLBACK GoYCB (int panel, int control, int event, |
||
426 | void *callbackData, int eventData1, int eventData2) { |
||
427 | int n2; |
||
428 | switch (event) { |
||
429 | case EVENT_COMMIT: |
||
430 | GetCtrlVal (p2, SCAN_YG, &n2); |
||
431 | #ifdef MIKRO |
||
432 | MIKRO_MoveTo(2,n2); |
||
433 | MIKRO_GetPosition(2,&n2); |
||
434 | |||
435 | #endif |
||
436 | SetCtrlVal (p2, SCAN_YP, n2); |
||
437 | break; |
||
438 | } |
||
439 | return 0; |
||
440 | } |
||
441 | |||
442 | int CVICALLBACK GetCurrentPositionCB (int panel, int control, int event, |
||
443 | void *callbackData, int eventData1, int eventData2) { |
||
444 | |||
445 | int n[2]; |
||
446 | switch (event) { |
||
447 | case EVENT_COMMIT: |
||
448 | #ifdef MIKRO |
||
449 | MIKRO_GetPosition(1,&n[0]); |
||
450 | SetCtrlVal (p2, SCAN_XP, n[0]); |
||
451 | MIKRO_GetPosition(2,&n[1]); |
||
452 | SetCtrlVal (p2, SCAN_YP, n[1]); |
||
453 | #endif |
||
454 | break; |
||
455 | } |
||
456 | return 0; |
||
457 | } |
||
458 | |||
459 | int CVICALLBACK HomeCB (int panel, int control, int event, |
||
460 | void *callbackData, int eventData1, int eventData2) { |
||
461 | switch (event) { |
||
462 | case EVENT_COMMIT: |
||
463 | #ifdef MIKRO |
||
464 | MIKRO_ReferenceMove(1); |
||
465 | MIKRO_ReferenceMove(2); |
||
466 | GetCurrentPositionCB(panel, control, event, NULL, 0, 0); |
||
467 | #endif |
||
468 | break; |
||
469 | } |
||
470 | return 0; |
||
471 | } |