Subversion Repositories f9daq

Rev

Rev 198 | Rev 201 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
195 f9daq 1
#include <utility.h>
2
#include <ansi_c.h>
3
#include <cvirte.h>             
4
#include <userint.h>
5
#include "drs4.h"
6
#include "drsread.h" 
7
 
8
static int daq_on;
200 f9daq 9
static int ph, plothandle[4];
195 f9daq 10
static int tfID;
11
static int controlID;
12
 
13
#define MAX_THREADS 10   
14
 
15
static CmtThreadPoolHandle poolHandle = 0;  
16
 
17
int main (int argc, char *argv[]) {
18
  if (InitCVIRTE (0, argv, 0) == 0)
19
    return -1;  /* out of memory */
20
  if ((ph = LoadPanel (0, "drs4.uir", PANEL)) < 0)
21
    return -1;
22
  SetStdioPort (CVI_STDIO_WINDOW);
23
  SetSleepPolicy(VAL_SLEEP_MORE);
24
  CmtNewThreadPool (MAX_THREADS,  &poolHandle);
25
 
26
  DisplayPanel (ph);
27
  RunUserInterface ();
28
  DiscardPanel (ph);
29
   CmtDiscardThreadPool (poolHandle);
30
  return 0;
31
}
32
 
33
 
34
 
35
 
36
 
37
static void start_timer (double tout) {
38
  SetCtrlAttribute (ph, PANEL_TIMER, ATTR_INTERVAL, tout);
39
  SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 1);
40
}
41
 
42
static void stop_timer ( void ) {
43
  SetCtrlAttribute (ph, PANEL_TIMER, ATTR_ENABLED, 0);
44
   DRSSetTimeout();      
45
}
46
 
47
 
48
 
49
 
50
void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle,
51
                               CmtThreadFunctionID functionID, unsigned int event,
52
                               int value, void *callbackData  ) {
53
 
54
  daq_on=0;
55
  //SetDimming(0);
56
  printf("End of Thread \n");
57
  return ;
58
 
59
}
60
 
61
 
62
int CVICALLBACK daq(void *functionData) {
63
 
64
 
65
  int neve;
66
  char filename[0xff];      
67
  char smask[0xff];      
68
  unsigned long mask;
69
  int frequency;
70
  double trgdelay;  
71
  double trglevel;  
72
  int trgtype;
73
  int trgchannel;
74
  int trgpolarity;  
75
  int verbose;  
76
  double range;
77
 
78
 
79
  GetCtrlVal(ph, PANEL_FILENAME, filename );
80
  GetCtrlVal(ph, PANEL_MASK, smask );      
81
  mask = strtoul (smask,NULL,0);
82
  GetCtrlVal(ph,PANEL_NEVE, &neve);
83
 
84
  GetCtrlVal(ph,PANEL_FREQUENCY, &frequency);
85
  GetCtrlVal(ph,PANEL_TRGDELAY, &trgdelay);
86
  GetCtrlVal(ph,PANEL_TRGCHANNEL, &trgchannel);
87
  GetCtrlVal(ph,PANEL_TRGTYPE, &trgtype);
88
  GetCtrlVal(ph,PANEL_TRGLEVEL, &trglevel);
89
  GetCtrlVal(ph,PANEL_TRGPOLARITY, &trgpolarity);
90
  GetCtrlVal(ph,PANEL_RANGE, &range);
91
  GetCtrlVal(ph,PANEL_DEBUG, &verbose);
92
 
93
  printf("mask=0x%x\n",mask);
94
 
95
  DRSSetMask( (unsigned char)( mask & 0xF ) );
96
 
97
  DRSSetFrequency( frequency  );
98
  DRSSetTriggerPolarity(trgpolarity);
99
  DRSSetTriggerLevel(trglevel);
100
  DRSSetRange (  range );
101
  DRSSetTriggerType( trgtype );
102
  DRSSetTriggerChannel(trgchannel );
103
 
104
  FILE *fp=fopen(filename,"wb");
105
 
106
 
107
  static unsigned char *buffer;
108
 
109
  int buffer_size = 0;
110
const int nBoards=1;
111
const int waveDepth=1024;
112
if (buffer_size == 0) {
113
         buffer_size =  4 +  nBoards * (4 + 4*(4+waveDepth*4));
114
         buffer_size += 24 + nBoards * (8 + 4*(4+waveDepth*2));
115
         buffer = (unsigned char *)malloc(buffer_size);
116
}
117
 
118
time_t t,told, tstart;
119
 
120
if (!DRSInit()){
121
  time(&tstart);
122
  told=tstart;
123
  int i=0;
124
  for (i=0; i<neve; i++) {
125
    start_timer(1);// 1 s timeout  
126
    int retval = DRSRead(0);
127
    stop_timer();
128
    int nb =  ( retval == 0 && fp ) ? DRSToBuffer( buffer , i ) : 0;
129
    SetCtrlVal(ph,PANEL_CEVE,i);       
130
        if (retval) i--;
131
    if (!daq_on) break;
132
    time(&t);
133
    if (t!=told ) {
200 f9daq 134
                  printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,(t-tstart), ctime(&t));
135
          }    
195 f9daq 136
    told=t;
137
// Save data    
138
        if (nb>0 && fp) fwrite(buffer, 1,nb ,fp);
139
// Plot Data
140
        for (int k=0;k<4;k++){
141
        if (! (mask & ( 0x1<<k ))  ) continue;
142
        float *t=DRSGetTime(k);
143
        float *x=DRSGetWave(k);        
144
 
200 f9daq 145
        const int col[4]={VAL_BLACK,VAL_RED,VAL_GREEN,VAL_BLUE};
146
        if (plothandle[k])  DeleteGraphPlot (ph, PANEL_GRAPH, plothandle[k], VAL_DELAYED_DRAW);
147
        plothandle[k] = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, col[k]);
148
 
149
              for (int i=0 ; i<1024 ; i++) {
195 f9daq 150
           if (verbose) printf("[%d] %d. x= %3.2f  y=%3.2f\n", k, i, t[i], x[i] );
151
           //h[k]->Fill( t[i], x[i]*1e-3);
152
        }
153
  }
154
  time(&t);
155
  printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t));
200 f9daq 156
        }
195 f9daq 157
  DRSEnd();
158
}
159
 
160
if (fp) fclose(fp);
161
 
162
free(buffer);
163
 
164
  return 0;
165
 
166
}
167
 
168
int CVICALLBACK StartCB (int panel, int control, int event,
169
                         void *callbackData, int eventData1, int eventData2) {
170
  ThreadFunctionPtr mythread = NULL;  
171
  switch (event) {
172
 
173
    case EVENT_COMMIT:
174
 
175
      mythread = daq;
176
      if (mythread!=NULL) {
177
      printf("New Thread panel=%d button=%d\n", panel, control);
178
 
179
     // SetDimming(1); 
180
      controlID= control;
181
      daq_on=1;
182
        CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID,
183
                                        DEFAULT_THREAD_PRIORITY,
184
                                        EndOfThread,
185
                                        EVENT_TP_THREAD_FUNCTION_END,
186
                                        NULL, RUN_IN_SCHEDULED_THREAD,
187
                                        &tfID);
188
    }
189
      break;
190
  }
191
  return 0;
192
}
193
 
194
int CVICALLBACK StopCB (int panel, int control, int event,
195
                        void *callbackData, int eventData1, int eventData2) {
196
  switch (event) {
197
    case EVENT_COMMIT:
198
      daq_on=0;        
199
      break;
200
  }
201
  return 0;
202
}
203
 
204
int CVICALLBACK ExitCB (int panel, int control, int event,
205
                        void *callbackData, int eventData1, int eventData2) {
206
  switch (event) {
207
    case EVENT_COMMIT:
208
      QuitUserInterface (0);
209
      break;
210
  }
211
  return 0;
212
}