Rev 251 | Rev 253 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
225 | f9daq | 1 | #include "redpitaya_gui.h" |
2 | #include <ansi_c.h> |
||
3 | #include <tcpsupp.h> |
||
4 | #include <utility.h> |
||
5 | #include <cvirte.h> |
||
6 | #include <userint.h> |
||
7 | #include "redpitaya_gui.h" |
||
8 | |||
229 | f9daq | 9 | #include "H1D.h" |
10 | |||
11 | |||
225 | f9daq | 12 | #define NBEFORE 150 |
13 | #define MAXSAMPLES 16384 |
||
14 | #define MINTRGDELAY -8192 |
||
15 | |||
16 | |||
17 | static int panelHandle; |
||
18 | |||
19 | static unsigned int chandle = 0; |
||
245 | f9daq | 20 | //static int tfID; |
225 | f9daq | 21 | int pfreq; |
22 | static int plothandle[2]= {0,0}; |
||
242 | f9daq | 23 | static int tdcplothandle[2]= {0,0}; |
24 | static int adcplothandle[2]= {0,0}; |
||
225 | f9daq | 25 | int debug ; |
26 | int initialized = 0; |
||
27 | #define MAX_THREADS 10 |
||
28 | int nsamples=0; |
||
242 | f9daq | 29 | int adctype; |
30 | float athreshold, twin0,twin1; |
||
31 | float timebins[0XFFFF]; |
||
233 | f9daq | 32 | |
33 | FILE *foutput; |
||
245 | f9daq | 34 | int outwaveforms=0; |
233 | f9daq | 35 | |
225 | f9daq | 36 | static CmtThreadPoolHandle poolHandle = 0; |
37 | int ctrl_c=0; |
||
38 | |||
39 | |||
40 | int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData); |
||
41 | |||
42 | |||
229 | f9daq | 43 | |
44 | static int export_data (int hid) { |
||
242 | f9daq | 45 | |
229 | f9daq | 46 | char filename[0xFF],rootcmd[0xFF]; |
243 | f9daq | 47 | char fname[0xFF]; |
229 | f9daq | 48 | int type=0; |
242 | f9daq | 49 | |
243 | f9daq | 50 | GetCtrlVal(panelHandle,PANEL_EXPORTNAME,fname); |
229 | f9daq | 51 | GetCtrlVal(panelHandle,PANEL_FILETYPE,&type); |
242 | f9daq | 52 | |
229 | f9daq | 53 | FILE *fp; |
242 | f9daq | 54 | |
229 | f9daq | 55 | switch (type) { |
56 | case 0: |
||
57 | case 1: |
||
243 | f9daq | 58 | sprintf(filename,"%s_%d.root",fname, hid); |
229 | f9daq | 59 | fp =fopen(filename,"wb"); |
60 | if (fp) { |
||
61 | H1D_Write2File(hid,fp); |
||
62 | fclose(fp); |
||
63 | if (type) { |
||
64 | sprintf(rootcmd ,"thisroot.bat && root.exe H1Dload.cxx(\\\"%s\\\")", filename); |
||
65 | LaunchExecutable(rootcmd); |
||
66 | |||
67 | } |
||
243 | f9daq | 68 | printf("Histogram %d exported to %s\n", hid, filename); |
229 | f9daq | 69 | } |
70 | break; |
||
71 | case 2: |
||
243 | f9daq | 72 | sprintf(filename,"%s_%d.txt",fname, hid); |
229 | f9daq | 73 | fp=fopen(filename,"w"); |
74 | if (fp) { |
||
243 | f9daq | 75 | for (int i=0; i<H1D_GetNbinsX(hid); i++) fprintf(fp,"%g\t%g\n", H1D_GetXBinCenter(hid,i), H1D_GetBinContent(hid,i) ); |
229 | f9daq | 76 | fclose(fp); |
243 | f9daq | 77 | printf("Histogram %d exported to %s\n", hid, filename); |
229 | f9daq | 78 | } |
243 | f9daq | 79 | |
229 | f9daq | 80 | break; |
81 | |||
82 | } |
||
83 | |||
84 | |||
85 | return (0); |
||
86 | } |
||
87 | |||
88 | |||
225 | f9daq | 89 | int main (int argc, char *argv[]) { |
90 | if (InitCVIRTE (0, argv, 0) == 0) |
||
91 | return -1; /* out of memory */ |
||
92 | if ((panelHandle = LoadPanel (0, "redpitaya_gui.uir", PANEL)) < 0) |
||
93 | return -1; |
||
94 | |||
242 | f9daq | 95 | |
225 | f9daq | 96 | SetStdioPort (CVI_STDIO_WINDOW); |
97 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
98 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
242 | f9daq | 99 | |
229 | f9daq | 100 | //for (int i=0;i<1000;i++) H1D_Fill(1,i,i); |
242 | f9daq | 101 | //H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]); |
229 | f9daq | 102 | printf("size of double = %d\n",sizeof(double)); |
225 | f9daq | 103 | DisplayPanel (panelHandle); |
104 | RunUserInterface (); |
||
105 | DiscardPanel (panelHandle); |
||
106 | CmtDiscardThreadPool (poolHandle); |
||
107 | if (chandle!=0) DisconnectFromTCPServer (chandle); |
||
108 | return 0; |
||
109 | } |
||
110 | |||
111 | char strbuf[0xFF]; |
||
112 | |||
113 | int gLog=0; |
||
114 | |||
115 | int printf(const char *format, ...) { |
||
116 | va_list aptr; |
||
117 | int ret; |
||
118 | FILE *flog; |
||
119 | |||
120 | va_start(aptr, format); |
||
121 | ret = vsprintf(strbuf, format, aptr); |
||
122 | va_end(aptr); |
||
123 | SetCtrlVal(panelHandle,PANEL_STDIO,strbuf); |
||
124 | |||
125 | if (gLog) { |
||
126 | flog = fopen ("stdio.log", "a"); |
||
127 | fprintf (flog, "%s", strbuf); |
||
128 | fclose (flog); |
||
129 | } |
||
130 | return(ret); |
||
131 | } |
||
132 | |||
252 | f9daq | 133 | |
134 | int CVICALLBACK SetGraphLogYCB (int panel, int control, int event, |
||
135 | void *callbackData, int eventData1, int eventData2) |
||
136 | { |
||
137 | |||
138 | int cid=0; |
||
139 | int logy=0; |
||
140 | switch (event) |
||
141 | { |
||
142 | case EVENT_COMMIT: |
||
143 | GetCtrlVal(panel,control, &logy); |
||
144 | switch (control){ |
||
145 | case PANEL_LOGY_1: cid = PANEL_TDC1; break; |
||
146 | case PANEL_LOGY_2: cid = PANEL_ADC1; break; |
||
147 | case PANEL_LOGY_3: cid = PANEL_TDC2; break; |
||
148 | case PANEL_LOGY_4: cid = PANEL_ADC2; break; |
||
149 | } |
||
150 | if (logy) SetCtrlAttribute (panel, cid, ATTR_YMAP_MODE, VAL_LOG); |
||
151 | else SetCtrlAttribute (panel, cid, ATTR_YMAP_MODE, VAL_LINEAR); |
||
152 | break; |
||
153 | } |
||
154 | return 0; |
||
155 | } |
||
156 | |||
157 | int CVICALLBACK SetGraphPropertiesCB (int panel, int control, int event, |
||
158 | void *callbackData, int eventData1, int eventData2) |
||
159 | { |
||
160 | |||
161 | float min, max; |
||
162 | int autoscale; |
||
163 | switch (event) |
||
164 | { |
||
165 | case EVENT_COMMIT: |
||
166 | GetCtrlVal(panelHandle,PANEL_MINX_5, &min); |
||
167 | GetCtrlVal(panelHandle,PANEL_MAXX_5, &max); |
||
168 | GetCtrlVal(panelHandle,PANEL_AUTOY, &autoscale); |
||
169 | if (autoscale) |
||
170 | SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_LEFT_YAXIS, VAL_AUTOSCALE, min, max); |
||
171 | else |
||
172 | SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_LEFT_YAXIS, VAL_MANUAL, min, max); |
||
173 | |||
174 | GetCtrlVal(panelHandle,PANEL_MINX_6, &min); |
||
175 | GetCtrlVal(panelHandle,PANEL_MAXX_6, &max); |
||
176 | GetCtrlVal(panelHandle,PANEL_AUTOX, &autoscale); |
||
177 | if (autoscale) |
||
178 | SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_BOTTOM_XAXIS, VAL_AUTOSCALE, min, max); |
||
179 | else |
||
180 | SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_BOTTOM_XAXIS, VAL_MANUAL, min, max); |
||
181 | |||
182 | |||
183 | break; |
||
184 | } |
||
185 | return 0; |
||
186 | } |
||
187 | |||
188 | |||
233 | f9daq | 189 | int histoinit() { |
190 | int nch; |
||
191 | float min,max; |
||
242 | f9daq | 192 | |
233 | f9daq | 193 | GetCtrlVal(panelHandle,PANEL_NCH_1, &nch); |
194 | GetCtrlVal(panelHandle,PANEL_MINX_1, &min); |
||
195 | GetCtrlVal(panelHandle,PANEL_MAXX_1, &max); |
||
225 | f9daq | 196 | |
233 | f9daq | 197 | H1D_Init(1, "ADC ch 1","Pulse height", nch, min, max ); |
241 | f9daq | 198 | H1D_SetTitleX(1,"ADC (V)"); |
233 | f9daq | 199 | H1D_SetTitleY(1,"N"); |
229 | f9daq | 200 | |
233 | f9daq | 201 | GetCtrlVal(panelHandle,PANEL_NCH_2, &nch); |
202 | GetCtrlVal(panelHandle,PANEL_MINX_2, &min); |
||
203 | GetCtrlVal(panelHandle,PANEL_MAXX_2, &max); |
||
204 | |||
205 | H1D_Init(2, "ADC ch 2","Pulse height", nch, min, max ); |
||
241 | f9daq | 206 | H1D_SetTitleX(2,"ADC (V)"); |
233 | f9daq | 207 | H1D_SetTitleY(2,"N"); |
208 | |||
209 | GetCtrlVal(panelHandle,PANEL_NCH_3, &nch); |
||
210 | GetCtrlVal(panelHandle,PANEL_MINX_3, &min); |
||
211 | GetCtrlVal(panelHandle,PANEL_MAXX_3, &max); |
||
212 | |||
213 | H1D_Init(3, "TDC ch 1","TDC", nch, min, max ); |
||
241 | f9daq | 214 | H1D_SetTitleX(3,"TDC (us)"); |
233 | f9daq | 215 | H1D_SetTitleY(3,"N"); |
216 | |||
217 | GetCtrlVal(panelHandle,PANEL_NCH_4, &nch); |
||
218 | GetCtrlVal(panelHandle,PANEL_MINX_4, &min); |
||
219 | GetCtrlVal(panelHandle,PANEL_MAXX_4, &max); |
||
220 | |||
221 | H1D_Init(4, "TDC ch 2","TDC", nch, min, max ); |
||
241 | f9daq | 222 | H1D_SetTitleX(4,"TDC (us)"); |
233 | f9daq | 223 | H1D_SetTitleY(4,"N"); |
242 | f9daq | 224 | |
241 | f9daq | 225 | SetCtrlAttribute (panelHandle, PANEL_ADC1, ATTR_XNAME, H1D_GetTitleX(1) ); |
226 | SetCtrlAttribute (panelHandle, PANEL_ADC1, ATTR_YNAME, H1D_GetTitleY(1) ); |
||
227 | SetCtrlAttribute (panelHandle, PANEL_ADC2, ATTR_XNAME, H1D_GetTitleX(2) ); |
||
228 | SetCtrlAttribute (panelHandle, PANEL_ADC2, ATTR_YNAME, H1D_GetTitleY(2) ); |
||
242 | f9daq | 229 | |
241 | f9daq | 230 | SetCtrlAttribute (panelHandle, PANEL_TDC1, ATTR_XNAME, H1D_GetTitleX(3) ); |
231 | SetCtrlAttribute (panelHandle, PANEL_TDC1, ATTR_YNAME, H1D_GetTitleY(3) ); |
||
232 | SetCtrlAttribute (panelHandle, PANEL_TDC2, ATTR_XNAME, H1D_GetTitleX(4) ); |
||
233 | SetCtrlAttribute (panelHandle, PANEL_TDC2, ATTR_YNAME, H1D_GetTitleY(4) ); |
||
252 | f9daq | 234 | SetGraphLogYCB( panelHandle, PANEL_LOGY_1, EVENT_COMMIT,NULL,0,0); |
235 | SetGraphLogYCB( panelHandle, PANEL_LOGY_2, EVENT_COMMIT,NULL,0,0); |
||
236 | SetGraphLogYCB( panelHandle, PANEL_LOGY_3, EVENT_COMMIT,NULL,0,0); |
||
237 | SetGraphLogYCB( panelHandle, PANEL_LOGY_4, EVENT_COMMIT,NULL,0,0); |
||
238 | |||
239 | SetGraphPropertiesCB( panelHandle, PANEL, EVENT_COMMIT,NULL,0,0); |
||
240 | |||
242 | f9daq | 241 | SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_LABEL_TEXT , "sampling adc data"); |
241 | f9daq | 242 | SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_XNAME, "t(us)" ); |
243 | SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_YNAME, "U(V)" ); |
||
242 | f9daq | 244 | |
245 | GetCtrlVal(panelHandle,PANEL_TWIN0, &twin0); |
||
246 | GetCtrlVal(panelHandle,PANEL_TWIN1, &twin1); |
||
247 | GetCtrlVal(panelHandle,PANEL_ADCTYPE, &adctype); |
||
248 | GetCtrlVal(panelHandle,PANEL_ITRGLEVEL , &athreshold); |
||
233 | f9daq | 249 | return 0; |
250 | } |
||
225 | f9daq | 251 | |
245 | f9daq | 252 | int analyse(int nb, unsigned char *cdata, int *info, int *ninfo) { |
242 | f9daq | 253 | |
254 | int *ibuf = (int *)cdata; |
||
233 | f9daq | 255 | float *fbuf = (float *)cdata; |
245 | f9daq | 256 | float *finfo = (float *)info; |
233 | f9daq | 257 | int neve=0; |
258 | int *data = (ibuf+3); |
||
259 | int nr=0; |
||
242 | f9daq | 260 | static float adc = 10000; |
261 | static float qdc = 0; |
||
245 | f9daq | 262 | *ninfo = 0; |
242 | f9daq | 263 | printf("Run HDR LEN=%d NEVE=%d dt=%f adc=%f qdc=%f\n", ibuf[0],ibuf[1],fbuf[2],adc,qdc); |
233 | f9daq | 264 | while (nr<nb) { |
265 | |||
225 | f9daq | 266 | int recid = *data++; |
267 | int chmask = *data++; |
||
233 | f9daq | 268 | nr +=8; |
225 | f9daq | 269 | if (recid!=0x2) continue; |
242 | f9daq | 270 | for (int id=0; id<2; id++) { |
225 | f9daq | 271 | if ( !(chmask & (1 << id)) ) { |
242 | f9daq | 272 | if (neve % pfreq == 0) |
225 | f9daq | 273 | if (plothandle[id]) { |
274 | DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW); |
||
275 | plothandle[id] = 0; |
||
276 | } |
||
277 | continue; |
||
278 | } |
||
279 | if ( id != *(data++) ) printf("Error\n"); |
||
242 | f9daq | 280 | |
225 | f9daq | 281 | int nsamples = *(data++); |
242 | f9daq | 282 | if (nsamples<=0 || nsamples>16*1024) { |
229 | f9daq | 283 | printf("Error nsamples %d\n", nsamples); |
284 | return -1; |
||
242 | f9daq | 285 | } |
286 | float *fdata = (float *) data; |
||
233 | f9daq | 287 | if ( nsamples>0 && neve % pfreq == 0) { |
242 | f9daq | 288 | const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE}; |
289 | if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW); |
||
225 | f9daq | 290 | |
252 | f9daq | 291 | plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, fdata, nsamples, VAL_FLOAT, VAL_FLOAT, VAL_FAT_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]); |
242 | f9daq | 292 | |
293 | H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]); |
||
294 | H1D_Draw(2,panelHandle,PANEL_ADC2,&adcplothandle[1]); |
||
295 | H1D_Draw(3,panelHandle,PANEL_TDC1,&tdcplothandle[0]); |
||
296 | H1D_Draw(4,panelHandle,PANEL_TDC2,&tdcplothandle[1]); |
||
297 | |||
298 | if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]); |
||
225 | f9daq | 299 | } |
242 | f9daq | 300 | |
233 | f9daq | 301 | nr+=8; |
242 | f9daq | 302 | adc=10000; |
303 | qdc=0; |
||
245 | f9daq | 304 | int nqdc=0; |
305 | int ntdc=0; |
||
233 | f9daq | 306 | for (int k=1; k<nsamples; k++) { |
242 | f9daq | 307 | float t =timebins[k]; |
308 | float tp=timebins[k-1]; |
||
309 | if (fdata[k] < adc) adc = fdata[k]; |
||
310 | if (t>twin0 && t<twin1 ) { |
||
311 | nqdc++; |
||
312 | qdc+=fdata[k]; |
||
313 | } |
||
314 | if (fdata[k]< athreshold && fdata[k-1]> athreshold) { |
||
315 | double t0= tp+(athreshold-fdata[k-1])/(fdata[k]-fdata[k-1])* (t-tp); |
||
316 | H1D_Fill(3+id, t0,1); |
||
245 | f9daq | 317 | finfo[*ninfo+4+ntdc]=t0; |
318 | ntdc++; |
||
242 | f9daq | 319 | } |
229 | f9daq | 320 | } |
242 | f9daq | 321 | if (nqdc) qdc/=nqdc; |
322 | //printf("adc %f qdc %f\n", adc,qdc); |
||
245 | f9daq | 323 | info[*ninfo]=(ntdc+4)*sizeof(int); // len |
324 | info[*ninfo+1]=id | 0xF0000000; // recid |
||
325 | finfo[*ninfo+2]=adc; |
||
326 | finfo[*ninfo+3]=qdc; |
||
327 | *ninfo+= ntdc+4; |
||
328 | |||
242 | f9daq | 329 | if (adctype) |
330 | H1D_Fill(1+id, -adc,1); |
||
331 | else |
||
332 | H1D_Fill(1+id, -qdc,1); |
||
229 | f9daq | 333 | |
233 | f9daq | 334 | nr+=4*nsamples; |
242 | f9daq | 335 | data+=nsamples; |
225 | f9daq | 336 | } |
337 | recid = *data++; |
||
338 | int event = *data++; |
||
233 | f9daq | 339 | nr+=8; |
340 | neve++; |
||
242 | f9daq | 341 | if (debug) printf("recid %d event %d\n",recid, event ); |
342 | } |
||
225 | f9daq | 343 | |
233 | f9daq | 344 | return neve; |
225 | f9daq | 345 | } |
346 | |||
233 | f9daq | 347 | const int maxlen = 100000000; |
348 | unsigned char data[maxlen]; |
||
225 | f9daq | 349 | int *idata = (int *) &data[0]; |
350 | |||
351 | |||
245 | f9daq | 352 | int evinfo[maxlen]; |
225 | f9daq | 353 | int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) { |
354 | |||
355 | int nb = 0 ; |
||
356 | static int event = 0; |
||
242 | f9daq | 357 | static int ncalls = 0; |
245 | f9daq | 358 | static time_t t0, t1; |
359 | time(&t0); |
||
225 | f9daq | 360 | switch (xType) { |
361 | case TCP_DISCONNECT: |
||
362 | printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode)); |
||
363 | printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString()); |
||
241 | f9daq | 364 | |
225 | f9daq | 365 | chandle = 0; |
366 | break; |
||
367 | case TCP_DATAREADY: { |
||
242 | f9daq | 368 | int hdr[2]= {0,0}; |
233 | f9daq | 369 | nb = ClientTCPRead(handle,&hdr[0],8,1000); |
242 | f9daq | 370 | |
371 | int size = hdr[1] - 8; |
||
233 | f9daq | 372 | if (size>maxlen) size=maxlen; |
373 | nb = 0; |
||
374 | while (nb < size) { |
||
242 | f9daq | 375 | int retval = ClientTCPRead(handle,&data[nb],size-nb,1000); |
376 | if (retval<1) break; |
||
377 | nb += retval; |
||
233 | f9daq | 378 | } |
242 | f9daq | 379 | if (debug) printf("Received RECID %d HDRLEN %d == read %d\n", hdr[0], size, nb); |
380 | |||
245 | f9daq | 381 | int ninfo=0; |
233 | f9daq | 382 | switch (hdr[0]) { |
383 | case 0: |
||
384 | data[nb]=0; |
||
385 | printf("%s\n",data); |
||
225 | f9daq | 386 | break; |
233 | f9daq | 387 | case 1:// read |
242 | f9daq | 388 | |
389 | GetCtrlVal(panelHandle,PANEL_CEVE , &event); |
||
233 | f9daq | 390 | GetCtrlVal(panelHandle,PANEL_NTOTAL , &ncalls); |
245 | f9daq | 391 | |
392 | event += analyse(nb,data, evinfo, &ninfo); |
||
393 | if (foutput) { |
||
394 | if (outwaveforms) fwrite(data, 1, nb, foutput); |
||
395 | fwrite(evinfo, 1, ninfo*sizeof(int), foutput); |
||
396 | time(&t1); |
||
397 | if ((t1-t0)>10000) { |
||
398 | for (int i=0;i<4;i++) H1D_Write2File(1+i,foutput); |
||
399 | |||
400 | time(&t0); |
||
401 | } |
||
402 | } |
||
242 | f9daq | 403 | SetCtrlVal(panelHandle,PANEL_CEVE , event); |
404 | if (event>=ncalls) StartCB (panelHandle, PANEL_START, EVENT_COMMIT, NULL,1,0); |
||
405 | |||
225 | f9daq | 406 | break; |
407 | default: |
||
233 | f9daq | 408 | printf("Unknown command = %d\n", hdr[0]); |
225 | f9daq | 409 | break; |
410 | } |
||
411 | break; |
||
242 | f9daq | 412 | |
225 | f9daq | 413 | } |
414 | } |
||
415 | return 0; |
||
416 | } |
||
417 | |||
250 | f9daq | 418 | int rpdecimation(int i){ |
225 | f9daq | 419 | |
250 | f9daq | 420 | switch (i){ |
421 | case 1: return 0; |
||
422 | case 8: return 1; |
||
423 | case 64: return 2; |
||
424 | case 1024: return 3; |
||
425 | case 8192: return 4; |
||
426 | case 65536: return 5; |
||
427 | } |
||
428 | return 0; |
||
225 | f9daq | 429 | |
250 | f9daq | 430 | } |
431 | |||
225 | f9daq | 432 | int CVICALLBACK StartCB (int panel, int control, int event, |
433 | void *callbackData, int eventData1, int eventData2) { |
||
434 | char ip[0xFF]; |
||
233 | f9daq | 435 | int hdr[0xFF]; |
242 | f9daq | 436 | char filename[0xFF]; |
233 | f9daq | 437 | unsigned short *sbuff = (unsigned short *)&hdr[5] ; |
242 | f9daq | 438 | unsigned char *cbuff = (unsigned char *)&sbuff[3] ; |
225 | f9daq | 439 | int imask[2]; |
440 | unsigned char mask; |
||
441 | unsigned char trigger; |
||
442 | unsigned short nsamples; |
||
242 | f9daq | 443 | int delay; |
229 | f9daq | 444 | int nbefore; |
250 | f9daq | 445 | unsigned int decimation; |
225 | f9daq | 446 | unsigned short neve; |
233 | f9daq | 447 | int output; |
225 | f9daq | 448 | switch (event) { |
449 | case EVENT_COMMIT: { |
||
242 | f9daq | 450 | GetCtrlVal(panel,PANEL_IP, ip); |
225 | f9daq | 451 | GetCtrlVal(panel,PANEL_TRIGGER, &trigger); |
452 | GetCtrlVal(panel,PANEL_SAMPLES, &nsamples); |
||
453 | GetCtrlVal(panel,PANEL_DECIMATION,&decimation); |
||
454 | GetCtrlVal(panel,PANEL_NEVE , &neve); |
||
455 | GetCtrlVal(panel,PANEL_CH0 , &imask[0] ); |
||
456 | GetCtrlVal(panel,PANEL_CH1 , &imask[1] ); |
||
457 | GetCtrlVal(panel,PANEL_PFREQ , &pfreq); |
||
458 | GetCtrlVal(panel,PANEL_DEBUG , &debug); |
||
229 | f9daq | 459 | GetCtrlVal(panel,PANEL_NBEFORE , &nbefore); |
233 | f9daq | 460 | GetCtrlVal(panel,PANEL_ENABLEDOUTPUT, &output); |
250 | f9daq | 461 | GetCtrlVal(panel,PANEL_OUTWAVE, &outwaveforms); |
242 | f9daq | 462 | GetCtrlVal(panel,PANEL_FILENAME, filename); |
463 | |||
229 | f9daq | 464 | delay= MINTRGDELAY + nsamples - nbefore + 1; |
242 | f9daq | 465 | |
225 | f9daq | 466 | mask = 0; |
467 | for (int i=0; i<2; i++) { |
||
468 | if (imask[i]) mask |= (1<<i); |
||
469 | } |
||
470 | |||
471 | double level =0; |
||
472 | GetCtrlVal(panel,PANEL_TRGLEVEL , &level); |
||
242 | f9daq | 473 | |
225 | f9daq | 474 | |
475 | switch (control) { |
||
242 | f9daq | 476 | case PANEL_CONNECT: { |
477 | int state; |
||
478 | GetCtrlVal(panel,PANEL_CONNECT, &state); |
||
479 | if (state) { |
||
480 | ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT); |
||
481 | } else { |
||
482 | if (chandle!=0) DisconnectFromTCPServer (chandle); |
||
483 | chandle = 0; |
||
484 | } |
||
485 | } |
||
486 | break; |
||
487 | case PANEL_START: { |
||
488 | int state; |
||
489 | GetCtrlVal(panel,PANEL_START, &state); |
||
490 | if (state && eventData1==0) { |
||
491 | histoinit(); |
||
250 | f9daq | 492 | /* decimation n (=1,8,64...) : frequency = 125/n MHz*/ |
493 | for (int i=0; i<nsamples; i++) timebins[i]=(i-nbefore)* (float)decimation /125.; |
||
242 | f9daq | 494 | if (output) foutput = fopen(filename, "wb"); |
250 | f9daq | 495 | printf("decimation %d\n", decimation); |
242 | f9daq | 496 | SetCtrlVal(panel,PANEL_CEVE , 0); |
497 | ctrl_c=0; |
||
498 | hdr[0] = 0; |
||
499 | hdr[1] = 7*sizeof(int); |
||
500 | hdr[2] = delay; |
||
250 | f9daq | 501 | hdr[3] = rpdecimation(decimation); |
242 | f9daq | 502 | hdr[4] = level * 1000; |
503 | sbuff[0] = neve; |
||
504 | sbuff[1] = nsamples; |
||
505 | sbuff[2] = 1000; //tout |
||
506 | cbuff[0] = trigger; |
||
507 | cbuff[1] = mask; |
||
508 | ClientTCPWrite(chandle,&hdr[0],hdr[1],5000); // acquire |
||
509 | } else { |
||
510 | hdr[0] = 1; |
||
511 | hdr[1] = 2*sizeof(int); |
||
512 | ClientTCPWrite(chandle,&hdr[0],hdr[1],5000); // stop the transfer |
||
251 | f9daq | 513 | printf("INFO Stopping the acquisition\n"); |
514 | SetCtrlVal(panel,PANEL_START, 0); |
||
242 | f9daq | 515 | if (foutput) fclose(foutput); |
516 | foutput=NULL; |
||
517 | } |
||
225 | f9daq | 518 | break; |
242 | f9daq | 519 | } |
225 | f9daq | 520 | default: |
521 | printf("Unknown command\n"); |
||
522 | break; |
||
523 | } |
||
524 | |||
525 | |||
526 | |||
242 | f9daq | 527 | |
225 | f9daq | 528 | ProcessSystemEvents (); |
529 | break; |
||
530 | } |
||
531 | } |
||
532 | return 0; |
||
533 | } |
||
534 | |||
535 | |||
536 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
537 | void *callbackData, int eventData1, int eventData2) { |
||
538 | switch (event) { |
||
539 | case EVENT_COMMIT: |
||
540 | QuitUserInterface(0); |
||
541 | break; |
||
542 | } |
||
543 | return 0; |
||
544 | } |
||
545 | |||
229 | f9daq | 546 | |
233 | f9daq | 547 | |
229 | f9daq | 548 | int CVICALLBACK ExportCB (int panel, int control, int event, |
549 | void *callbackData, int eventData1, int eventData2) { |
||
550 | int hid=0; |
||
551 | switch (event) { |
||
552 | case EVENT_COMMIT: |
||
242 | f9daq | 553 | switch (control) { |
554 | case PANEL_EXPORT_1: |
||
555 | hid=1; |
||
556 | break; |
||
557 | case PANEL_EXPORT_2: |
||
558 | hid=2; |
||
559 | break; |
||
560 | case PANEL_EXPORT_3: |
||
561 | hid=3; |
||
562 | break; |
||
563 | case PANEL_EXPORT_4: |
||
564 | hid=4; |
||
565 | break; |
||
566 | |||
229 | f9daq | 567 | } |
242 | f9daq | 568 | export_data(hid); |
229 | f9daq | 569 | break; |
570 | } |
||
571 | return 0; |
||
572 | } |
||
573 | |||
574 | int CVICALLBACK ResetCB (int panel, int control, int event, |
||
575 | void *callbackData, int eventData1, int eventData2) { |
||
576 | switch (event) { |
||
577 | case EVENT_COMMIT: |
||
242 | f9daq | 578 | |
579 | for (int i=1; i<=4; i++) H1D_Clear(i); |
||
229 | f9daq | 580 | break; |
581 | } |
||
582 | return 0; |
||
583 | } |
||
252 | f9daq | 584 | |
585 |