Subversion Repositories f9daq

Rev

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