Subversion Repositories f9daq

Rev

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