Subversion Repositories f9daq

Rev

Rev 252 | Rev 258 | 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);
253 f9daq 181
 
182
 
183
                        GetCtrlVal(panelHandle,PANEL_MINX_7, &min);
184
                        GetCtrlVal(panelHandle,PANEL_MAXX_7, &max);
185
                        GetCtrlVal(panelHandle,PANEL_AUTOY_2, &autoscale);
186
                        if (autoscale)
187
                                SetAxisScalingMode (panelHandle, PANEL_TDC1, VAL_LEFT_YAXIS, VAL_AUTOSCALE, min, max);
188
                        else
189
                                SetAxisScalingMode (panelHandle, PANEL_TDC1, VAL_LEFT_YAXIS, VAL_MANUAL, min, max);
190
 
191
                        GetCtrlVal(panelHandle,PANEL_MINX_8, &min);
192
                        GetCtrlVal(panelHandle,PANEL_MAXX_8, &max);
193
                        GetCtrlVal(panelHandle,PANEL_AUTOY_3, &autoscale);
194
                        if (autoscale)
195
                                SetAxisScalingMode (panelHandle, PANEL_ADC1, VAL_LEFT_YAXIS, VAL_AUTOSCALE, min, max);
196
                        else
197
                                SetAxisScalingMode (panelHandle, PANEL_ADC1, VAL_LEFT_YAXIS, VAL_MANUAL, min, max);
198
 
199
                        GetCtrlVal(panelHandle,PANEL_MINX_9, &min);
200
                        GetCtrlVal(panelHandle,PANEL_MAXX_9, &max);
201
                        GetCtrlVal(panelHandle,PANEL_AUTOY_4, &autoscale);
202
                        if (autoscale)
203
                                SetAxisScalingMode (panelHandle, PANEL_TDC2, VAL_LEFT_YAXIS, VAL_AUTOSCALE, min, max);
204
                        else
205
                                SetAxisScalingMode (panelHandle, PANEL_TDC2, VAL_LEFT_YAXIS, VAL_MANUAL, min, max);
252 f9daq 206
 
253 f9daq 207
                        GetCtrlVal(panelHandle,PANEL_MINX_10, &min);
208
                        GetCtrlVal(panelHandle,PANEL_MAXX_10, &max);
209
                        GetCtrlVal(panelHandle,PANEL_AUTOY_5, &autoscale);
210
                        if (autoscale)
211
                                SetAxisScalingMode (panelHandle, PANEL_ADC2, VAL_LEFT_YAXIS, VAL_AUTOSCALE, min, max);
212
                        else
213
                                SetAxisScalingMode (panelHandle, PANEL_ADC2, VAL_LEFT_YAXIS, VAL_MANUAL, min, max);
252 f9daq 214
 
253 f9daq 215
 
216
 
252 f9daq 217
                        break;
218
        }
219
        return 0;
220
}
221
 
222
 
233 f9daq 223
int histoinit() {
224
  int nch;
225
  float min,max;
242 f9daq 226
 
233 f9daq 227
  GetCtrlVal(panelHandle,PANEL_NCH_1, &nch);
228
  GetCtrlVal(panelHandle,PANEL_MINX_1, &min);
229
  GetCtrlVal(panelHandle,PANEL_MAXX_1, &max);
225 f9daq 230
 
233 f9daq 231
  H1D_Init(1, "ADC ch 1","Pulse height", nch, min, max );
241 f9daq 232
  H1D_SetTitleX(1,"ADC (V)");
233 f9daq 233
  H1D_SetTitleY(1,"N");
229 f9daq 234
 
233 f9daq 235
  GetCtrlVal(panelHandle,PANEL_NCH_2, &nch);
236
  GetCtrlVal(panelHandle,PANEL_MINX_2, &min);
237
  GetCtrlVal(panelHandle,PANEL_MAXX_2, &max);
238
 
239
  H1D_Init(2, "ADC ch 2","Pulse height", nch, min, max );
241 f9daq 240
  H1D_SetTitleX(2,"ADC (V)");
233 f9daq 241
  H1D_SetTitleY(2,"N");
242
 
243
  GetCtrlVal(panelHandle,PANEL_NCH_3, &nch);
244
  GetCtrlVal(panelHandle,PANEL_MINX_3, &min);
245
  GetCtrlVal(panelHandle,PANEL_MAXX_3, &max);
246
 
247
  H1D_Init(3, "TDC ch 1","TDC", nch, min, max  );
241 f9daq 248
  H1D_SetTitleX(3,"TDC (us)");
233 f9daq 249
  H1D_SetTitleY(3,"N");
250
 
251
  GetCtrlVal(panelHandle,PANEL_NCH_4, &nch);
252
  GetCtrlVal(panelHandle,PANEL_MINX_4, &min);
253
  GetCtrlVal(panelHandle,PANEL_MAXX_4, &max);
254
 
255
  H1D_Init(4, "TDC ch 2","TDC", nch, min, max   );
241 f9daq 256
  H1D_SetTitleX(4,"TDC (us)");
233 f9daq 257
  H1D_SetTitleY(4,"N");
242 f9daq 258
 
241 f9daq 259
  SetCtrlAttribute  (panelHandle, PANEL_ADC1, ATTR_XNAME, H1D_GetTitleX(1) );
260
  SetCtrlAttribute  (panelHandle, PANEL_ADC1, ATTR_YNAME, H1D_GetTitleY(1) );
261
  SetCtrlAttribute  (panelHandle, PANEL_ADC2, ATTR_XNAME, H1D_GetTitleX(2) );
262
  SetCtrlAttribute  (panelHandle, PANEL_ADC2, ATTR_YNAME, H1D_GetTitleY(2) );
242 f9daq 263
 
241 f9daq 264
  SetCtrlAttribute  (panelHandle, PANEL_TDC1, ATTR_XNAME, H1D_GetTitleX(3) );
265
  SetCtrlAttribute  (panelHandle, PANEL_TDC1, ATTR_YNAME, H1D_GetTitleY(3) );
266
  SetCtrlAttribute  (panelHandle, PANEL_TDC2, ATTR_XNAME, H1D_GetTitleX(4) );
267
  SetCtrlAttribute  (panelHandle, PANEL_TDC2, ATTR_YNAME, H1D_GetTitleY(4) );
252 f9daq 268
  SetGraphLogYCB( panelHandle, PANEL_LOGY_1,  EVENT_COMMIT,NULL,0,0);
269
  SetGraphLogYCB( panelHandle, PANEL_LOGY_2,  EVENT_COMMIT,NULL,0,0);
270
  SetGraphLogYCB( panelHandle, PANEL_LOGY_3,  EVENT_COMMIT,NULL,0,0);
271
  SetGraphLogYCB( panelHandle, PANEL_LOGY_4,  EVENT_COMMIT,NULL,0,0);
272
 
273
  SetGraphPropertiesCB( panelHandle, PANEL, EVENT_COMMIT,NULL,0,0);
274
 
242 f9daq 275
  SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_LABEL_TEXT , "sampling adc data");
241 f9daq 276
  SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_XNAME, "t(us)" );
277
  SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_YNAME, "U(V)" );
242 f9daq 278
 
279
  GetCtrlVal(panelHandle,PANEL_TWIN0, &twin0);
280
  GetCtrlVal(panelHandle,PANEL_TWIN1, &twin1);
281
  GetCtrlVal(panelHandle,PANEL_ADCTYPE, &adctype);
282
  GetCtrlVal(panelHandle,PANEL_ITRGLEVEL  , &athreshold);
233 f9daq 283
  return 0;
284
}
225 f9daq 285
 
245 f9daq 286
int analyse(int nb, unsigned char *cdata, int *info, int *ninfo) {
242 f9daq 287
 
288
  int   *ibuf = (int *)cdata;
233 f9daq 289
  float *fbuf = (float *)cdata;
245 f9daq 290
  float *finfo = (float *)info;
233 f9daq 291
  int neve=0;
292
  int *data = (ibuf+3);
293
  int nr=0;
242 f9daq 294
  static float adc = 10000;
295
  static float qdc = 0;
245 f9daq 296
  *ninfo = 0;
242 f9daq 297
  printf("Run HDR LEN=%d NEVE=%d dt=%f adc=%f qdc=%f\n", ibuf[0],ibuf[1],fbuf[2],adc,qdc);
233 f9daq 298
  while (nr<nb) {
299
 
225 f9daq 300
    int recid   = *data++;
301
    int chmask  = *data++;
233 f9daq 302
    nr +=8;
225 f9daq 303
    if (recid!=0x2) continue;
242 f9daq 304
    for (int id=0; id<2; id++) {
225 f9daq 305
      if ( !(chmask & (1 << id)) ) {
242 f9daq 306
        if (neve % pfreq == 0)
225 f9daq 307
          if (plothandle[id]) {
308
            DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
309
            plothandle[id] = 0;
310
          }
311
        continue;
312
      }
313
      if ( id != *(data++) ) printf("Error\n");
242 f9daq 314
 
225 f9daq 315
      int nsamples = *(data++);
242 f9daq 316
      if (nsamples<=0 || nsamples>16*1024) {
229 f9daq 317
        printf("Error nsamples %d\n", nsamples);
318
        return -1;
242 f9daq 319
      }
320
      float *fdata = (float *) data;
233 f9daq 321
      if ( nsamples>0  && neve % pfreq == 0) {
242 f9daq 322
        const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE};
323
        if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
225 f9daq 324
 
252 f9daq 325
        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 326
 
327
        H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]);
328
        H1D_Draw(2,panelHandle,PANEL_ADC2,&adcplothandle[1]);
329
        H1D_Draw(3,panelHandle,PANEL_TDC1,&tdcplothandle[0]);
330
        H1D_Draw(4,panelHandle,PANEL_TDC2,&tdcplothandle[1]);
331
 
332
        if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]);
225 f9daq 333
      }
242 f9daq 334
 
233 f9daq 335
      nr+=8;
242 f9daq 336
      adc=10000;
337
      qdc=0;
245 f9daq 338
      int nqdc=0;
339
          int ntdc=0;
233 f9daq 340
      for (int k=1; k<nsamples; k++) {
242 f9daq 341
        float t =timebins[k];
342
        float tp=timebins[k-1];
343
        if (fdata[k] < adc) adc = fdata[k];
344
        if (t>twin0 && t<twin1 ) {
345
          nqdc++;
346
          qdc+=fdata[k];
347
        }  
348
        if (fdata[k]< athreshold && fdata[k-1]> athreshold) {
349
          double t0= tp+(athreshold-fdata[k-1])/(fdata[k]-fdata[k-1])* (t-tp);
350
          H1D_Fill(3+id, t0,1);
245 f9daq 351
                  finfo[*ninfo+4+ntdc]=t0;
352
                  ntdc++;
242 f9daq 353
        }
229 f9daq 354
      }
242 f9daq 355
      if (nqdc) qdc/=nqdc;
356
      //printf("adc %f qdc %f\n", adc,qdc);
245 f9daq 357
          info[*ninfo]=(ntdc+4)*sizeof(int); // len
358
          info[*ninfo+1]=id | 0xF0000000;    // recid
359
          finfo[*ninfo+2]=adc;
360
          finfo[*ninfo+3]=qdc;
361
          *ninfo+= ntdc+4;
362
 
242 f9daq 363
      if (adctype)
364
        H1D_Fill(1+id, -adc,1);
365
      else
366
        H1D_Fill(1+id, -qdc,1);
229 f9daq 367
 
233 f9daq 368
      nr+=4*nsamples;
242 f9daq 369
      data+=nsamples;
225 f9daq 370
    }
371
    recid      = *data++;
372
    int event  = *data++;
233 f9daq 373
    nr+=8;
374
    neve++;
242 f9daq 375
    if (debug) printf("recid %d event %d\n",recid, event );
376
  }
225 f9daq 377
 
233 f9daq 378
  return neve;
225 f9daq 379
}
380
 
233 f9daq 381
const int maxlen = 100000000;
382
unsigned char data[maxlen];
225 f9daq 383
int *idata = (int *) &data[0];
384
 
385
 
245 f9daq 386
int evinfo[maxlen];
225 f9daq 387
int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {
388
 
389
  int nb = 0 ;
390
  static int event = 0;
242 f9daq 391
  static int ncalls = 0;
245 f9daq 392
  static time_t t0, t1;
393
  time(&t0);
225 f9daq 394
  switch (xType) {
395
    case TCP_DISCONNECT:
396
      printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
397
      printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
241 f9daq 398
 
225 f9daq 399
      chandle = 0;
400
      break;
401
    case TCP_DATAREADY: {
242 f9daq 402
      int  hdr[2]= {0,0};
233 f9daq 403
      nb = ClientTCPRead(handle,&hdr[0],8,1000);
242 f9daq 404
 
405
      int size = hdr[1] - 8;
233 f9daq 406
      if (size>maxlen) size=maxlen;
407
      nb = 0;
408
      while (nb < size) {
242 f9daq 409
        int retval = ClientTCPRead(handle,&data[nb],size-nb,1000);
410
        if (retval<1) break;
411
        nb += retval;
233 f9daq 412
      }
242 f9daq 413
      if (debug) printf("Received RECID %d HDRLEN %d == read %d\n", hdr[0], size, nb);
414
 
245 f9daq 415
          int ninfo=0;
233 f9daq 416
      switch (hdr[0]) {
417
        case 0:
418
          data[nb]=0;
419
          printf("%s\n",data);
225 f9daq 420
          break;
233 f9daq 421
        case 1:// read
242 f9daq 422
 
423
          GetCtrlVal(panelHandle,PANEL_CEVE     , &event);
233 f9daq 424
          GetCtrlVal(panelHandle,PANEL_NTOTAL   , &ncalls);
245 f9daq 425
 
426
          event += analyse(nb,data, evinfo, &ninfo);
427
          if (foutput) {
428
                        if (outwaveforms) fwrite(data, 1, nb, foutput);
429
                        fwrite(evinfo, 1, ninfo*sizeof(int), foutput);
430
                        time(&t1);
431
                        if ((t1-t0)>10000) {
432
                           for (int i=0;i<4;i++) H1D_Write2File(1+i,foutput);
433
 
434
                           time(&t0);
435
                        }      
436
                  }
242 f9daq 437
          SetCtrlVal(panelHandle,PANEL_CEVE     , event);
438
          if (event>=ncalls) StartCB (panelHandle, PANEL_START, EVENT_COMMIT, NULL,1,0);
439
 
225 f9daq 440
          break;
441
        default:
233 f9daq 442
          printf("Unknown command = %d\n", hdr[0]);
225 f9daq 443
          break;
444
      }
445
      break;
242 f9daq 446
 
225 f9daq 447
    }
448
  }
449
  return 0;
450
}
451
 
250 f9daq 452
int rpdecimation(int i){
225 f9daq 453
 
250 f9daq 454
        switch (i){
455
                case 1: return 0;
456
                case 8: return 1;
457
                case 64: return 2;
458
                case 1024: return 3;
459
                case 8192: return 4;
460
                case 65536: return 5;
461
        }
462
        return 0;
225 f9daq 463
 
250 f9daq 464
}
465
 
225 f9daq 466
int CVICALLBACK StartCB (int panel, int control, int event,
467
                         void *callbackData, int eventData1, int eventData2) {
468
  char ip[0xFF];
233 f9daq 469
  int hdr[0xFF];
242 f9daq 470
  char filename[0xFF];
233 f9daq 471
  unsigned short *sbuff = (unsigned short *)&hdr[5] ;
242 f9daq 472
  unsigned char  *cbuff = (unsigned char *)&sbuff[3] ;
225 f9daq 473
  int imask[2];
474
  unsigned char mask;
475
  unsigned char trigger;
476
  unsigned short nsamples;
242 f9daq 477
  int delay;
229 f9daq 478
  int nbefore;
250 f9daq 479
  unsigned int decimation;
225 f9daq 480
  unsigned short neve;
233 f9daq 481
  int output;
225 f9daq 482
  switch (event) {
483
    case EVENT_COMMIT: {
242 f9daq 484
      GetCtrlVal(panel,PANEL_IP, ip);
225 f9daq 485
      GetCtrlVal(panel,PANEL_TRIGGER, &trigger);
486
      GetCtrlVal(panel,PANEL_SAMPLES, &nsamples);
487
      GetCtrlVal(panel,PANEL_DECIMATION,&decimation);
488
      GetCtrlVal(panel,PANEL_NEVE   , &neve);
489
      GetCtrlVal(panel,PANEL_CH0    , &imask[0] );
490
      GetCtrlVal(panel,PANEL_CH1    , &imask[1]  );
491
      GetCtrlVal(panel,PANEL_PFREQ  , &pfreq);
492
      GetCtrlVal(panel,PANEL_DEBUG  , &debug);
229 f9daq 493
      GetCtrlVal(panel,PANEL_NBEFORE  , &nbefore);
233 f9daq 494
      GetCtrlVal(panel,PANEL_ENABLEDOUTPUT, &output);
250 f9daq 495
            GetCtrlVal(panel,PANEL_OUTWAVE, &outwaveforms);  
242 f9daq 496
      GetCtrlVal(panel,PANEL_FILENAME, filename);
497
 
229 f9daq 498
      delay= MINTRGDELAY + nsamples - nbefore + 1;
242 f9daq 499
 
225 f9daq 500
      mask = 0;
501
      for (int i=0; i<2; i++) {
502
        if (imask[i]) mask |= (1<<i);
503
      }
504
 
505
      double level =0;
506
      GetCtrlVal(panel,PANEL_TRGLEVEL , &level);
242 f9daq 507
 
225 f9daq 508
 
509
      switch (control) {
242 f9daq 510
        case PANEL_CONNECT: {
511
          int state;
512
          GetCtrlVal(panel,PANEL_CONNECT, &state);
513
          if (state) {
514
            ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
515
          } else {
516
            if (chandle!=0) DisconnectFromTCPServer (chandle);
517
            chandle = 0;
518
          }
519
        }
520
        break;
521
        case PANEL_START: {
522
          int state;
523
          GetCtrlVal(panel,PANEL_START, &state);
524
          if (state && eventData1==0) {
525
            histoinit();
250 f9daq 526
                                                /* decimation n (=1,8,64...) : frequency = 125/n MHz*/
527
            for (int i=0; i<nsamples; i++) timebins[i]=(i-nbefore)* (float)decimation /125.;
242 f9daq 528
            if (output) foutput = fopen(filename, "wb");
250 f9daq 529
                                                printf("decimation %d\n", decimation);
242 f9daq 530
            SetCtrlVal(panel,PANEL_CEVE  , 0);
531
            ctrl_c=0;
532
            hdr[0] = 0;
533
            hdr[1] = 7*sizeof(int);
534
            hdr[2] = delay;
250 f9daq 535
            hdr[3] = rpdecimation(decimation);
242 f9daq 536
            hdr[4] = level * 1000;
537
            sbuff[0] = neve;
538
            sbuff[1] = nsamples;
539
            sbuff[2] = 1000; //tout
540
            cbuff[0] = trigger;
541
            cbuff[1] = mask;
542
            ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // acquire
543
          } else {
544
            hdr[0] = 1;
545
            hdr[1] = 2*sizeof(int);
546
            ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // stop the transfer
251 f9daq 547
                                                printf("INFO Stopping the acquisition\n");
548
                                                SetCtrlVal(panel,PANEL_START, 0);
242 f9daq 549
            if (foutput) fclose(foutput);
550
            foutput=NULL;
551
          }
225 f9daq 552
          break;
242 f9daq 553
        }
225 f9daq 554
        default:
555
          printf("Unknown command\n");
556
          break;
557
      }
558
 
559
 
560
 
242 f9daq 561
 
225 f9daq 562
      ProcessSystemEvents ();
563
      break;
564
    }
565
  }
566
  return 0;
567
}
568
 
569
 
570
int CVICALLBACK ExitCB (int panel, int control, int event,
571
                        void *callbackData, int eventData1, int eventData2) {
572
  switch (event) {
573
    case EVENT_COMMIT:
574
      QuitUserInterface(0);
575
      break;
576
  }
577
  return 0;
578
}
579
 
229 f9daq 580
 
233 f9daq 581
 
229 f9daq 582
int CVICALLBACK ExportCB (int panel, int control, int event,
583
                          void *callbackData, int eventData1, int eventData2) {
584
  int hid=0;
585
  switch (event) {
586
    case EVENT_COMMIT:
242 f9daq 587
      switch (control) {
588
        case PANEL_EXPORT_1:
589
          hid=1;
590
          break;
591
        case PANEL_EXPORT_2:
592
          hid=2;
593
          break;
594
        case PANEL_EXPORT_3:
595
          hid=3;
596
          break;
597
        case PANEL_EXPORT_4:
598
          hid=4;
599
          break;
600
 
229 f9daq 601
      }
242 f9daq 602
      export_data(hid);
229 f9daq 603
      break;
604
  }
605
  return 0;
606
}
607
 
608
int CVICALLBACK ResetCB (int panel, int control, int event,
609
                         void *callbackData, int eventData1, int eventData2) {
610
  switch (event) {
611
    case EVENT_COMMIT:
242 f9daq 612
 
613
      for (int i=1; i<=4; i++) H1D_Clear(i);
229 f9daq 614
      break;
615
  }
616
  return 0;
617
}
252 f9daq 618
 
619