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