source: multiio/pcmmio/original/repeat.c @ 13bdbee

base
Last change on this file since 13bdbee was 2bae2aa, checked in by Joel Sherrill <joel.sherrill@…>, on 06/08/09 at 14:52:43

Initial import.

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