Subversion Repositories f9daq

Rev

Rev 226 | Rev 233 | 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};
229 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;
229 f9daq 29
int ithreshold;
225 f9daq 30
float timebins[0XFFFF];
31
static CmtThreadPoolHandle poolHandle = 0;
32
int ctrl_c=0;
33
 
34
 
35
int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData);
36
 
37
 
229 f9daq 38
 
39
static int export_data (int hid) {
40
 
41
  char filename[0xFF],rootcmd[0xFF];
42
  int type=0;
43
  int neve;
44
  GetCtrlVal(panelHandle,PANEL_FILENAME,filename);
45
  GetCtrlVal(panelHandle,PANEL_FILETYPE,&type);
46
 
47
  FILE *fp;
48
 
49
  switch (type) {
50
    case 0:
51
    case 1:
52
      fp  =fopen(filename,"wb");
53
      if (fp) {
54
        H1D_Write2File(hid,fp);
55
        fclose(fp);
56
        if (type) {
57
          sprintf(rootcmd ,"thisroot.bat && root.exe H1Dload.cxx(\\\"%s\\\")", filename);
58
          LaunchExecutable(rootcmd);
59
 
60
        }
61
      }
62
      break;
63
    case 2:
64
      fp=fopen(filename,"w");
65
      if (fp) {
66
        for (int i=0; i<H1D_GetNbinsX(hid); i++) fprintf(fp,"%f\n", H1D_GetBinContent(hid,i) );
67
        fclose(fp);
68
      }
69
      break;
70
 
71
  }
72
 
73
 
74
  return (0);
75
}
76
 
77
 
225 f9daq 78
int main (int argc, char *argv[]) {
79
  if (InitCVIRTE (0, argv, 0) == 0)
80
    return -1;  /* out of memory */
81
  if ((panelHandle = LoadPanel (0, "redpitaya_gui.uir", PANEL)) < 0)
82
    return -1;
83
 
84
 
85
  SetStdioPort (CVI_STDIO_WINDOW);
86
  SetSleepPolicy(VAL_SLEEP_MORE);
87
  CmtNewThreadPool (MAX_THREADS,  &poolHandle);
229 f9daq 88
  H1D_Init(1, "ADC ch 1","Pulse height", 100, 0, 5000 );
89
  H1D_SetTitleX(1,"ADC (a.u.)");
90
  H1D_SetTitleY(1,"N");
91
  H1D_Init(3, "TDC ch 1","TDC", 200, 0, 1000 );
92
  H1D_SetTitleX(3,"TDC (a.u.)");
93
  H1D_SetTitleY(3,"N");
94
  H1D_Init(2, "ADC ch 2","Pulse height", 100, 0, 5000 );
95
  H1D_SetTitleX(2,"ADC (a.u.)");
96
  H1D_SetTitleY(2,"N");
97
  H1D_Init(4, "TDC ch 2","TDC", 200, 0, 1000 );
98
  H1D_SetTitleX(4,"TDC (a.u.)");
99
  H1D_SetTitleY(4,"N");
100
  //for (int i=0;i<1000;i++) H1D_Fill(1,i,i);
101
  H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]);
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
 
133
 
229 f9daq 134
 
225 f9daq 135
int analyse(int neve, unsigned char *cdata) {
229 f9daq 136
 
225 f9daq 137
  int *data = (int *) cdata;
138
  for (int i=0; i<neve; i++) {
139
 
140
    int recid   = *data++;
141
    int chmask  = *data++;
142
    if (recid!=0x2) continue;
143
    for (int id=0;id<2;id++){
144
      if ( !(chmask & (1 << id)) ) {
145
        if (i % pfreq == 0)
146
          if (plothandle[id]) {
147
            DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
148
            plothandle[id] = 0;
149
          }
150
        continue;
151
      }
152
      if ( id != *(data++) ) printf("Error\n");
153
      int nsamples = *(data++);
229 f9daq 154
      if (nsamples<=0 || nsamples>16*1024){
155
        printf("Error nsamples %d\n", nsamples);
156
        return -1;
157
      }  
225 f9daq 158
 
229 f9daq 159
      if ( nsamples>0  && i % pfreq == 0) {
225 f9daq 160
          const int col[4]= {VAL_RED,VAL_GREEN,VAL_BLUE,VAL_WHITE};
161
          if (plothandle[id]) DeleteGraphPlot (panelHandle, PANEL_GRAPH, plothandle[id], VAL_IMMEDIATE_DRAW);
229 f9daq 162
          //float fdata[nsamples];
163
          //for (int k=0;k<nsamples;k++) fdata[k] = *(float *) (data+k);
164
          //printf("data0 %f %d\n", fdata[0], data[0]);
165
          //plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, fdata, nsamples, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
225 f9daq 166
 
167
          plothandle[id] = PlotXY (panelHandle, PANEL_GRAPH, timebins, data, nsamples, VAL_FLOAT, VAL_INTEGER, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[id]);
229 f9daq 168
          H1D_Draw(1,panelHandle,PANEL_ADC1,&adcplothandle[0]);  
169
          H1D_Draw(2,panelHandle,PANEL_ADC2,&adcplothandle[1]);
170
          H1D_Draw(3,panelHandle,PANEL_TDC1,&tdcplothandle[0]);  
171
          H1D_Draw(4,panelHandle,PANEL_TDC2,&tdcplothandle[1]);
172
 
225 f9daq 173
          if (debug) for (int k=0; k<10; k++) printf("%d %d (%f , %d)\n", id,k, timebins[k],data[k]);
174
      }
229 f9daq 175
 
176
 
177
      float adcdata = 10000;
178
      for (int k=0; k<nsamples; k++) {
179
         if (data[k] < adcdata) adcdata = data[k];
180
         if (data[k] < ithreshold && data[k-1] > ithreshold) H1D_Fill(3+id, k,1);
181
      }
182
      H1D_Fill(1+id, -adcdata,1);
183
 
184
 
185
      data+=nsamples;
225 f9daq 186
    }
187
    recid      = *data++;
188
    int event  = *data++;
189
    //printf("recid %d event %d\n",recid, event );
190
  }  
191
 
192
  return 0;
193
}
194
 
195
unsigned char data[100000000];
196
int *idata = (int *) &data[0];
197
 
198
 
229 f9daq 199
 
225 f9daq 200
int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {
201
 
202
  int nb = 0 ;
203
  static int event = 0;
229 f9daq 204
  static int ncalls = 0;
225 f9daq 205
  unsigned char cmd;
206
  switch (xType) {
207
    case TCP_DISCONNECT:
208
      printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
209
      printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
210
      //DisconnectFromTCPServer (&chandle);
211
      chandle = 0;
212
      break;
213
    case TCP_DATAREADY: {
214
      printf("Data Ready \n");
215
      nb = ClientTCPRead(handle,&cmd,1,1000);
216
      printf("Server Response = %d\n", cmd);
217
      int hdr[3]= {0,0,0};
218
 
219
      switch (cmd) {
220
        case 1: // init finished
221
          initialized = 1;
222
          break;
223
        case 2:
224
          break;
225
        case 3: // data ready
226
          cmd = 4;
227
          ClientTCPWrite(handle,&cmd,1,5000);  // read 
228
          break;
229
        case 4:// read
230
          {
231
          ClientTCPRead(handle,&hdr[0],12,5000);
232
          int neve = hdr[1];
233
          printf("Client: Data received: %d, %d, %d\n", hdr[0], neve, hdr[2]);
234
          int size = hdr[0]-12;
235
          nb = 0;
236
          while (nb < size) {
237
            int retval = ClientTCPRead(handle,&data[nb],size-nb,5000);
238
            if (retval<1) break;
239
            nb += retval;
240
          }
241
          printf("Read nb = %d size %d\n", nb, size);
242
          analyse(neve, data);
229 f9daq 243
          GetCtrlVal(panelHandle,PANEL_CEVE     , &event);          
244
          GetCtrlVal(panelHandle,PANEL_NCALLS   , &ncalls);
245
          event +=1;
246
          SetCtrlVal(panelHandle,PANEL_CEVE     , event);  
247
          if (ctrl_c==0 && event < ncalls) StartCB (panelHandle, PANEL_START,EVENT_COMMIT,NULL, 0, 0 );
225 f9daq 248
 
249
          }
250
 
251
          break;
252
        default:
253
          printf("Unknown command = %d\n", cmd);
254
          break;
255
      }
256
      break;
257
 
258
    }
259
  }
260
  return 0;
261
}
262
 
263
 
264
 
265
int CVICALLBACK StartCB (int panel, int control, int event,
266
                         void *callbackData, int eventData1, int eventData2) {
267
  char msg[0xFF];
268
  char ip[0xFF];
269
  int *hdr   = (int *)&msg[1] ;
270
  unsigned short *sbuff = (unsigned short *)&msg[1] ;
271
  unsigned char  *cbuff = (unsigned char  *)&msg[1] ;
272
  int imask[2];
273
  unsigned char mask;
274
  unsigned char trigger;
275
  unsigned short nsamples;
229 f9daq 276
  int delay;  
277
  int nbefore;
225 f9daq 278
  unsigned short decimation;
279
  unsigned short neve;
280
  switch (event) {
281
    case EVENT_COMMIT: {
282
      GetCtrlVal(panel,PANEL_IP, ip);
283
      GetCtrlVal(panel,PANEL_TRIGGER, &trigger);
284
      GetCtrlVal(panel,PANEL_SAMPLES, &nsamples);
285
      GetCtrlVal(panel,PANEL_DECIMATION,&decimation);
286
      GetCtrlVal(panel,PANEL_NEVE   , &neve);
287
      GetCtrlVal(panel,PANEL_CH0    , &imask[0] );
288
      GetCtrlVal(panel,PANEL_CH1    , &imask[1]  );
289
      GetCtrlVal(panel,PANEL_PFREQ  , &pfreq);
290
      GetCtrlVal(panel,PANEL_DEBUG  , &debug);
229 f9daq 291
      GetCtrlVal(panel,PANEL_NBEFORE  , &nbefore);
292
 
293
      delay= MINTRGDELAY + nsamples - nbefore + 1;
294
      //delay= nsamples - NBEFORE + 1;       
225 f9daq 295
      mask = 0;
296
      for (int i=0; i<2; i++) {
297
        if (imask[i]) mask |= (1<<i);
298
      }
299
 
300
      double level =0;
301
      GetCtrlVal(panel,PANEL_TRGLEVEL , &level);
229 f9daq 302
      GetCtrlVal(panel,PANEL_ITRGLEVEL  , &ithreshold);
225 f9daq 303
 
304
      switch (control) {
305
        case PANEL_CONNECT:    
306
          ConnectToTCPServerEx (&chandle, 9930, ip, SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);
307
          break;
308
        case PANEL_INIT:
309
          ctrl_c=0;
310
          msg[0] = 1;
311
          hdr[0] = delay;
312
          hdr[1] = decimation;
313
          hdr[2] = level * 1000;
229 f9daq 314
          SetCtrlVal(panel,PANEL_CEVE  , 0);  
225 f9daq 315
          ClientTCPWrite(chandle,&msg[0],13,5000);  // init
316
          break;
317
        case PANEL_START:
318
          msg[0] = 3;
319
          sbuff[0] = neve;
320
          sbuff[1] = nsamples;
321
          sbuff[2] = 1000; //tout
322
          cbuff[6] = trigger;
323
          cbuff[7] = mask;
324
          ClientTCPWrite(chandle,&msg[0],9,5000);  // acquire
325
 
326
          break;
327
 
328
        case PANEL_END:
329
          msg[0] = 0;
330
 
331
          ClientTCPWrite(chandle,&msg[0],1,5000);  // disconnect
332
          chandle = 0;
333
          break;
334
        default:
335
          printf("Unknown command\n");
336
          break;
337
      }
338
 
339
 
340
      for (int i=0; i<nsamples; i++) timebins[i]=i*8*decimation/1000.;
341
 
342
      ProcessSystemEvents ();
343
      break;
344
    }
345
  }
346
  return 0;
347
}
348
 
349
 
350
 
351
int CVICALLBACK OpenGuiCB (int panel, int control, int event,
352
                           void *callbackData, int eventData1, int eventData2) {
353
  switch (event) {
354
    case EVENT_COMMIT:
355
 
356
      break;
357
  }
358
  return 0;
359
}
360
 
361
int CVICALLBACK ExitCB (int panel, int control, int event,
362
                        void *callbackData, int eventData1, int eventData2) {
363
  switch (event) {
364
    case EVENT_COMMIT:
365
      QuitUserInterface(0);
366
      break;
367
  }
368
  return 0;
369
}
370
 
371
int CVICALLBACK StopCB (int panel, int control, int event,
372
                        void *callbackData, int eventData1, int eventData2) {
373
  switch (event) {
374
    case EVENT_COMMIT:
375
      //if (chandle != 0)
376
      ctrl_c=1;
377
      break;
378
  }
379
  return 0;
380
}
229 f9daq 381
 
382
int CVICALLBACK ExportCB (int panel, int control, int event,
383
                          void *callbackData, int eventData1, int eventData2) {
384
  int hid=0;
385
  switch (event) {
386
    case EVENT_COMMIT:
387
      switch (control){
388
        case PANEL_EXPORT_1: hid=1; break;
389
        case PANEL_EXPORT_2: hid=2; break;  
390
        case PANEL_EXPORT_3: hid=3; break;  
391
        case PANEL_EXPORT_4: hid=4; break;  
392
 
393
      }
394
      export_data(hid);
395
      break;
396
  }
397
  return 0;
398
}
399
 
400
int CVICALLBACK ResetCB (int panel, int control, int event,
401
                         void *callbackData, int eventData1, int eventData2) {
402
  switch (event) {
403
    case EVENT_COMMIT:
404
      for (int i=1;i<=4;i++) H1D_Clear(i);
405
      break;
406
  }
407
  return 0;
408
}