Rev 292 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
212 | f9daq | 1 | #include <formatio.h> |
2 | #include <cvirte.h> |
||
3 | #include <userint.h> |
||
4 | #include "AitGui.h" |
||
5 | #include "XYSCAN.h" |
||
6 | |||
7 | #include <stdbool.h> |
||
8 | #include "AitInterface.h" |
||
9 | #include "AitMduManager_DEF.h" |
||
10 | |||
11 | #include "H1D.h" |
||
12 | #include "H2D.h" |
||
13 | #include "H3D.h" |
||
14 | |||
15 | |||
16 | typedef unsigned short ushort; |
||
17 | typedef unsigned int uint; |
||
18 | |||
19 | static int ait; |
||
20 | static int xyscan; |
||
21 | //#define MIKRO |
||
22 | |||
23 | #ifdef MIKRO |
||
24 | #include "MIKRO.h" |
||
25 | #endif |
||
26 | |||
27 | static int daq_on; |
||
28 | static int plothandle[4]= {0,0,0, 0}; |
||
29 | static int tfID; |
||
30 | static int controlID; |
||
31 | static int verbose; |
||
32 | |||
33 | #define MAX_THREADS 10 |
||
34 | |||
35 | static CmtThreadPoolHandle poolHandle = 0; |
||
36 | |||
37 | |||
38 | /* |
||
39 | |||
40 | x: 140000 280000 |
||
41 | |||
42 | */ |
||
43 | |||
44 | //**************************** missing declarations in the library |
||
45 | char strbuf[0xFF]; |
||
46 | |||
47 | int gLog=0; |
||
48 | |||
49 | int printf(const char *format, ...) { |
||
50 | va_list aptr; |
||
51 | int ret; |
||
52 | FILE *flog; |
||
53 | |||
54 | va_start(aptr, format); |
||
55 | ret = vsprintf(strbuf, format, aptr); |
||
56 | va_end(aptr); |
||
57 | SetCtrlVal(ait,AIT_STDIO,strbuf); |
||
58 | |||
59 | if (gLog) { |
||
60 | flog = fopen ("stdio.log", "a"); |
||
61 | fprintf (flog, "%s", strbuf); |
||
62 | fclose (flog); |
||
63 | } |
||
64 | return(ret); |
||
65 | } |
||
66 | |||
67 | |||
68 | |||
69 | static int timeout=0; |
||
70 | int CVICALLBACK SetTimeoutCB (int panel, int control, int event, |
||
71 | void *callbackData, int eventData1, int eventData2) { |
||
72 | switch (event) { |
||
73 | case EVENT_TIMER_TICK: |
||
74 | timeout=1; |
||
75 | printf("#### Timeout\n"); |
||
76 | break; |
||
77 | } |
||
78 | return 0; |
||
79 | } |
||
80 | |||
81 | |||
82 | |||
83 | static void start_timer (double tout) { |
||
84 | timeout = 0; |
||
85 | SetCtrlAttribute (ait, AIT_TIMER, ATTR_INTERVAL, tout); |
||
86 | SetCtrlAttribute (ait, AIT_TIMER, ATTR_ENABLED, 1); |
||
87 | } |
||
88 | |||
89 | static void stop_timer ( void ) { |
||
90 | SetCtrlAttribute (ait, AIT_TIMER, ATTR_ENABLED, 0); |
||
91 | //DRSSetTimeout(); |
||
92 | } |
||
93 | |||
94 | |||
95 | |||
96 | void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle, |
||
97 | CmtThreadFunctionID functionID, unsigned int event, |
||
98 | int value, void *callbackData ) { |
||
99 | |||
100 | daq_on=0; |
||
101 | //SetDimming(0); |
||
102 | printf("End of Thread \n"); |
||
103 | return ; |
||
104 | |||
105 | } |
||
106 | |||
107 | int AitInit() { |
||
108 | InitializeConnection(); |
||
109 | return 0; |
||
110 | } |
||
111 | |||
112 | |||
113 | int AitValidEvent() { |
||
114 | |||
115 | return 1; |
||
116 | }; |
||
117 | |||
118 | int AitToFile(FILE *fp, int n) { |
||
119 | const int bsize=10000 ; |
||
120 | unsigned short a[bsize] ; |
||
121 | unsigned short buffer[2]={0x1}; |
||
122 | buffer[1] = (unsigned short)(n*CHANNELS*2); |
||
123 | for (int eventNum = 0; eventNum < n; eventNum++) { |
||
124 | |||
125 | for (int channel = 0; channel < CHANNELS; channel++) { |
||
126 | int index = channel+ CHANNELS * eventNum; |
||
127 | if (index<bsize) |
||
128 | a[index] = ExtractAdcData(eventNum, channel); |
||
129 | } |
||
130 | } |
||
131 | fwrite(buffer, 2*sizeof(short),1,fp); |
||
132 | return fwrite(a, 1,buffer[1],fp); |
||
133 | }; |
||
134 | |||
135 | int AitFillHistograms(int events) { |
||
136 | ushort rawAdcData = 0; |
||
137 | ushort adcData = 0; |
||
138 | int channelReceived = 0; |
||
139 | int channelExpected = 0; |
||
140 | bool includeSaturated = 0; // cbIncludeSaturated.Checked; |
||
141 | ushort max[CHANNELS]; |
||
142 | ushort min[CHANNELS]; |
||
143 | ushort average[CHANNELS]; |
||
144 | int total[CHANNELS]; |
||
145 | for (int i = 0; i < CHANNELS; i++) { |
||
146 | min[i] = 0xFFFF; |
||
147 | max[i] = 0; |
||
148 | total[i] = 0; |
||
149 | } |
||
150 | |||
151 | double threshold, xysumcut; |
||
152 | GetCtrlVal (ait, AIT_ADCTHRESHOLD, &threshold); |
||
153 | GetCtrlVal (ait, AIT_XYCUT, &xysumcut); |
||
154 | |||
155 | for (int eventNum = 0; eventNum < events; eventNum++) { |
||
156 | |||
157 | int sum = 0; |
||
158 | double a[4]= {0,0,0,0} ; |
||
159 | for (int channel = 0; channel < CHANNELS; channel++) { |
||
160 | |||
161 | rawAdcData = ExtractAdcData(eventNum, channel); |
||
162 | if (addChannelNum == true) { |
||
163 | channelReceived = (rawAdcData >> 12) & 0xF; |
||
164 | channelExpected = (channel & 0xF); |
||
165 | if (channelReceived != channelExpected) { |
||
166 | |||
167 | AddMessage("ERROR in Event %d : Expected channel %d, received %d -ANALYSIS STOPPED", eventNum, channelExpected, channelReceived); |
||
168 | |||
169 | //EnableRegisterScanTimer(true); |
||
170 | return eventNum; |
||
171 | } |
||
172 | } |
||
173 | adcData = (ushort)(rawAdcData & 0x0FFF); // remove channel number |
||
174 | //if ((includeSaturated == true) || ((includeSaturated == false) && (adcData < (ADCMAX - 2)))) |
||
175 | |||
176 | if (adcData>threshold) H1D_Fill(channel, adcData,1); |
||
177 | a[channel]=adcData; |
||
178 | total[channel] = total[channel] + adcData; |
||
179 | if (adcData > max[channel]) |
||
180 | max[channel] = adcData; |
||
181 | else if (adcData < min[channel]) |
||
182 | min[channel] = adcData; |
||
183 | |||
184 | sum += adcData; |
||
185 | } |
||
186 | if (sum>threshold) { |
||
187 | H1D_Fill(10, sum, 1 ); |
||
188 | double x = a[0]/(a[0]+a[1]); |
||
189 | double y = a[2]/(a[2]+a[3]); |
||
190 | if (sum>xysumcut) H2D_Fill(0, x, y , 1 ); |
||
191 | } |
||
192 | histogramTotalEvents++; |
||
193 | } |
||
194 | //tbAdcStatistics.Clear(); |
||
195 | for (int channel = 0; channel < CHANNELS; channel++) { |
||
196 | average[channel] = (ushort)(total[channel] / events); |
||
197 | |||
198 | if (verbose) AddMessage(" {channel:%d} : {min:0x%d} {max:0x%d} {max-min:%d} {average:%d}\n", channel + 1, min[channel], max[channel], max[channel] - min[channel], average[channel]); |
||
199 | //if (channel < (CHANNELS - 1)) |
||
200 | // tbAdcStatistics.AppendText(Environment.NewLine); |
||
201 | |||
202 | } |
||
203 | //SetTextboxToTop(tbAdcStatistics); |
||
204 | return 1; |
||
205 | }; |
||
206 | |||
207 | |||
208 | int CVICALLBACK RefreshGraphs (int panel, int control, int event, |
||
209 | void *callbackData, int eventData1, int eventData2) { |
||
210 | switch (event) { |
||
211 | case EVENT_TIMER_TICK: |
||
212 | // if (!daq_on ) return 0; |
||
213 | case EVENT_COMMIT: { |
||
214 | |||
215 | int ch=0; |
||
216 | int logy=0; |
||
217 | int updateplots=0; |
||
218 | GetCtrlVal(ait,AIT_CH, &ch); |
||
219 | GetCtrlVal(ait,AIT_LOGY, &logy); |
||
220 | GetCtrlVal(ait,AIT_UPDATEPLOTS, &updateplots); |
||
221 | if (!updateplots) return 0; |
||
222 | if (logy){ |
||
223 | SetCtrlAttribute (ait, AIT_GRAPH, ATTR_YMAP_MODE, VAL_LOG); |
||
224 | SetCtrlAttribute (ait, AIT_GRAPHSUM, ATTR_YMAP_MODE, VAL_LOG); |
||
225 | |||
226 | } else { |
||
227 | SetCtrlAttribute (ait, AIT_GRAPH, ATTR_YMAP_MODE, VAL_LINEAR); |
||
228 | SetCtrlAttribute (ait, AIT_GRAPHSUM, ATTR_YMAP_MODE, VAL_LINEAR); |
||
229 | |||
230 | } |
||
231 | H1D_Draw(ch,ait,AIT_GRAPH,&plothandle[0]); |
||
232 | H1D_Draw(10,ait,AIT_GRAPHSUM,&plothandle[1]); |
||
233 | H2D_Draw(0,ait,AIT_GRAPHXY,&plothandle[2]); |
||
234 | |||
235 | |||
236 | } |
||
237 | |||
238 | |||
239 | break; |
||
240 | |||
241 | } |
||
242 | return 0; |
||
243 | } |
||
244 | |||
245 | |||
246 | |||
247 | int CVICALLBACK daq(void *functionData) { |
||
248 | |||
249 | |||
250 | int neve; |
||
251 | char filename[0xff]; |
||
252 | char fname[0xff]; |
||
253 | char pname[0xff]; |
||
254 | |||
255 | |||
256 | |||
257 | int pfreq; |
||
258 | int enabledoutput; |
||
259 | int neveold = 0; |
||
260 | double rate; |
||
261 | int *args = (int *) functionData; |
||
262 | |||
263 | GetCtrlVal(ait, AIT_FILENAME, fname ); |
||
264 | GetCtrlVal(ait, AIT_PATHNAME, pname ); |
||
265 | sprintf(filename,"%s/%s", pname, fname); |
||
266 | |||
267 | GetCtrlVal(ait,AIT_NEVE, &neve); |
||
268 | GetCtrlVal(ait,AIT_DEBUG, &verbose); |
||
269 | GetCtrlVal(ait,AIT_PFREQ, &pfreq); |
||
270 | GetCtrlVal(ait,AIT_ENABLEDOUTPUT, &enabledoutput); |
||
271 | |||
272 | |||
273 | int range=0; |
||
274 | int nch = range; |
||
275 | GetCtrlVal(ait,AIT_MAX, &range); |
||
276 | if (range>512) nch = 512; |
||
277 | |||
278 | |||
279 | for (int i=0; i<4; i++) { |
||
280 | char name[0xFF]; |
||
281 | sprintf(name,"adc%d", i); |
||
282 | |||
283 | H1D_Init(i, name,name, range, 0 , range); |
||
284 | H1D_SetTitleX(0,"ADC"); |
||
285 | H1D_SetTitleY(0,"N"); |
||
286 | } |
||
287 | H1D_Init(10, "adcsum","adcsum", range, 0 , 4*range); |
||
288 | H1D_SetTitleX(0,"ADC"); |
||
289 | H1D_SetTitleY(0,"N"); |
||
290 | |||
291 | H2D_Init(0, "cog","Center of gravity", 256, 0 ,1, 256, 0 ,1); |
||
292 | H2D_SetTitleX(0,"x"); |
||
293 | H2D_SetTitleY(0,"y"); |
||
294 | |||
295 | |||
296 | |||
297 | FILE *fp= NULL; |
||
298 | |||
299 | if (enabledoutput) { |
||
300 | if (args[0]) fp = fopen(filename,"wb"); |
||
301 | else fp = fopen(filename,"ab"); |
||
302 | } |
||
303 | |||
304 | |||
305 | |||
306 | |||
307 | |||
308 | time_t t=0,told=0, tstart=0; |
||
309 | |||
310 | if (!AitInit()) { |
||
311 | |||
312 | |||
313 | ReadRegisters(); |
||
314 | |||
315 | |||
316 | // Update UI |
||
317 | int csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
318 | |||
319 | int value = ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN2) ? 1 : 0) << 1) | |
||
320 | ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN1) ? 1 : 0) << 0); |
||
321 | SetCtrlVal (ait, AIT_GETADCGAIN, value); |
||
322 | double valued = GetThreshold(); |
||
323 | SetCtrlVal (ait, AIT_GETADCTHRESHOLD, valued); |
||
324 | valued = GetIntegrationTime(); |
||
325 | SetCtrlVal (ait, AIT_GETADCINTEGRATIONTIME, valued); |
||
326 | valued = GetHvVoltage(ADDR_HV_VMON); |
||
327 | SetCtrlVal (ait, AIT_BIASMON, valued); |
||
328 | |||
329 | value = |
||
330 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN4) ? 1 : 0) << 3) | |
||
331 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN3) ? 1 : 0) << 2) | |
||
332 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN2) ? 1 : 0) << 1) | |
||
333 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN1) ? 1 : 0) << 0); |
||
334 | SetCtrlVal (ait, AIT_GETADCSUMGAIN, value); |
||
335 | |||
336 | |||
337 | |||
338 | RefreshGraphs (ait, 0 , EVENT_COMMIT ,NULL, 0, 0 ); |
||
339 | |||
340 | daq_on=1; |
||
341 | //RefreshGraphs (ait, 0 , EVENT_TIMER_TICK ,NULL, 0, 0 ); |
||
342 | time(&tstart); |
||
343 | told=tstart; |
||
344 | int i=0; |
||
345 | for (i=0; i<neve; i++) { |
||
346 | start_timer(1);// 1 s timeout |
||
347 | // Set ADC reset |
||
348 | ResetAdc(true); |
||
349 | // Clear ADC reset |
||
350 | ResetAdc(false); |
||
351 | int eventsAcquired = Acquire(pfreq); |
||
352 | stop_timer(); |
||
353 | if (!daq_on) break; |
||
354 | if (timeout) { |
||
355 | i--; |
||
356 | printf("INFO Timeout\n"); |
||
357 | continue; |
||
358 | } |
||
359 | int isvalid =1; |
||
360 | if (eventsAcquired < 1) { |
||
361 | printf("Acquired events %d\n", eventsAcquired); |
||
362 | isvalid = 0; |
||
363 | } else { |
||
364 | AitFillHistograms(eventsAcquired); |
||
365 | if (verbose) ShowEvent(1); |
||
366 | } |
||
367 | |||
368 | int nb = ( isvalid && fp ) ? AitToFile( fp , pfreq ) : 0; |
||
369 | SetCtrlVal(ait,AIT_CEVE,i); |
||
370 | if (!isvalid) i--; |
||
371 | |||
372 | time(&t); |
||
373 | if (t!=told ) { |
||
374 | rate = (i-neveold); |
||
375 | printf("%d events in %2.2f min (%d s) Rate %f Hz %s ",i+1, (double)(t-tstart)/60.,(t-tstart), rate , ctime(&t)); |
||
376 | neveold = i; |
||
377 | RefreshGraphs (ait, 0 , EVENT_COMMIT ,NULL, 0, 0 ); |
||
378 | double daqBlocksize = GetAdcEventSize() * pfreq * 2.0; |
||
379 | double daqTimeMs = BlockTransferTime(); |
||
380 | double daqTransferRate = daqBlocksize / daqTimeMs; |
||
381 | SetCtrlVal(ait,AIT_DAQBLOCKSIZE, daqBlocksize); |
||
382 | SetCtrlVal(ait,AIT_DAQTIMEMS,daqTimeMs); |
||
383 | SetCtrlVal(ait,AIT_DAQTRANSFERRATE,daqTransferRate); |
||
384 | } |
||
385 | told=t; |
||
386 | |||
387 | |||
388 | |||
389 | } |
||
390 | time(&t); |
||
391 | printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t)); |
||
392 | |||
393 | } |
||
394 | |||
395 | if (fp) fclose(fp); |
||
396 | |||
397 | |||
398 | RefreshGraphs (ait, 0 , EVENT_TIMER_TICK ,NULL, 0, 0 ); |
||
399 | return 0; |
||
400 | |||
401 | } |
||
402 | |||
403 | |||
404 | int CVICALLBACK scan(void *functionData) { |
||
405 | |||
406 | int dx[3]={0,0,0}; |
||
407 | int nx[3]={0,0,0}; |
||
408 | int x0[3]={0,0,0}; |
||
409 | int ix[3]={0,0,0}; |
||
410 | int idx[3]={0,0,0}; |
||
411 | unsigned short size; |
||
412 | unsigned short posrec={0x2}; |
||
413 | unsigned short runbuf={0x3}; |
||
414 | |||
415 | int n[3]; |
||
416 | char filename[0xFF]; |
||
417 | char fname[0xff]; |
||
418 | char pname[0xff]; |
||
419 | int enabledoutput; |
||
420 | |||
421 | FILE *fp; |
||
422 | GetCtrlVal(ait, AIT_FILENAME, fname ); |
||
423 | GetCtrlVal(ait, AIT_PATHNAME, pname ); |
||
424 | sprintf(filename,"%s/%s", pname, fname); |
||
425 | |||
426 | GetCtrlVal(ait,AIT_ENABLEDOUTPUT, &enabledoutput); |
||
427 | int fsize; |
||
428 | if ( GetFileInfo(filename,&fsize) ) { |
||
429 | MessagePopup ("Warning", "File exist. Remove it first or choose another file"); |
||
430 | return 0; |
||
431 | } |
||
432 | GetCtrlVal(xyscan, SCAN_STEPX, &dx[0]); |
||
433 | GetCtrlVal(xyscan, SCAN_STEPY, &dx[1]); |
||
434 | GetCtrlVal(xyscan, SCAN_NSTEPSX, &nx[0]); |
||
435 | GetCtrlVal(xyscan, SCAN_NSTEPSY, &nx[1]); |
||
436 | GetCtrlVal(xyscan, SCAN_STARTX, &x0[0]); |
||
437 | GetCtrlVal(xyscan, SCAN_STARTY, &x0[1]); |
||
438 | |||
439 | |||
440 | if (enabledoutput) { |
||
441 | fp = fopen(filename,"ab"); |
||
442 | if (fp) { |
||
443 | size=36; |
||
444 | fwrite(&runbuf, sizeof(unsigned short),1 ,fp); |
||
445 | fwrite(&size , sizeof(unsigned short),1 ,fp); |
||
446 | fwrite(x0 , 1,4*3 ,fp); |
||
447 | fwrite(dx , 1,4*3 ,fp); |
||
448 | fwrite(nx , 1,4*3 ,fp); |
||
449 | fclose(fp); |
||
450 | } |
||
451 | } |
||
452 | |||
453 | for (int j=0; j<nx[1]; j++) { |
||
454 | |||
455 | SetCtrlVal (xyscan, SCAN_IY, j); |
||
456 | ix[1]= x0[1]+j*dx[1]; |
||
457 | #ifdef MIKRO |
||
458 | MIKRO_MoveTo(2,ix[1]); |
||
459 | MIKRO_GetPosition(2,&n[1]); |
||
460 | SetCtrlVal (xyscan, SCAN_YP, n[1]); |
||
461 | #endif |
||
462 | |||
463 | for (int i=0; i<nx[0]; i++) { |
||
464 | SetCtrlVal (xyscan, SCAN_IX, i); |
||
465 | ix[0]= x0[0]+i*dx[0]; |
||
466 | #ifdef MIKRO |
||
467 | MIKRO_MoveTo(1,ix[0]); |
||
468 | MIKRO_GetPosition(1,&n[0]); |
||
469 | SetCtrlVal (xyscan, SCAN_XP, n[0]); |
||
470 | |||
471 | |||
472 | if (enabledoutput) { |
||
473 | fp = fopen(filename,"ab"); |
||
474 | if (fp) { |
||
475 | time_t mtime; |
||
476 | idx[0]=i; |
||
477 | idx[1]=j; |
||
478 | size=24+sizeof(mtime); |
||
479 | fwrite(&posrec, sizeof(unsigned short),1 ,fp); |
||
480 | fwrite(&size , sizeof(unsigned short),1 ,fp); |
||
481 | fwrite(n , 1,4*3 ,fp); |
||
482 | fwrite(idx , 1,4*3 ,fp); |
||
483 | time(&mtime); |
||
484 | fwrite(&mtime , sizeof(time_t),1 ,fp); |
||
485 | fclose(fp); |
||
486 | } |
||
487 | } |
||
488 | |||
489 | #endif |
||
490 | int newfile=0; |
||
491 | daq(&newfile); |
||
492 | if (!daq_on) break; |
||
493 | } |
||
494 | if (!daq_on) break; |
||
495 | } |
||
496 | |||
497 | return 0; |
||
498 | } |
||
499 | |||
500 | |||
501 | int main (int argc, char *argv[]) { |
||
502 | short port; |
||
503 | if (InitCVIRTE (0, argv, 0) == 0) |
||
504 | return -1; /* out of memory */ |
||
505 | if ((ait = LoadPanel (0, "AitGui.uir", AIT)) < 0) |
||
506 | return -1; |
||
507 | if ((xyscan = LoadPanel (0, "XYSCAN.uir", SCAN)) < 0) |
||
508 | return -1; |
||
509 | SetStdioPort (CVI_STDIO_WINDOW); |
||
510 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
511 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
512 | DisplayPanel (ait); |
||
513 | DisplayPanel (xyscan); |
||
514 | AitInit(); |
||
515 | |||
516 | |||
517 | #ifdef MIKRO |
||
518 | GetCtrlVal(xyscan, SCAN_PORT, &port); |
||
519 | if (MIKRO_Open (port)) MessagePopup ("Error", "Mikro Port Not found !\n Change in the GUI") ; |
||
520 | MIKRO_Init(1,0); |
||
521 | MIKRO_Init(2,0); |
||
522 | #endif |
||
523 | |||
524 | |||
525 | RunUserInterface (); |
||
526 | |||
527 | #ifdef MIKRO |
||
528 | MIKRO_Close (); |
||
529 | #endif |
||
530 | DiscardPanel (ait); |
||
531 | DiscardPanel (xyscan); |
||
532 | return 0; |
||
533 | } |
||
534 | |||
535 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
536 | void *callbackData, int eventData1, int eventData2) { |
||
537 | switch (event) { |
||
538 | case EVENT_COMMIT: |
||
539 | daq_on=0; |
||
540 | break; |
||
541 | } |
||
542 | return 0; |
||
543 | } |
||
544 | |||
545 | |||
546 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
547 | void *callbackData, int eventData1, int eventData2) { |
||
548 | ThreadFunctionPtr mythread = NULL; |
||
549 | switch (event) { |
||
550 | |||
551 | case EVENT_COMMIT: |
||
552 | |||
553 | controlID= control; |
||
554 | if (panel == ait && control == AIT_START) { |
||
555 | mythread = daq; |
||
556 | |||
557 | } |
||
558 | if (panel == xyscan && control == SCAN_SCAN) mythread = scan; |
||
559 | if (mythread!=NULL) { |
||
560 | printf("New Thread panel=%d button=%d\n", panel, control); |
||
561 | |||
562 | // SetDimming(1); |
||
563 | |||
564 | |||
565 | CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID, |
||
566 | DEFAULT_THREAD_PRIORITY, |
||
567 | EndOfThread, |
||
568 | EVENT_TP_THREAD_FUNCTION_END, |
||
569 | NULL, RUN_IN_SCHEDULED_THREAD, |
||
570 | &tfID); |
||
571 | } |
||
572 | break; |
||
573 | } |
||
574 | return 0; |
||
575 | } |
||
576 | |||
577 | |||
578 | |||
579 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
580 | void *callbackData, int eventData1, int eventData2) { |
||
581 | switch (event) { |
||
582 | case EVENT_COMMIT: |
||
583 | QuitUserInterface (0); |
||
584 | break; |
||
585 | } |
||
586 | return 0; |
||
587 | } |
||
588 | |||
589 | int CVICALLBACK ReadCB (int panel, int control, int event, |
||
590 | void *callbackData, int eventData1, int eventData2) { |
||
591 | switch (event) { |
||
592 | case EVENT_COMMIT: |
||
593 | |||
594 | |||
595 | |||
596 | ReadRegisters(); |
||
597 | break; |
||
598 | } |
||
599 | return 0; |
||
600 | } |
||
601 | |||
602 | int CVICALLBACK ChangeChannelCB (int panel, int control, int event, |
||
603 | void *callbackData, int eventData1, int eventData2) { |
||
604 | int ch=0; |
||
605 | switch (event) { |
||
606 | case EVENT_COMMIT: |
||
607 | |||
608 | int logy=0; |
||
609 | GetCtrlVal(ait,AIT_CH, &ch); |
||
610 | GetCtrlVal(ait,AIT_LOGY, &logy); |
||
611 | if (logy){ |
||
612 | SetCtrlAttribute (ait, AIT_GRAPH, ATTR_YMAP_MODE, VAL_LOG); |
||
613 | SetCtrlAttribute (ait, AIT_GRAPHSUM, ATTR_YMAP_MODE, VAL_LOG); |
||
614 | |||
615 | } else { |
||
616 | SetCtrlAttribute (ait, AIT_GRAPH, ATTR_YMAP_MODE, VAL_LINEAR); |
||
617 | SetCtrlAttribute (ait, AIT_GRAPHSUM, ATTR_YMAP_MODE, VAL_LINEAR); |
||
618 | |||
619 | } |
||
620 | H1D_Draw(ch,ait,AIT_GRAPH,&plothandle[0]); |
||
621 | break; |
||
622 | } |
||
623 | return 0; |
||
624 | } |
||
625 | |||
626 | |||
627 | |||
628 | |||
629 | |||
630 | int CVICALLBACK InitCB (int panel, int control, int event, |
||
631 | void *callbackData, int eventData1, int eventData2) { |
||
632 | switch (event) { |
||
633 | case EVENT_COMMIT: |
||
634 | AitInit(); |
||
635 | break; |
||
636 | } |
||
637 | return 0; |
||
638 | } |
||
639 | |||
640 | int CVICALLBACK QuickCB (int panel, int control, int event, |
||
641 | void *callbackData, int eventData1, int eventData2) { |
||
642 | switch (event) { |
||
643 | case EVENT_COMMIT: |
||
644 | switch (control) { |
||
645 | case AIT_QUICKSETUP_1: |
||
646 | QuickSetupContinuous(); |
||
647 | SetCtrlVal(ait,AIT_TRGTYPE, 0); |
||
648 | break; |
||
649 | case AIT_QUICKSETUP_2: |
||
650 | QuickSetupDiscriminator(); |
||
651 | SetCtrlVal(ait,AIT_TRGTYPE, 2); |
||
652 | break; |
||
653 | case AIT_QUICKSETUP_3: |
||
654 | QuickSetupTriggerOff(); |
||
655 | break; |
||
656 | |||
657 | |||
658 | } |
||
659 | float bias = GetHvVoltage(ADDR_HV_VMON); |
||
660 | SetCtrlVal (panel, AIT_BIASMON, bias); |
||
661 | double value = GetIntegrationTime(); |
||
662 | SetCtrlVal (panel, AIT_GETADCINTEGRATIONTIME, value); |
||
663 | value = GetThreshold(); |
||
664 | SetCtrlVal (panel, AIT_GETADCTHRESHOLD, value); |
||
665 | int csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
666 | |||
667 | int ivalue = ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN2) ? 1 : 0) << 1) | |
||
668 | ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN1) ? 1 : 0) << 0); |
||
669 | SetCtrlVal (panel, AIT_GETADCGAIN, ivalue); |
||
670 | ivalue = |
||
671 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN4) ? 1 : 0) << 3) | |
||
672 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN3) ? 1 : 0) << 2) | |
||
673 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN2) ? 1 : 0) << 1) | |
||
674 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN1) ? 1 : 0) << 0); |
||
675 | SetCtrlVal (panel, AIT_GETADCSUMGAIN, ivalue); |
||
676 | break; |
||
677 | } |
||
678 | return 0; |
||
679 | } |
||
680 | |||
681 | |||
682 | |||
683 | int CVICALLBACK MoveStageCB (int panel, int control, int event, |
||
684 | void *callbackData, int eventData1, int eventData2) { |
||
685 | int axis=0, step=1000, direction=1; |
||
686 | switch (event) { |
||
687 | case EVENT_COMMIT: |
||
688 | |||
689 | if (panel == xyscan) { |
||
690 | switch (control) { |
||
691 | case SCAN_BR : |
||
692 | axis = 1; |
||
693 | direction = 1; |
||
694 | GetCtrlVal(panel, SCAN_STEPX, &step); |
||
695 | break; |
||
696 | case SCAN_BL : |
||
697 | axis = 1; |
||
698 | direction = -1; |
||
699 | GetCtrlVal(panel, SCAN_STEPX, &step); |
||
700 | break; |
||
701 | case SCAN_BU : |
||
702 | axis = 2; |
||
703 | direction = 1; |
||
704 | GetCtrlVal(panel, SCAN_STEPY, &step); |
||
705 | break; |
||
706 | case SCAN_BD : |
||
707 | axis = 2; |
||
708 | direction = -1; |
||
709 | GetCtrlVal(panel, SCAN_STEPY, &step); |
||
710 | break; |
||
711 | } |
||
712 | #ifdef MIKRO |
||
713 | { |
||
714 | int n=0; |
||
715 | MIKRO_MoveFor(axis, direction*step ); |
||
716 | MIKRO_GetPosition(axis,&n); |
||
717 | if (axis == 1) SetCtrlVal (panel, SCAN_XP, n); |
||
718 | if (axis == 2) SetCtrlVal (panel, SCAN_YP, n); |
||
719 | } |
||
720 | #endif // MIKRO |
||
721 | } |
||
722 | |||
723 | break; |
||
724 | } |
||
725 | return 0; |
||
726 | } |
||
727 | |||
728 | |||
729 | |||
730 | |||
731 | |||
732 | int CVICALLBACK GoXCB (int panel, int control, int event, |
||
733 | void *callbackData, int eventData1, int eventData2) { |
||
734 | int n2; |
||
735 | switch (event) { |
||
736 | case EVENT_COMMIT: |
||
737 | GetCtrlVal (panel, SCAN_XG, &n2); |
||
738 | #ifdef MIKRO |
||
739 | MIKRO_MoveTo(1,n2); |
||
740 | MIKRO_GetPosition(1,&n2); |
||
741 | |||
742 | #endif |
||
743 | SetCtrlVal (panel, SCAN_XP, n2); |
||
744 | break; |
||
745 | } |
||
746 | return 0; |
||
747 | } |
||
748 | |||
749 | int CVICALLBACK GoYCB (int panel, int control, int event, |
||
750 | void *callbackData, int eventData1, int eventData2) { |
||
751 | int n2; |
||
752 | switch (event) { |
||
753 | case EVENT_COMMIT: |
||
754 | GetCtrlVal (panel, SCAN_YG, &n2); |
||
755 | #ifdef MIKRO |
||
756 | MIKRO_MoveTo(2,n2); |
||
757 | MIKRO_GetPosition(2,&n2); |
||
758 | |||
759 | #endif |
||
760 | SetCtrlVal (panel, SCAN_YP, n2); |
||
761 | break; |
||
762 | } |
||
763 | return 0; |
||
764 | } |
||
765 | |||
766 | int CVICALLBACK GetCurrentPositionCB (int panel, int control, int event, |
||
767 | void *callbackData, int eventData1, int eventData2) { |
||
768 | |||
769 | |||
770 | switch (event) { |
||
771 | case EVENT_COMMIT: { |
||
772 | #ifdef MIKRO |
||
773 | int n[2]; |
||
774 | MIKRO_GetPosition(1,&n[0]); |
||
775 | SetCtrlVal (panel, SCAN_XP, n[0]); |
||
776 | MIKRO_GetPosition(2,&n[1]); |
||
777 | SetCtrlVal (panel, SCAN_YP, n[1]); |
||
778 | #endif |
||
779 | break; |
||
780 | } |
||
781 | } |
||
782 | return 0; |
||
783 | } |
||
784 | |||
785 | int CVICALLBACK HomeCB (int panel, int control, int event, |
||
786 | void *callbackData, int eventData1, int eventData2) { |
||
787 | switch (event) { |
||
788 | case EVENT_COMMIT: |
||
789 | #ifdef MIKRO |
||
790 | MIKRO_ReferenceMove(1); |
||
791 | MIKRO_ReferenceMove(2); |
||
792 | GetCurrentPositionCB(panel, control, event, NULL, 0, 0); |
||
793 | #endif |
||
794 | break; |
||
795 | } |
||
796 | return 0; |
||
797 | } |
||
798 | |||
799 | int CVICALLBACK SetBiasCB (int panel, int control, int event, |
||
800 | void *callbackData, int eventData1, int eventData2) { |
||
801 | double bias=0; |
||
802 | switch (event) { |
||
803 | case EVENT_COMMIT: |
||
804 | GetCtrlVal (panel, AIT_BIAS, &bias); |
||
805 | SetHvVoltage(ADDR_HV_CTRL, bias); |
||
806 | bias = GetHvVoltage(ADDR_HV_VMON); |
||
807 | SetCtrlVal (panel, AIT_BIASMON, bias); |
||
808 | break; |
||
809 | } |
||
810 | return 0; |
||
811 | } |
||
812 | |||
813 | int CVICALLBACK SetHvOnCB (int panel, int control, int event, |
||
814 | void *callbackData, int eventData1, int eventData2) { |
||
815 | int state=0; |
||
816 | double bias; |
||
817 | switch (event) { |
||
818 | case EVENT_COMMIT: |
||
819 | GetCtrlVal (panel, control, &state); |
||
820 | if (state) SetHvOn(true); |
||
821 | else SetHvOn(false); |
||
822 | bias = GetHvVoltage(ADDR_HV_VMON); |
||
823 | SetCtrlVal (panel, AIT_BIASMON, bias); |
||
824 | break; |
||
825 | } |
||
826 | return 0; |
||
827 | } |
||
828 | |||
829 | int CVICALLBACK SetAdcIntegrationTimeCB (int panel, int control, int event, |
||
830 | void *callbackData, int eventData1, int eventData2) { |
||
831 | |||
832 | switch (event) { |
||
833 | case EVENT_COMMIT: { |
||
834 | double value; |
||
835 | GetCtrlVal (panel, AIT_ADCINTEGRATIONTIME, &value); |
||
836 | SetIntegrationTime(value); |
||
837 | value = GetIntegrationTime(); |
||
838 | SetCtrlVal (panel, AIT_GETADCINTEGRATIONTIME, value); |
||
839 | } |
||
840 | break; |
||
841 | } |
||
842 | return 0; |
||
843 | } |
||
844 | |||
845 | int CVICALLBACK SetAdcThresholdCB (int panel, int control, int event, |
||
846 | void *callbackData, int eventData1, int eventData2) { |
||
847 | switch (event) { |
||
848 | case EVENT_COMMIT: { |
||
849 | double value; |
||
850 | GetCtrlVal (panel, AIT_ADCTHRESHOLD, &value); |
||
851 | SetThreshold(value); |
||
852 | value = GetThreshold(); |
||
853 | SetCtrlVal (panel, AIT_GETADCTHRESHOLD, value); |
||
854 | } |
||
855 | break; |
||
856 | } |
||
857 | return 0; |
||
858 | } |
||
859 | |||
860 | int CVICALLBACK SetAdcGainCB (int panel, int control, int event, |
||
861 | void *callbackData, int eventData1, int eventData2) { |
||
862 | switch (event) { |
||
863 | case EVENT_COMMIT: { |
||
864 | int value; |
||
865 | GetCtrlVal (panel, AIT_ADCGAIN, &value); |
||
866 | SetAdcGain(value); |
||
867 | int csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
868 | |||
869 | value = ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN2) ? 1 : 0) << 1) | |
||
870 | ((GetBit(csrDetector, BIT_CSR_DET_ADCGAIN1) ? 1 : 0) << 0); |
||
871 | SetCtrlVal (panel, AIT_GETADCGAIN, value); |
||
872 | } |
||
873 | break; |
||
874 | } |
||
875 | return 0; |
||
876 | } |
||
877 | |||
878 | int CVICALLBACK SetAdcSumGainCB (int panel, int control, int event, |
||
879 | void *callbackData, int eventData1, int eventData2) { |
||
880 | switch (event) { |
||
881 | case EVENT_COMMIT:{ |
||
882 | int value; |
||
883 | GetCtrlVal (panel, AIT_ADCSUMGAIN, &value); |
||
884 | SetSumGain(value); |
||
885 | int csrDetector = ReadSD4Register(ADDR_CSR_DETECTOR); |
||
886 | value = |
||
887 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN4) ? 1 : 0) << 3) | |
||
888 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN3) ? 1 : 0) << 2) | |
||
889 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN2) ? 1 : 0) << 1) | |
||
890 | ((GetBit(csrDetector, BIT_CSR_DET_SUMGAIN1) ? 1 : 0) << 0); |
||
891 | SetCtrlVal (panel, AIT_GETADCSUMGAIN, value); |
||
892 | } |
||
893 | |||
894 | break; |
||
895 | } |
||
896 | return 0; |
||
897 | } |
||
898 | |||
899 | int CVICALLBACK SetDiscriminatorTypeCB (int panel, int control, int event, |
||
900 | void *callbackData, int eventData1, int eventData2) { |
||
901 | switch (event) { |
||
902 | case EVENT_COMMIT:{ |
||
903 | int trgtype; |
||
904 | GetCtrlVal(ait,AIT_TRGTYPE, &trgtype); |
||
905 | EnableTrigger(trgtype); |
||
906 | } |
||
907 | break; |
||
908 | } |
||
909 | return 0; |
||
910 | } |
||
911 | |||
912 | int CVICALLBACK ResetCB (int panel, int control, int event, |
||
913 | void *callbackData, int eventData1, int eventData2) { |
||
914 | switch (event) { |
||
915 | case EVENT_COMMIT: |
||
916 | ReconfigureDevice() ; |
||
917 | ReconnectDevice(); |
||
918 | //ResetDevice() ; |
||
919 | break; |
||
920 | } |
||
921 | return 0; |
||
922 | } |
||
923 | |||
924 | int CVICALLBACK OpenScanCB (int panel, int control, int event, |
||
925 | void *callbackData, int eventData1, int eventData2) { |
||
926 | switch (event) { |
||
927 | case EVENT_COMMIT: |
||
928 | DisplayPanel (xyscan); |
||
929 | break; |
||
930 | } |
||
931 | return 0; |
||
932 | } |
||
933 | |||
934 | int CVICALLBACK OpenMainCB (int panel, int control, int event, |
||
935 | void *callbackData, int eventData1, int eventData2) { |
||
936 | switch (event) { |
||
937 | case EVENT_COMMIT: |
||
938 | DisplayPanel (ait); |
||
939 | break; |
||
940 | } |
||
941 | return 0; |
||
942 | } |