Subversion Repositories f9daq

Rev

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