Rev 197 | Rev 200 | 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; |
||
9 | static int ph, plothandle; |
||
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 ) { |
||
134 | printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,(t-tstart), ctime(&t)); |
||
135 | |||
136 | } |
||
137 | told=t; |
||
138 | // Save data |
||
139 | if (nb>0 && fp) fwrite(buffer, 1,nb ,fp); |
||
140 | // Plot Data |
||
141 | for (int k=0;k<4;k++){ |
||
142 | if (! (mask & ( 0x1<<k )) ) continue; |
||
143 | float *t=DRSGetTime(k); |
||
144 | float *x=DRSGetWave(k); |
||
145 | |||
146 | if (!k){ |
||
147 | if (plothandle) DeleteGraphPlot (ph, PANEL_GRAPH, plothandle, VAL_DELAYED_DRAW); |
||
148 | plothandle = PlotXY (ph, PANEL_GRAPH, t, x, 1024, VAL_FLOAT, VAL_FLOAT, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, VAL_BLUE); |
||
149 | } |
||
150 | |||
151 | |||
152 | for (int i=0 ; i<1024 ; i++) { |
||
153 | if (verbose) printf("[%d] %d. x= %3.2f y=%3.2f\n", k, i, t[i], x[i] ); |
||
154 | //h[k]->Fill( t[i], x[i]*1e-3); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | time(&t); |
||
159 | printf("%d events in %2.2f min (%d s) %s",i+1, (double)(t-tstart)/60.,t-tstart, ctime(&t)); |
||
160 | |||
161 | DRSEnd(); |
||
162 | } |
||
163 | |||
164 | if (fp) fclose(fp); |
||
165 | |||
166 | free(buffer); |
||
167 | |||
168 | return 0; |
||
169 | |||
170 | } |
||
171 | |||
172 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
173 | void *callbackData, int eventData1, int eventData2) { |
||
174 | ThreadFunctionPtr mythread = NULL; |
||
175 | switch (event) { |
||
176 | |||
177 | case EVENT_COMMIT: |
||
178 | |||
179 | mythread = daq; |
||
180 | if (mythread!=NULL) { |
||
181 | printf("New Thread panel=%d button=%d\n", panel, control); |
||
182 | |||
183 | // SetDimming(1); |
||
184 | controlID= control; |
||
185 | daq_on=1; |
||
186 | CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID, |
||
187 | DEFAULT_THREAD_PRIORITY, |
||
188 | EndOfThread, |
||
189 | EVENT_TP_THREAD_FUNCTION_END, |
||
190 | NULL, RUN_IN_SCHEDULED_THREAD, |
||
191 | &tfID); |
||
192 | } |
||
193 | break; |
||
194 | } |
||
195 | return 0; |
||
196 | } |
||
197 | |||
198 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
199 | void *callbackData, int eventData1, int eventData2) { |
||
200 | switch (event) { |
||
201 | case EVENT_COMMIT: |
||
202 | daq_on=0; |
||
203 | break; |
||
204 | } |
||
205 | return 0; |
||
206 | } |
||
207 | |||
208 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
209 | void *callbackData, int eventData1, int eventData2) { |
||
210 | switch (event) { |
||
211 | case EVENT_COMMIT: |
||
212 | QuitUserInterface (0); |
||
213 | break; |
||
214 | } |
||
215 | return 0; |
||
216 | } |