Rev 233 | Rev 242 | 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; |
||
20 | static int tfID; |
||
21 | int pfreq; |
||
22 | static int plothandle[2]= {0,0}; |
||
229 | 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; |
||
233 | f9daq | 29 | float athreshold; |
225 | f9daq | 30 | float timebins[0XFFFF]; |
233 | f9daq | 31 | |
32 | FILE *foutput; |
||
33 | |||
225 | f9daq | 34 | static CmtThreadPoolHandle poolHandle = 0; |
35 | int ctrl_c=0; |
||
36 | |||
37 | |||
38 | int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData); |
||
39 | |||
40 | |||
229 | f9daq | 41 | |
42 | static int export_data (int hid) { |
||
43 | |||
44 | char filename[0xFF],rootcmd[0xFF]; |
||
45 | int type=0; |
||
233 | f9daq | 46 | |
47 | GetCtrlVal(panelHandle,PANEL_EXPORTNAME,filename); |
||
229 | f9daq | 48 | GetCtrlVal(panelHandle,PANEL_FILETYPE,&type); |
49 | |||
50 | FILE *fp; |
||
51 | |||
52 | switch (type) { |
||
53 | case 0: |
||
54 | case 1: |
||
55 | fp =fopen(filename,"wb"); |
||
56 | if (fp) { |
||
57 | H1D_Write2File(hid,fp); |
||
58 | fclose(fp); |
||
59 | if (type) { |
||
60 | sprintf(rootcmd ,"thisroot.bat && root.exe H1Dload.cxx(\\\"%s\\\")", filename); |
||
61 | LaunchExecutable(rootcmd); |
||
62 | |||
63 | } |
||
64 | } |
||
65 | break; |
||
66 | case 2: |
||
67 | fp=fopen(filename,"w"); |
||
68 | if (fp) { |
||
69 | for (int i=0; i<H1D_GetNbinsX(hid); i++) fprintf(fp,"%f\n", H1D_GetBinContent(hid,i) ); |
||
70 | fclose(fp); |
||
71 | } |
||
72 | break; |
||
73 | |||
74 | } |
||
75 | |||
76 | |||
77 | return (0); |
||
78 | } |
||
79 | |||
80 | |||
225 | f9daq | 81 | int main (int argc, char *argv[]) { |
82 | if (InitCVIRTE (0, argv, 0) == 0) |
||
83 | return -1; /* out of memory */ |
||
84 | if ((panelHandle = LoadPanel (0, "redpitaya_gui.uir", PANEL)) < 0) |
||
85 | return -1; |
||
86 | |||
87 | |||
88 | SetStdioPort (CVI_STDIO_WINDOW); |
||
89 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
90 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
233 | f9daq | 91 | |
229 | f9daq | 92 | //for (int i=0;i<1000;i++) H1D_Fill(1,i,i); |
233 | f9daq | 93 | //H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]); |
229 | f9daq | 94 | printf("size of double = %d\n",sizeof(double)); |
225 | f9daq | 95 | DisplayPanel (panelHandle); |
96 | RunUserInterface (); |
||
97 | DiscardPanel (panelHandle); |
||
98 | CmtDiscardThreadPool (poolHandle); |
||
99 | if (chandle!=0) DisconnectFromTCPServer (chandle); |
||
100 | return 0; |
||
101 | } |
||
102 | |||
103 | char strbuf[0xFF]; |
||
104 | |||
105 | int gLog=0; |
||
106 | |||
107 | int printf(const char *format, ...) { |
||
108 | va_list aptr; |
||
109 | int ret; |
||
110 | FILE *flog; |
||
111 | |||
112 | va_start(aptr, format); |
||
113 | ret = vsprintf(strbuf, format, aptr); |
||
114 | va_end(aptr); |
||
115 | SetCtrlVal(panelHandle,PANEL_STDIO,strbuf); |
||
116 | |||
117 | if (gLog) { |
||
118 | flog = fopen ("stdio.log", "a"); |
||
119 | fprintf (flog, "%s", strbuf); |
||
120 | fclose (flog); |
||
121 | } |
||
122 | return(ret); |
||
123 | } |
||
124 | |||
233 | f9daq | 125 | int histoinit() { |
126 | int nch; |
||
127 | float min,max; |
||
128 | |||
129 | GetCtrlVal(panelHandle,PANEL_NCH_1, &nch); |
||
130 | GetCtrlVal(panelHandle,PANEL_MINX_1, &min); |
||
131 | GetCtrlVal(panelHandle,PANEL_MAXX_1, &max); |
||
225 | f9daq | 132 | |
233 | f9daq | 133 | H1D_Init(1, "ADC ch 1","Pulse height", nch, min, max ); |
241 | f9daq | 134 | H1D_SetTitleX(1,"ADC (V)"); |
233 | f9daq | 135 | H1D_SetTitleY(1,"N"); |
229 | f9daq | 136 | |
233 | f9daq | 137 | GetCtrlVal(panelHandle,PANEL_NCH_2, &nch); |
138 | GetCtrlVal(panelHandle,PANEL_MINX_2, &min); |
||
139 | GetCtrlVal(panelHandle,PANEL_MAXX_2, &max); |
||
140 | |||
141 | H1D_Init(2, "ADC ch 2","Pulse height", nch, min, max ); |
||
241 | f9daq | 142 | H1D_SetTitleX(2,"ADC (V)"); |
233 | f9daq | 143 | H1D_SetTitleY(2,"N"); |
144 | |||
145 | GetCtrlVal(panelHandle,PANEL_NCH_3, &nch); |
||
146 | GetCtrlVal(panelHandle,PANEL_MINX_3, &min); |
||
147 | GetCtrlVal(panelHandle,PANEL_MAXX_3, &max); |
||
148 | |||
149 | H1D_Init(3, "TDC ch 1","TDC", nch, min, max ); |
||
241 | f9daq | 150 | H1D_SetTitleX(3,"TDC (us)"); |
233 | f9daq | 151 | H1D_SetTitleY(3,"N"); |
152 | |||
153 | GetCtrlVal(panelHandle,PANEL_NCH_4, &nch); |
||
154 | GetCtrlVal(panelHandle,PANEL_MINX_4, &min); |
||
155 | GetCtrlVal(panelHandle,PANEL_MAXX_4, &max); |
||
156 | |||
157 | H1D_Init(4, "TDC ch 2","TDC", nch, min, max ); |
||
241 | f9daq | 158 | H1D_SetTitleX(4,"TDC (us)"); |
233 | f9daq | 159 | H1D_SetTitleY(4,"N"); |
241 | f9daq | 160 | |
161 | SetCtrlAttribute (panelHandle, PANEL_ADC1, ATTR_XNAME, H1D_GetTitleX(1) ); |
||
162 | SetCtrlAttribute (panelHandle, PANEL_ADC1, ATTR_YNAME, H1D_GetTitleY(1) ); |
||
163 | SetCtrlAttribute (panelHandle, PANEL_ADC2, ATTR_XNAME, H1D_GetTitleX(2) ); |
||
164 | SetCtrlAttribute (panelHandle, PANEL_ADC2, ATTR_YNAME, H1D_GetTitleY(2) ); |
||
165 | |||
166 | SetCtrlAttribute (panelHandle, PANEL_TDC1, ATTR_XNAME, H1D_GetTitleX(3) ); |
||
167 | SetCtrlAttribute (panelHandle, PANEL_TDC1, ATTR_YNAME, H1D_GetTitleY(3) ); |
||
168 | SetCtrlAttribute (panelHandle, PANEL_TDC2, ATTR_XNAME, H1D_GetTitleX(4) ); |
||
169 | SetCtrlAttribute (panelHandle, PANEL_TDC2, ATTR_YNAME, H1D_GetTitleY(4) ); |
||
233 | f9daq | 170 | |
171 | GetCtrlVal(panelHandle,PANEL_MINX_5, &min); |
||
172 | GetCtrlVal(panelHandle,PANEL_MAXX_5, &max); |
||
173 | SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_LEFT_YAXIS, VAL_MANUAL, min, max); |
||
241 | f9daq | 174 | SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_LABEL_TEXT , "sampling adc data"); |
175 | SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_XNAME, "t(us)" ); |
||
176 | SetCtrlAttribute (panelHandle, PANEL_GRAPH, ATTR_YNAME, "U(V)" ); |
||
233 | f9daq | 177 | return 0; |
178 | } |
||
225 | f9daq | 179 | |
233 | f9daq | 180 | int analyse(int nb, unsigned char *cdata) { |
181 | |||
182 | int *ibuf = (int *)cdata; |
||
183 | float *fbuf = (float *)cdata; |
||
184 | int neve=0; |
||
185 | int *data = (ibuf+3); |
||
186 | int nr=0; |
||
187 | printf("Run HDR LEN=%d NEVE=%d dt=%f\n", ibuf[0],ibuf[1],fbuf[2]); |
||
188 | while (nr<nb) { |
||
189 | |||
225 | f9daq | 190 | int recid = *data++; |
191 | int chmask = *data++; |
||
233 | f9daq | 192 | nr +=8; |
225 | f9daq | 193 | if (recid!=0x2) continue; |
194 | for (int id=0;id<2;id++){ |
||
195 | if ( !(chmask & (1 << id)) ) { |
||
233 | f9daq | 196 | if (neve % pfreq == 0) |
225 | f9daq | 197 | if (plothandle[id]) { |
198 | DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW); |
||
199 | plothandle[id] = 0; |
||
200 | } |
||
201 | continue; |
||
202 | } |
||
203 | if ( id != *(data++) ) printf("Error\n"); |
||
233 | f9daq | 204 | |
225 | f9daq | 205 | int nsamples = *(data++); |
229 | f9daq | 206 | if (nsamples<=0 || nsamples>16*1024){ |
207 | printf("Error nsamples %d\n", nsamples); |
||
208 | return -1; |
||
209 | } |
||
233 | f9daq | 210 | float *fdata = (float *) data; |
211 | if ( nsamples>0 && neve % pfreq == 0) { |
||
225 | f9daq | 212 | const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE}; |
213 | if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW); |
||
241 | f9daq | 214 | |
233 | f9daq | 215 | plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, fdata, nsamples, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]); |
225 | f9daq | 216 | |
233 | f9daq | 217 | //plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, data, nsamples, VAL_FLOAT, VAL_INTEGER, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]); |
229 | f9daq | 218 | H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]); |
219 | H1D_Draw(2,panelHandle,PANEL_ADC2,&adcplothandle[1]); |
||
220 | H1D_Draw(3,panelHandle,PANEL_TDC1,&tdcplothandle[0]); |
||
221 | H1D_Draw(4,panelHandle,PANEL_TDC2,&tdcplothandle[1]); |
||
222 | |||
225 | f9daq | 223 | if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]); |
224 | } |
||
229 | f9daq | 225 | |
233 | f9daq | 226 | nr+=8; |
229 | f9daq | 227 | float adcdata = 10000; |
233 | f9daq | 228 | for (int k=1; k<nsamples; k++) { |
229 | if (fdata[k] < adcdata) adcdata = fdata[k]; |
||
230 | if (fdata[k]< athreshold && fdata[k-1]> athreshold) { |
||
231 | double t0= timebins[k-1]+(athreshold-fdata[k-1])/(fdata[k]-fdata[k-1])* (timebins[k]-timebins[k-1]); |
||
232 | H1D_Fill(3+id, t0,1); |
||
233 | } |
||
229 | f9daq | 234 | } |
235 | H1D_Fill(1+id, -adcdata,1); |
||
236 | |||
233 | f9daq | 237 | nr+=4*nsamples; |
229 | f9daq | 238 | data+=nsamples; |
225 | f9daq | 239 | } |
240 | recid = *data++; |
||
241 | int event = *data++; |
||
233 | f9daq | 242 | nr+=8; |
243 | neve++; |
||
225 | f9daq | 244 | //printf("recid %d event %d\n",recid, event ); |
245 | } |
||
246 | |||
233 | f9daq | 247 | return neve; |
225 | f9daq | 248 | } |
249 | |||
233 | f9daq | 250 | const int maxlen = 100000000; |
251 | unsigned char data[maxlen]; |
||
225 | f9daq | 252 | int *idata = (int *) &data[0]; |
253 | |||
254 | |||
229 | f9daq | 255 | |
225 | f9daq | 256 | int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) { |
257 | |||
258 | int nb = 0 ; |
||
259 | static int event = 0; |
||
229 | f9daq | 260 | static int ncalls = 0; |
233 | f9daq | 261 | |
225 | f9daq | 262 | switch (xType) { |
263 | case TCP_DISCONNECT: |
||
264 | printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode)); |
||
265 | printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString()); |
||
241 | f9daq | 266 | |
225 | f9daq | 267 | chandle = 0; |
268 | break; |
||
269 | case TCP_DATAREADY: { |
||
233 | f9daq | 270 | int hdr[2]={0,0}; |
271 | nb = ClientTCPRead(handle,&hdr[0],8,1000); |
||
225 | f9daq | 272 | |
233 | f9daq | 273 | int size = hdr[1] - 8; |
274 | if (size>maxlen) size=maxlen; |
||
275 | nb = 0; |
||
276 | while (nb < size) { |
||
277 | int retval = ClientTCPRead(handle,&data[nb],size-nb,1000); |
||
278 | if (retval<1) break; |
||
279 | nb += retval; |
||
280 | } |
||
281 | printf("Received RECID %d HDRLEN %d == read %d\n", hdr[0], size, nb); |
||
282 | |||
283 | |||
284 | switch (hdr[0]) { |
||
285 | case 0: |
||
286 | data[nb]=0; |
||
287 | printf("%s\n",data); |
||
225 | f9daq | 288 | break; |
233 | f9daq | 289 | case 1:// read |
290 | |||
229 | f9daq | 291 | GetCtrlVal(panelHandle,PANEL_CEVE , &event); |
233 | f9daq | 292 | GetCtrlVal(panelHandle,PANEL_NTOTAL , &ncalls); |
293 | event += analyse(nb,data); |
||
294 | if (foutput) fwrite(data, 1, nb, foutput); |
||
295 | |||
229 | f9daq | 296 | SetCtrlVal(panelHandle,PANEL_CEVE , event); |
233 | f9daq | 297 | if (event>=ncalls) StartCB (panelHandle, PANEL_END, EVENT_COMMIT, NULL,0,0); |
298 | |||
225 | f9daq | 299 | break; |
300 | default: |
||
233 | f9daq | 301 | printf("Unknown command = %d\n", hdr[0]); |
225 | f9daq | 302 | break; |
303 | } |
||
304 | break; |
||
305 | |||
306 | } |
||
307 | } |
||
308 | return 0; |
||
309 | } |
||
310 | |||
311 | |||
312 | |||
313 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
314 | void *callbackData, int eventData1, int eventData2) { |
||
315 | char ip[0xFF]; |
||
233 | f9daq | 316 | int hdr[0xFF]; |
317 | char filename[0xFF]; |
||
318 | unsigned short *sbuff = (unsigned short *)&hdr[5] ; |
||
319 | unsigned char *cbuff = (unsigned char *)&sbuff[3] ; |
||
225 | f9daq | 320 | int imask[2]; |
321 | unsigned char mask; |
||
322 | unsigned char trigger; |
||
323 | unsigned short nsamples; |
||
229 | f9daq | 324 | int delay; |
325 | int nbefore; |
||
225 | f9daq | 326 | unsigned short decimation; |
327 | unsigned short neve; |
||
233 | f9daq | 328 | int output; |
225 | f9daq | 329 | switch (event) { |
330 | case EVENT_COMMIT: { |
||
331 | GetCtrlVal(panel,PANEL_IP, ip); |
||
332 | GetCtrlVal(panel,PANEL_TRIGGER, &trigger); |
||
333 | GetCtrlVal(panel,PANEL_SAMPLES, &nsamples); |
||
334 | GetCtrlVal(panel,PANEL_DECIMATION,&decimation); |
||
335 | GetCtrlVal(panel,PANEL_NEVE , &neve); |
||
336 | GetCtrlVal(panel,PANEL_CH0 , &imask[0] ); |
||
337 | GetCtrlVal(panel,PANEL_CH1 , &imask[1] ); |
||
338 | GetCtrlVal(panel,PANEL_PFREQ , &pfreq); |
||
339 | GetCtrlVal(panel,PANEL_DEBUG , &debug); |
||
229 | f9daq | 340 | GetCtrlVal(panel,PANEL_NBEFORE , &nbefore); |
233 | f9daq | 341 | GetCtrlVal(panel,PANEL_ENABLEDOUTPUT, &output); |
342 | GetCtrlVal(panel,PANEL_FILENAME, filename); |
||
229 | f9daq | 343 | |
344 | delay= MINTRGDELAY + nsamples - nbefore + 1; |
||
241 | f9daq | 345 | |
225 | f9daq | 346 | mask = 0; |
347 | for (int i=0; i<2; i++) { |
||
348 | if (imask[i]) mask |= (1<<i); |
||
349 | } |
||
350 | |||
351 | double level =0; |
||
352 | GetCtrlVal(panel,PANEL_TRGLEVEL , &level); |
||
233 | f9daq | 353 | GetCtrlVal(panel,PANEL_ITRGLEVEL , &athreshold); |
225 | f9daq | 354 | |
355 | switch (control) { |
||
356 | case PANEL_CONNECT: |
||
357 | ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT); |
||
358 | break; |
||
233 | f9daq | 359 | case PANEL_DISCONNECT: |
360 | if (chandle!=0) DisconnectFromTCPServer (chandle); |
||
361 | chandle = 0; |
||
362 | break; |
||
363 | case PANEL_START: |
||
364 | histoinit(); |
||
365 | if (output) foutput = fopen(filename, "wb"); |
||
366 | |||
367 | SetCtrlVal(panel,PANEL_CEVE , 0); |
||
225 | f9daq | 368 | ctrl_c=0; |
233 | f9daq | 369 | hdr[0] = 0; |
370 | hdr[1] = 7*sizeof(int); |
||
371 | hdr[2] = delay; |
||
372 | hdr[3] = decimation; |
||
373 | hdr[4] = level * 1000; |
||
225 | f9daq | 374 | sbuff[0] = neve; |
375 | sbuff[1] = nsamples; |
||
376 | sbuff[2] = 1000; //tout |
||
233 | f9daq | 377 | cbuff[0] = trigger; |
378 | cbuff[1] = mask; |
||
379 | ClientTCPWrite(chandle,&hdr[0],hdr[1],5000); // acquire |
||
225 | f9daq | 380 | break; |
381 | |||
382 | case PANEL_END: |
||
233 | f9daq | 383 | hdr[0] = 1; |
384 | hdr[1] = 2*sizeof(int); |
||
385 | ClientTCPWrite(chandle,&hdr[0],hdr[1],5000); // stop the transfer |
||
386 | |||
387 | if (foutput) fclose(foutput); |
||
388 | foutput=NULL; |
||
389 | |||
225 | f9daq | 390 | break; |
391 | default: |
||
392 | printf("Unknown command\n"); |
||
393 | break; |
||
394 | } |
||
395 | |||
396 | |||
397 | for (int i=0; i<nsamples; i++) timebins[i]=i*8*decimation/1000.; |
||
398 | |||
399 | ProcessSystemEvents (); |
||
400 | break; |
||
401 | } |
||
402 | } |
||
403 | return 0; |
||
404 | } |
||
405 | |||
406 | |||
407 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
408 | void *callbackData, int eventData1, int eventData2) { |
||
409 | switch (event) { |
||
410 | case EVENT_COMMIT: |
||
411 | QuitUserInterface(0); |
||
412 | break; |
||
413 | } |
||
414 | return 0; |
||
415 | } |
||
416 | |||
229 | f9daq | 417 | |
233 | f9daq | 418 | |
229 | f9daq | 419 | int CVICALLBACK ExportCB (int panel, int control, int event, |
420 | void *callbackData, int eventData1, int eventData2) { |
||
421 | int hid=0; |
||
422 | switch (event) { |
||
423 | case EVENT_COMMIT: |
||
424 | switch (control){ |
||
425 | case PANEL_EXPORT_1: hid=1; break; |
||
426 | case PANEL_EXPORT_2: hid=2; break; |
||
427 | case PANEL_EXPORT_3: hid=3; break; |
||
428 | case PANEL_EXPORT_4: hid=4; break; |
||
429 | |||
430 | } |
||
431 | export_data(hid); |
||
432 | break; |
||
433 | } |
||
434 | return 0; |
||
435 | } |
||
436 | |||
437 | int CVICALLBACK ResetCB (int panel, int control, int event, |
||
438 | void *callbackData, int eventData1, int eventData2) { |
||
439 | switch (event) { |
||
440 | case EVENT_COMMIT: |
||
233 | f9daq | 441 | |
229 | f9daq | 442 | for (int i=1;i<=4;i++) H1D_Clear(i); |
443 | break; |
||
444 | } |
||
445 | return 0; |
||
446 | } |