Subversion Repositories f9daq

Rev

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