1 | /* repeat.c |
---|
2 | * Demonstration program for WinSystems PCM-MIO Driver |
---|
3 | * |
---|
4 | * $Id$ |
---|
5 | * |
---|
6 | * Compile for GNU/Linux with: |
---|
7 | * |
---|
8 | * gcc repeat.c kbhit.c mio_io.o -o repeat |
---|
9 | * |
---|
10 | * This program demonstrates the adc_convert_single_repeated function |
---|
11 | * which allows for a sequence of conversions to be accomplished |
---|
12 | * with a single call on the same channel. |
---|
13 | */ |
---|
14 | |
---|
15 | #include "mio_io.h" |
---|
16 | |
---|
17 | #include <stdio.h> |
---|
18 | #include <fcntl.h> /* open */ |
---|
19 | #include <unistd.h> /* exit */ |
---|
20 | #include <sys/ioctl.h> /* ioctl */ |
---|
21 | #include <stdlib.h> |
---|
22 | #include <pthread.h> |
---|
23 | |
---|
24 | // This array will store the results of 2000 conversions |
---|
25 | |
---|
26 | unsigned short values[2000]; |
---|
27 | volatile unsigned long count = 0; |
---|
28 | |
---|
29 | void *thread_function(void *arg); |
---|
30 | void *thread_function2(void *arg); |
---|
31 | |
---|
32 | /* Event count, counts the number of events we've handled */ |
---|
33 | |
---|
34 | volatile int exit_flag = 0; |
---|
35 | |
---|
36 | void init_keyboard(void); |
---|
37 | void close_keyboard(void); |
---|
38 | int kbhit(void); |
---|
39 | int readch(void); |
---|
40 | |
---|
41 | |
---|
42 | int main(int argc, char* argv[]) |
---|
43 | { |
---|
44 | int channel = 0; |
---|
45 | unsigned short result; |
---|
46 | float min,max,current; |
---|
47 | int x,c; |
---|
48 | int res; |
---|
49 | int res2; |
---|
50 | pthread_t a_thread; |
---|
51 | pthread_t b_thread; |
---|
52 | |
---|
53 | // We'll keep track of the minimum and maximum voltage values |
---|
54 | // we see on a channel as well as the count of conversions |
---|
55 | // completed. |
---|
56 | |
---|
57 | max = -10.0; |
---|
58 | min = 10.0; |
---|
59 | count = 0; |
---|
60 | |
---|
61 | // The default channel is 0 but an alternate channel can be specified |
---|
62 | // as a command line argument. |
---|
63 | |
---|
64 | if(argc > 1) |
---|
65 | channel = atoi(argv[1]); |
---|
66 | |
---|
67 | // Check for a valid channel number. Abort if bad |
---|
68 | |
---|
69 | if(channel <0 || channel > 15) |
---|
70 | { |
---|
71 | printf("Channel numbers must be between 0 and 15 - Aborting\n"); |
---|
72 | exit(0); |
---|
73 | } |
---|
74 | |
---|
75 | // This call sets the mode for the specified channel. We are going to |
---|
76 | // set up for single-ended +/- 10 V range. That way any legal input |
---|
77 | // can be accomodated. |
---|
78 | |
---|
79 | adc_set_channel_mode(channel,ADC_SINGLE_ENDED,ADC_BIPOLAR,ADC_TOP_10V); |
---|
80 | |
---|
81 | if(mio_error_code) |
---|
82 | { |
---|
83 | printf("\nError occured - %s\n",mio_error_string); |
---|
84 | exit(1); |
---|
85 | } |
---|
86 | |
---|
87 | /* Enable interrupts on both controllers */ |
---|
88 | |
---|
89 | enable_adc_interrupt(0); |
---|
90 | enable_adc_interrupt(1); |
---|
91 | |
---|
92 | if(mio_error_code) |
---|
93 | { |
---|
94 | printf("\nError occured - %s\n",mio_error_string); |
---|
95 | exit(1); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | res = pthread_create(&a_thread,NULL,thread_function,NULL); |
---|
100 | |
---|
101 | if(res != 0) |
---|
102 | { |
---|
103 | perror("Thread 1 creation failed"); |
---|
104 | exit(EXIT_FAILURE); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | res2 = pthread_create(&b_thread,NULL,thread_function2,NULL); |
---|
109 | |
---|
110 | if(res != 0) |
---|
111 | { |
---|
112 | perror("Thread 2 creation failed"); |
---|
113 | exit(EXIT_FAILURE); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | init_keyboard(); |
---|
119 | |
---|
120 | while(1) |
---|
121 | { |
---|
122 | // We'll keep running until a recognized key is pressed |
---|
123 | if(kbhit()) |
---|
124 | { |
---|
125 | c = readch(); |
---|
126 | |
---|
127 | // The 'C' key clears the min/max and count values |
---|
128 | |
---|
129 | if(c== 'c' || c == 'C') |
---|
130 | { |
---|
131 | count = 0; |
---|
132 | min = 10.0; |
---|
133 | max = -10.0; |
---|
134 | } |
---|
135 | |
---|
136 | // The 'N' key moves to the next channel, wrapping from 15 |
---|
137 | // back to 0 when appropriate. |
---|
138 | |
---|
139 | else if(c == 'n' || c == 'N') |
---|
140 | { |
---|
141 | printf("\n"); |
---|
142 | channel++; |
---|
143 | if(channel > 15) |
---|
144 | channel = 0; |
---|
145 | |
---|
146 | // When we change channels we need to make sure we set |
---|
147 | // the channel's mode to a valid range. |
---|
148 | |
---|
149 | adc_set_channel_mode(channel,ADC_SINGLE_ENDED,ADC_BIPOLAR,ADC_TOP_10V); |
---|
150 | if(mio_error_code) |
---|
151 | { |
---|
152 | printf("\nError occured - %s\n",mio_error_string); |
---|
153 | exit(1); |
---|
154 | } |
---|
155 | |
---|
156 | // A new channel also clears the count and min/max values. |
---|
157 | |
---|
158 | count = 0; |
---|
159 | min = 10.0; |
---|
160 | max = -10.0; |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | disable_adc_interrupt(0); |
---|
165 | disable_adc_interrupt(1); |
---|
166 | pthread_cancel(a_thread); |
---|
167 | exit_flag = 1; |
---|
168 | close_keyboard(); |
---|
169 | printf("\n\n"); |
---|
170 | exit(0); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | // Finally the real thing. This function-call results in 2000 |
---|
175 | // conversions on the specified channel with the results going into |
---|
176 | // a buffer called "values". |
---|
177 | |
---|
178 | adc_convert_single_repeated(channel,2000,values); |
---|
179 | |
---|
180 | if(mio_error_code) |
---|
181 | { |
---|
182 | printf("\nError occured - %s\n",mio_error_string); |
---|
183 | exit(1); |
---|
184 | } |
---|
185 | |
---|
186 | // Bump up the count |
---|
187 | |
---|
188 | ; count += 2000; |
---|
189 | |
---|
190 | // Now we'll read out the 2000 conversion values. Convert them to |
---|
191 | // floating point voltages and set the min and max values as appropriate |
---|
192 | |
---|
193 | for(x=0; x<2000; x++) |
---|
194 | { |
---|
195 | result = values[x]; |
---|
196 | |
---|
197 | current = adc_convert_to_volts(channel,result); |
---|
198 | |
---|
199 | // Check and load the min/max values as needed |
---|
200 | |
---|
201 | if(current < min) |
---|
202 | min = current; |
---|
203 | |
---|
204 | if(current > max) |
---|
205 | max = current; |
---|
206 | |
---|
207 | // Print the values |
---|
208 | |
---|
209 | printf("CH %02d %09ld %9.5f Min =%9.5f Max =%9.5f\r",channel,count,current,min,max); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | return 0; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | void *thread_function(void *arg) |
---|
220 | { |
---|
221 | int c; |
---|
222 | |
---|
223 | while(1) |
---|
224 | { |
---|
225 | pthread_testcancel(); |
---|
226 | |
---|
227 | if(exit_flag) |
---|
228 | break; |
---|
229 | |
---|
230 | /* This call will put THIS process to sleep until either an |
---|
231 | interrupt occurs or a terminating signal is sent by the |
---|
232 | parent or the system. |
---|
233 | */ |
---|
234 | c = wait_adc_int(0); |
---|
235 | |
---|
236 | /* We check to see if it was a real interrupt instead of a |
---|
237 | termination request. |
---|
238 | */ |
---|
239 | if(c == 0) |
---|
240 | { |
---|
241 | #ifdef DEBUG |
---|
242 | printf("Interrupt occured on ADC 0\n"); |
---|
243 | #endif |
---|
244 | ++count; |
---|
245 | |
---|
246 | } |
---|
247 | else |
---|
248 | break; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | void *thread_function2(void *arg) |
---|
255 | { |
---|
256 | int c; |
---|
257 | |
---|
258 | while(1) |
---|
259 | { |
---|
260 | pthread_testcancel(); |
---|
261 | |
---|
262 | if(exit_flag) |
---|
263 | break; |
---|
264 | |
---|
265 | /* This call will put THIS process to sleep until either an |
---|
266 | interrupt occurs or a terminating signal is sent by the |
---|
267 | parent or the system. |
---|
268 | */ |
---|
269 | c = wait_adc_int(1); |
---|
270 | |
---|
271 | /* We check to see if it was a real interrupt instead of a |
---|
272 | termination request. |
---|
273 | */ |
---|
274 | if(c == 0) |
---|
275 | { |
---|
276 | #ifdef DEBUG |
---|
277 | printf("Interrupt occured on ADC 1\n"); |
---|
278 | #endif |
---|
279 | ++count; |
---|
280 | |
---|
281 | } |
---|
282 | else |
---|
283 | break; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|