Subversion Repositories f9daq

Rev

Rev 322 | 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) {
337 f9daq 435
    case TCP_CONNECT:
436
      printf("TCP_CONNECT \n");  
437
      break;
225 f9daq 438
    case TCP_DISCONNECT:
439
      printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
440
      printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
241 f9daq 441
 
225 f9daq 442
      chandle = 0;
443
      break;
444
    case TCP_DATAREADY: {
242 f9daq 445
      int  hdr[2]= {0,0};
233 f9daq 446
      nb = ClientTCPRead(handle,&hdr[0],8,1000);
242 f9daq 447
 
448
      int size = hdr[1] - 8;
233 f9daq 449
      if (size>maxlen) size=maxlen;
450
      nb = 0;
451
      while (nb < size) {
242 f9daq 452
        int retval = ClientTCPRead(handle,&data[nb],size-nb,1000);
453
        if (retval<1) break;
454
        nb += retval;
233 f9daq 455
      }
242 f9daq 456
      if (debug) printf("Received RECID %d HDRLEN %d == read %d\n", hdr[0], size, nb);
457
 
263 f9daq 458
      int ninfo=0;
233 f9daq 459
      switch (hdr[0]) {
460
        case 0:
461
          data[nb]=0;
462
          printf("%s\n",data);
225 f9daq 463
          break;
233 f9daq 464
        case 1:// read
242 f9daq 465
 
466
          GetCtrlVal(panelHandle,PANEL_CEVE     , &event);
322 f9daq 467
          GetCtrlVal(settingsHandle,SETTINGS_NTOTAL   , &ncalls);
263 f9daq 468
 
245 f9daq 469
          event += analyse(nb,data, evinfo, &ninfo);
470
          if (foutput) {
263 f9daq 471
            if (outwaveforms) fwrite(data, 1, nb, foutput);
472
            fwrite(evinfo, 1, ninfo*sizeof(int), foutput);
473
            time(&t1);
474
            if ((t1-t0)>10000) {
475
              for (int i=0; i<4; i++) H1D_Write2File(1+i,foutput);
476
 
477
              time(&t0);
478
            }
479
          }
242 f9daq 480
          SetCtrlVal(panelHandle,PANEL_CEVE     , event);
481
          if (event>=ncalls) StartCB (panelHandle, PANEL_START, EVENT_COMMIT, NULL,1,0);
482
 
225 f9daq 483
          break;
484
        default:
233 f9daq 485
          printf("Unknown command = %d\n", hdr[0]);
225 f9daq 486
          break;
487
      }
488
      break;
242 f9daq 489
 
225 f9daq 490
    }
491
  }
492
  return 0;
493
}
494
 
263 f9daq 495
int rpdecimation(int i) {
225 f9daq 496
 
263 f9daq 497
  switch (i) {
498
    case 1:
499
      return 0;
500
    case 8:
501
      return 1;
502
    case 64:
503
      return 2;
504
    case 1024:
505
      return 3;
506
    case 8192:
507
      return 4;
508
    case 65536:
509
      return 5;
510
  }
511
  return 0;
225 f9daq 512
 
250 f9daq 513
}
514
 
225 f9daq 515
int CVICALLBACK StartCB (int panel, int control, int event,
516
                         void *callbackData, int eventData1, int eventData2) {
517
  char ip[0xFF];
322 f9daq 518
  char path[0xFF];
233 f9daq 519
  int hdr[0xFF];
242 f9daq 520
  char filename[0xFF];
322 f9daq 521
  char filepath[0xFF];  
233 f9daq 522
  unsigned short *sbuff = (unsigned short *)&hdr[5] ;
242 f9daq 523
  unsigned char  *cbuff = (unsigned char *)&sbuff[3] ;
225 f9daq 524
  int imask[2];
525
  unsigned char mask;
526
  unsigned char trigger;
527
  unsigned short nsamples;
242 f9daq 528
  int delay;
229 f9daq 529
  int nbefore;
250 f9daq 530
  unsigned int decimation;
225 f9daq 531
  unsigned short neve;
233 f9daq 532
  int output;
225 f9daq 533
  switch (event) {
322 f9daq 534
    case EVENT_COMMIT: {                                                                                              
535
      GetCtrlVal(settingsHandle,SETTINGS_IP, ip);
536
      GetCtrlVal(settingsHandle,SETTINGS_TRIGGER, &trigger);
537
      GetCtrlVal(settingsHandle,SETTINGS_SAMPLES, &nsamples);
538
      GetCtrlVal(settingsHandle,SETTINGS_DECIMATION,&decimation);
539
      GetCtrlVal(settingsHandle,SETTINGS_NEVE   , &neve);
540
      GetCtrlVal(settingsHandle,SETTINGS_CH0    , &imask[0] );
541
      GetCtrlVal(settingsHandle,SETTINGS_CH1    , &imask[1]  );
542
      GetCtrlVal(settingsHandle,SETTINGS_PFREQ  , &pfreq);
543
      GetCtrlVal(settingsHandle,SETTINGS_DEBUG  , &debug);
544
      GetCtrlVal(settingsHandle,SETTINGS_NBEFORE  , &nbefore);
545
      GetCtrlVal(settingsHandle,SETTINGS_ENABLEDOUTPUT, &output);
263 f9daq 546
      GetCtrlVal(panel,PANEL_OUTWAVE, &outwaveforms);
322 f9daq 547
      GetCtrlVal(settingsHandle,SETTINGS_FILENAME, filename);
548
      sprintf(path, "%s\\Desktop\\data", getenv("USERPROFILE"));
549
      MakeDir (path);
550
      sprintf(filepath,"%s\\%s", path, filename);
229 f9daq 551
      delay= MINTRGDELAY + nsamples - nbefore + 1;
242 f9daq 552
 
225 f9daq 553
      mask = 0;
554
      for (int i=0; i<2; i++) {
555
        if (imask[i]) mask |= (1<<i);
556
      }
557
 
558
      double level =0;
322 f9daq 559
      GetCtrlVal(settingsHandle,SETTINGS_TRGLEVEL , &level);
225 f9daq 560
 
263 f9daq 561
 
225 f9daq 562
      switch (control) {
242 f9daq 563
        case PANEL_CONNECT: {
564
          int state;
565
          GetCtrlVal(panel,PANEL_CONNECT, &state);
566
          if (state) {
567
            ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
568
          } else {
569
            if (chandle!=0) DisconnectFromTCPServer (chandle);
570
            chandle = 0;
571
          }
572
        }
573
        break;
574
        case PANEL_START: {
575
          int state;
576
          GetCtrlVal(panel,PANEL_START, &state);
322 f9daq 577
          GetCtrlVal(ch0Handle,CH0_EXCLUDE_1, &excludefirst[0]);
578
          GetCtrlVal(ch1Handle,CH1_EXCLUDE_2, &excludefirst[1]);
579
 
242 f9daq 580
          if (state && eventData1==0) {
581
            histoinit();
263 f9daq 582
            starttime=Timer();
583
            daqtime  =0;
584
            /* decimation n (=1,8,64...) : frequency = 125/n MHz*/
250 f9daq 585
            for (int i=0; i<nsamples; i++) timebins[i]=(i-nbefore)* (float)decimation /125.;
322 f9daq 586
            if (output) foutput = fopen(filepath, "wb");
263 f9daq 587
            printf("decimation %d\n", decimation);
242 f9daq 588
            SetCtrlVal(panel,PANEL_CEVE  , 0);
589
            ctrl_c=0;
590
            hdr[0] = 0;
591
            hdr[1] = 7*sizeof(int);
592
            hdr[2] = delay;
250 f9daq 593
            hdr[3] = rpdecimation(decimation);
242 f9daq 594
            hdr[4] = level * 1000;
595
            sbuff[0] = neve;
596
            sbuff[1] = nsamples;
597
            sbuff[2] = 1000; //tout
598
            cbuff[0] = trigger;
599
            cbuff[1] = mask;
600
            ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // acquire
601
          } else {
602
            hdr[0] = 1;
603
            hdr[1] = 2*sizeof(int);
604
            ClientTCPWrite(chandle,&hdr[0],hdr[1],5000);  // stop the transfer
263 f9daq 605
            printf("INFO Stopping the acquisition\n");
606
            SetCtrlVal(panel,PANEL_START, 0);
242 f9daq 607
            if (foutput) fclose(foutput);
608
            foutput=NULL;
609
          }
225 f9daq 610
          break;
242 f9daq 611
        }
225 f9daq 612
        default:
613
          printf("Unknown command\n");
614
          break;
615
      }
616
 
617
 
618
 
242 f9daq 619
 
225 f9daq 620
      ProcessSystemEvents ();
621
      break;
622
    }
623
  }
624
  return 0;
625
}
626
 
627
 
628
int CVICALLBACK ExitCB (int panel, int control, int event,
629
                        void *callbackData, int eventData1, int eventData2) {
630
  switch (event) {
631
    case EVENT_COMMIT:
632
      QuitUserInterface(0);
633
      break;
634
  }
635
  return 0;
636
}
637
 
229 f9daq 638
 
233 f9daq 639
 
229 f9daq 640
int CVICALLBACK ExportCB (int panel, int control, int event,
641
                          void *callbackData, int eventData1, int eventData2) {
642
  int hid=0;
643
  switch (event) {
644
    case EVENT_COMMIT:
322 f9daq 645
      if  (panel== ch0Handle){
646
          switch (control) {
647
            case CH0_EXPORT_1:
648
              hid=1;
649
              break;
242 f9daq 650
 
322 f9daq 651
            case CH0_EXPORT_3:
652
              hid=3;
653
              break;
654
 
655
 
656
          }
657
      }  
658
      if  (panel== ch1Handle){  
659
          switch (control) {
660
 
661
            case CH1_EXPORT_2:
662
              hid=2;
663
              break;
664
 
665
            case CH1_EXPORT_4:
666
              hid=4;
667
              break;
668
 
669
          }
229 f9daq 670
      }
242 f9daq 671
      export_data(hid);
229 f9daq 672
      break;
673
  }
674
  return 0;
675
}
676
 
677
int CVICALLBACK ResetCB (int panel, int control, int event,
678
                         void *callbackData, int eventData1, int eventData2) {
679
  switch (event) {
680
    case EVENT_COMMIT:
242 f9daq 681
 
682
      for (int i=1; i<=4; i++) H1D_Clear(i);
229 f9daq 683
      break;
684
  }
685
  return 0;
686
}
252 f9daq 687
 
688