Subversion Repositories f9daq

Rev

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