Subversion Repositories f9daq

Rev

Rev 243 | Rev 250 | 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
 
233 f9daq 133
int histoinit() {
134
  int nch;
135
  float min,max;
242 f9daq 136
 
233 f9daq 137
  GetCtrlVal(panelHandle,PANEL_NCH_1, &nch);
138
  GetCtrlVal(panelHandle,PANEL_MINX_1, &min);
139
  GetCtrlVal(panelHandle,PANEL_MAXX_1, &max);
225 f9daq 140
 
233 f9daq 141
  H1D_Init(1, "ADC ch 1","Pulse height", nch, min, max );
241 f9daq 142
  H1D_SetTitleX(1,"ADC (V)");
233 f9daq 143
  H1D_SetTitleY(1,"N");
229 f9daq 144
 
233 f9daq 145
  GetCtrlVal(panelHandle,PANEL_NCH_2, &nch);
146
  GetCtrlVal(panelHandle,PANEL_MINX_2, &min);
147
  GetCtrlVal(panelHandle,PANEL_MAXX_2, &max);
148
 
149
  H1D_Init(2, "ADC ch 2","Pulse height", nch, min, max );
241 f9daq 150
  H1D_SetTitleX(2,"ADC (V)");
233 f9daq 151
  H1D_SetTitleY(2,"N");
152
 
153
  GetCtrlVal(panelHandle,PANEL_NCH_3, &nch);
154
  GetCtrlVal(panelHandle,PANEL_MINX_3, &min);
155
  GetCtrlVal(panelHandle,PANEL_MAXX_3, &max);
156
 
157
  H1D_Init(3, "TDC ch 1","TDC", nch, min, max  );
241 f9daq 158
  H1D_SetTitleX(3,"TDC (us)");
233 f9daq 159
  H1D_SetTitleY(3,"N");
160
 
161
  GetCtrlVal(panelHandle,PANEL_NCH_4, &nch);
162
  GetCtrlVal(panelHandle,PANEL_MINX_4, &min);
163
  GetCtrlVal(panelHandle,PANEL_MAXX_4, &max);
164
 
165
  H1D_Init(4, "TDC ch 2","TDC", nch, min, max   );
241 f9daq 166
  H1D_SetTitleX(4,"TDC (us)");
233 f9daq 167
  H1D_SetTitleY(4,"N");
242 f9daq 168
 
241 f9daq 169
  SetCtrlAttribute  (panelHandle, PANEL_ADC1, ATTR_XNAME, H1D_GetTitleX(1) );
170
  SetCtrlAttribute  (panelHandle, PANEL_ADC1, ATTR_YNAME, H1D_GetTitleY(1) );
171
  SetCtrlAttribute  (panelHandle, PANEL_ADC2, ATTR_XNAME, H1D_GetTitleX(2) );
172
  SetCtrlAttribute  (panelHandle, PANEL_ADC2, ATTR_YNAME, H1D_GetTitleY(2) );
242 f9daq 173
 
241 f9daq 174
  SetCtrlAttribute  (panelHandle, PANEL_TDC1, ATTR_XNAME, H1D_GetTitleX(3) );
175
  SetCtrlAttribute  (panelHandle, PANEL_TDC1, ATTR_YNAME, H1D_GetTitleY(3) );
176
  SetCtrlAttribute  (panelHandle, PANEL_TDC2, ATTR_XNAME, H1D_GetTitleX(4) );
177
  SetCtrlAttribute  (panelHandle, PANEL_TDC2, ATTR_YNAME, H1D_GetTitleY(4) );
242 f9daq 178
 
233 f9daq 179
  GetCtrlVal(panelHandle,PANEL_MINX_5, &min);
180
  GetCtrlVal(panelHandle,PANEL_MAXX_5, &max);
242 f9daq 181
  SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_LEFT_YAXIS, VAL_MANUAL, min, max);
182
  SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_LABEL_TEXT , "sampling adc data");
241 f9daq 183
  SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_XNAME, "t(us)" );
184
  SetCtrlAttribute  (panelHandle, PANEL_GRAPH, ATTR_YNAME, "U(V)" );
242 f9daq 185
 
186
  GetCtrlVal(panelHandle,PANEL_TWIN0, &twin0);
187
  GetCtrlVal(panelHandle,PANEL_TWIN1, &twin1);
188
  GetCtrlVal(panelHandle,PANEL_ADCTYPE, &adctype);
189
  GetCtrlVal(panelHandle,PANEL_ITRGLEVEL  , &athreshold);
233 f9daq 190
  return 0;
191
}
225 f9daq 192
 
245 f9daq 193
int analyse(int nb, unsigned char *cdata, int *info, int *ninfo) {
242 f9daq 194
 
195
  int   *ibuf = (int *)cdata;
233 f9daq 196
  float *fbuf = (float *)cdata;
245 f9daq 197
  float *finfo = (float *)info;
233 f9daq 198
  int neve=0;
199
  int *data = (ibuf+3);
200
  int nr=0;
242 f9daq 201
  static float adc = 10000;
202
  static float qdc = 0;
245 f9daq 203
  *ninfo = 0;
242 f9daq 204
  printf("Run HDR LEN=%d NEVE=%d dt=%f adc=%f qdc=%f\n", ibuf[0],ibuf[1],fbuf[2],adc,qdc);
233 f9daq 205
  while (nr<nb) {
206
 
225 f9daq 207
    int recid   = *data++;
208
    int chmask  = *data++;
233 f9daq 209
    nr +=8;
225 f9daq 210
    if (recid!=0x2) continue;
242 f9daq 211
    for (int id=0; id<2; id++) {
225 f9daq 212
      if ( !(chmask & (1 << id)) ) {
242 f9daq 213
        if (neve % pfreq == 0)
225 f9daq 214
          if (plothandle[id]) {
215
            DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
216
            plothandle[id] = 0;
217
          }
218
        continue;
219
      }
220
      if ( id != *(data++) ) printf("Error\n");
242 f9daq 221
 
225 f9daq 222
      int nsamples = *(data++);
242 f9daq 223
      if (nsamples<=0 || nsamples>16*1024) {
229 f9daq 224
        printf("Error nsamples %d\n", nsamples);
225
        return -1;
242 f9daq 226
      }
227
      float *fdata = (float *) data;
233 f9daq 228
      if ( nsamples>0  && neve % pfreq == 0) {
242 f9daq 229
        const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE};
230
        if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
225 f9daq 231
 
242 f9daq 232
        plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, fdata, nsamples, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
233
 
234
        //plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, data, nsamples, VAL_FLOAT, VAL_INTEGER, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
235
        H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]);
236
        H1D_Draw(2,panelHandle,PANEL_ADC2,&adcplothandle[1]);
237
        H1D_Draw(3,panelHandle,PANEL_TDC1,&tdcplothandle[0]);
238
        H1D_Draw(4,panelHandle,PANEL_TDC2,&tdcplothandle[1]);
239
 
240
        if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]);
225 f9daq 241
      }
242 f9daq 242
 
233 f9daq 243
      nr+=8;
242 f9daq 244
      adc=10000;
245
      qdc=0;
245 f9daq 246
      int nqdc=0;
247
          int ntdc=0;
233 f9daq 248
      for (int k=1; k<nsamples; k++) {
242 f9daq 249
        float t =timebins[k];
250
        float tp=timebins[k-1];
251
        if (fdata[k] < adc) adc = fdata[k];
252
        if (t>twin0 && t<twin1 ) {
253
          nqdc++;
254
          qdc+=fdata[k];
255
        }  
256
        if (fdata[k]< athreshold && fdata[k-1]> athreshold) {
257
          double t0= tp+(athreshold-fdata[k-1])/(fdata[k]-fdata[k-1])* (t-tp);
258
          H1D_Fill(3+id, t0,1);
245 f9daq 259
                  finfo[*ninfo+4+ntdc]=t0;
260
                  ntdc++;
242 f9daq 261
        }
229 f9daq 262
      }
242 f9daq 263
      if (nqdc) qdc/=nqdc;
264
      //printf("adc %f qdc %f\n", adc,qdc);
245 f9daq 265
          info[*ninfo]=(ntdc+4)*sizeof(int); // len
266
          info[*ninfo+1]=id | 0xF0000000;    // recid
267
          finfo[*ninfo+2]=adc;
268
          finfo[*ninfo+3]=qdc;
269
          *ninfo+= ntdc+4;
270
 
242 f9daq 271
      if (adctype)
272
        H1D_Fill(1+id, -adc,1);
273
      else
274
        H1D_Fill(1+id, -qdc,1);
229 f9daq 275
 
233 f9daq 276
      nr+=4*nsamples;
242 f9daq 277
      data+=nsamples;
225 f9daq 278
    }
279
    recid      = *data++;
280
    int event  = *data++;
233 f9daq 281
    nr+=8;
282
    neve++;
242 f9daq 283
    if (debug) printf("recid %d event %d\n",recid, event );
284
  }
225 f9daq 285
 
233 f9daq 286
  return neve;
225 f9daq 287
}
288
 
233 f9daq 289
const int maxlen = 100000000;
290
unsigned char data[maxlen];
225 f9daq 291
int *idata = (int *) &data[0];
292
 
293
 
245 f9daq 294
int evinfo[maxlen];
225 f9daq 295
int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {
296
 
297
  int nb = 0 ;
298
  static int event = 0;
242 f9daq 299
  static int ncalls = 0;
245 f9daq 300
  static time_t t0, t1;
301
  time(&t0);
225 f9daq 302
  switch (xType) {
303
    case TCP_DISCONNECT:
304
      printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
305
      printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
241 f9daq 306
 
225 f9daq 307
      chandle = 0;
308
      break;
309
    case TCP_DATAREADY: {
242 f9daq 310
      int  hdr[2]= {0,0};
233 f9daq 311
      nb = ClientTCPRead(handle,&hdr[0],8,1000);
242 f9daq 312
 
313
      int size = hdr[1] - 8;
233 f9daq 314
      if (size>maxlen) size=maxlen;
315
      nb = 0;
316
      while (nb < size) {
242 f9daq 317
        int retval = ClientTCPRead(handle,&data[nb],size-nb,1000);
318
        if (retval<1) break;
319
        nb += retval;
233 f9daq 320
      }
242 f9daq 321
      if (debug) printf("Received RECID %d HDRLEN %d == read %d\n", hdr[0], size, nb);
322
 
245 f9daq 323
          int ninfo=0;
233 f9daq 324
      switch (hdr[0]) {
325
        case 0:
326
          data[nb]=0;
327
          printf("%s\n",data);
225 f9daq 328
          break;
233 f9daq 329
        case 1:// read
242 f9daq 330
 
331
          GetCtrlVal(panelHandle,PANEL_CEVE     , &event);
233 f9daq 332
          GetCtrlVal(panelHandle,PANEL_NTOTAL   , &ncalls);
245 f9daq 333
 
334
          event += analyse(nb,data, evinfo, &ninfo);
335
          if (foutput) {
336
                        if (outwaveforms) fwrite(data, 1, nb, foutput);
337
                        fwrite(evinfo, 1, ninfo*sizeof(int), foutput);
338
                        time(&t1);
339
                        if ((t1-t0)>10000) {
340
                           for (int i=0;i<4;i++) H1D_Write2File(1+i,foutput);
341
 
342
                           time(&t0);
343
                        }      
344
                  }
242 f9daq 345
          SetCtrlVal(panelHandle,PANEL_CEVE     , event);
346
          if (event>=ncalls) StartCB (panelHandle, PANEL_START, EVENT_COMMIT, NULL,1,0);
347
 
225 f9daq 348
          break;
349
        default:
233 f9daq 350
          printf("Unknown command = %d\n", hdr[0]);
225 f9daq 351
          break;
352
      }
353
      break;
242 f9daq 354
 
225 f9daq 355
    }
356
  }
357
  return 0;
358
}
359
 
360
 
361
 
362
int CVICALLBACK StartCB (int panel, int control, int event,
363
                         void *callbackData, int eventData1, int eventData2) {
364
  char ip[0xFF];
233 f9daq 365
  int hdr[0xFF];
242 f9daq 366
  char filename[0xFF];
233 f9daq 367
  unsigned short *sbuff = (unsigned short *)&hdr[5] ;
242 f9daq 368
  unsigned char  *cbuff = (unsigned char *)&sbuff[3] ;
225 f9daq 369
  int imask[2];
370
  unsigned char mask;
371
  unsigned char trigger;
372
  unsigned short nsamples;
242 f9daq 373
  int delay;
229 f9daq 374
  int nbefore;
225 f9daq 375
  unsigned short decimation;
376
  unsigned short neve;
233 f9daq 377
  int output;
225 f9daq 378
  switch (event) {
379
    case EVENT_COMMIT: {
242 f9daq 380
      GetCtrlVal(panel,PANEL_IP, ip);
225 f9daq 381
      GetCtrlVal(panel,PANEL_TRIGGER, &trigger);
382
      GetCtrlVal(panel,PANEL_SAMPLES, &nsamples);
383
      GetCtrlVal(panel,PANEL_DECIMATION,&decimation);
384
      GetCtrlVal(panel,PANEL_NEVE   , &neve);
385
      GetCtrlVal(panel,PANEL_CH0    , &imask[0] );
386
      GetCtrlVal(panel,PANEL_CH1    , &imask[1]  );
387
      GetCtrlVal(panel,PANEL_PFREQ  , &pfreq);
388
      GetCtrlVal(panel,PANEL_DEBUG  , &debug);
229 f9daq 389
      GetCtrlVal(panel,PANEL_NBEFORE  , &nbefore);
233 f9daq 390
      GetCtrlVal(panel,PANEL_ENABLEDOUTPUT, &output);
245 f9daq 391
          GetCtrlVal(panel,PANEL_OUTWAVE, &outwaveforms);  
242 f9daq 392
      GetCtrlVal(panel,PANEL_FILENAME, filename);
393
 
229 f9daq 394
      delay= MINTRGDELAY + nsamples - nbefore + 1;
242 f9daq 395
 
225 f9daq 396
      mask = 0;
397
      for (int i=0; i<2; i++) {
398
        if (imask[i]) mask |= (1<<i);
399
      }
400
 
401
      double level =0;
402
      GetCtrlVal(panel,PANEL_TRGLEVEL , &level);
242 f9daq 403
 
225 f9daq 404
 
405
      switch (control) {
242 f9daq 406
        case PANEL_CONNECT: {
407
          int state;
408
          GetCtrlVal(panel,PANEL_CONNECT, &state);
409
          if (state) {
410
            ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
411
          } else {
412
            if (chandle!=0) DisconnectFromTCPServer (chandle);
413
            chandle = 0;
414
          }
415
        }
416
        break;
417
        case PANEL_START: {
418
          int state;
419
          GetCtrlVal(panel,PANEL_START, &state);
420
          if (state && eventData1==0) {
421
            histoinit();
422
            for (int i=0; i<nsamples; i++) timebins[i]=(i-nbefore)*8*decimation/1000.;
423
            if (output) foutput = fopen(filename, "wb");
225 f9daq 424
 
242 f9daq 425
            SetCtrlVal(panel,PANEL_CEVE  , 0);
426
            ctrl_c=0;
427
            hdr[0] = 0;
428
            hdr[1] = 7*sizeof(int);
429
            hdr[2] = delay;
430
            hdr[3] = decimation;
431
            hdr[4] = level * 1000;
432
            sbuff[0] = neve;
433
            sbuff[1] = nsamples;
434
            sbuff[2] = 1000; //tout
435
            cbuff[0] = trigger;
436
            cbuff[1] = mask;
437
            ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // acquire
438
          } else {
439
            hdr[0] = 1;
440
            hdr[1] = 2*sizeof(int);
441
            ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // stop the transfer
442
 
443
            if (foutput) fclose(foutput);
444
            foutput=NULL;
445
          }
225 f9daq 446
          break;
242 f9daq 447
        }
225 f9daq 448
        default:
449
          printf("Unknown command\n");
450
          break;
451
      }
452
 
453
 
454
 
242 f9daq 455
 
225 f9daq 456
      ProcessSystemEvents ();
457
      break;
458
    }
459
  }
460
  return 0;
461
}
462
 
463
 
464
int CVICALLBACK ExitCB (int panel, int control, int event,
465
                        void *callbackData, int eventData1, int eventData2) {
466
  switch (event) {
467
    case EVENT_COMMIT:
468
      QuitUserInterface(0);
469
      break;
470
  }
471
  return 0;
472
}
473
 
229 f9daq 474
 
233 f9daq 475
 
229 f9daq 476
int CVICALLBACK ExportCB (int panel, int control, int event,
477
                          void *callbackData, int eventData1, int eventData2) {
478
  int hid=0;
479
  switch (event) {
480
    case EVENT_COMMIT:
242 f9daq 481
      switch (control) {
482
        case PANEL_EXPORT_1:
483
          hid=1;
484
          break;
485
        case PANEL_EXPORT_2:
486
          hid=2;
487
          break;
488
        case PANEL_EXPORT_3:
489
          hid=3;
490
          break;
491
        case PANEL_EXPORT_4:
492
          hid=4;
493
          break;
494
 
229 f9daq 495
      }
242 f9daq 496
      export_data(hid);
229 f9daq 497
      break;
498
  }
499
  return 0;
500
}
501
 
502
int CVICALLBACK ResetCB (int panel, int control, int event,
503
                         void *callbackData, int eventData1, int eventData2) {
504
  switch (event) {
505
    case EVENT_COMMIT:
242 f9daq 506
 
507
      for (int i=1; i<=4; i++) H1D_Clear(i);
229 f9daq 508
      break;
509
  }
510
  return 0;
511
}