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