b32bd23eda40bd0df0bd1581edfae67b743c5adb
[comedilib.git] / demo / ao_waveform.c
1 /*
2  * Asynchronous Analog Output Example
3  * Part of Comedilib
4  *
5  * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
6  *
7  * This file may be freely modified, distributed, and combined with
8  * other software, as long as proper attribution is given in the
9  * source code.
10  */
11
12 /*
13  * Requirements: Analog output device capable of
14  *    asynchronous commands.
15  *
16  * This demo uses an analog output subdevice with an
17  * asynchronous command to generate a waveform.  The
18  * waveform in this example is a sine wave (surprise!),
19  * but this can be easily changed to make a generic
20  * function generator.
21  *
22  * The function generation algorithm is the same as
23  * what is typically used in digital function generators.
24  * A 32-bit accumulator is incremented by a phase factor,
25  * which is the amount (in radians) that the generator
26  * advances each time step.  The accumulator is then
27  * shifted right by 20 bits, to get a 12 bit offset into
28  * a lookup table.  The value in the lookup table at
29  * that offset is then put into a buffer for output to
30  * the DAC.
31  *
32  * [ Actually, the accumulator is only 26 bits, for some
33  * reason.  I'll fix this sometime. ]
34  *
35  * On the Comedi side of things, the setup for mode 2
36  * is similar to analog input, except for the TRIG_WRITE
37  * flag.  Once you have issued the command, comedi then
38  * expects you to keep the buffer full of data to output
39  * to the DAC.  This is done by write().  Since there
40  * may be a delay between the comedi_command() and a subsequent
41  * write(), you should fill the buffer using write() before
42  * you call comedi_command(), as is done here.
43  *
44  */
45
46 #include <stdio.h>
47 #include <comedilib.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <getopt.h>
53 #include <ctype.h>
54 #include <math.h>
55 #include "examples.h"
56
57
58 /* frequency of the sine wave to output */
59 double waveform_frequency       = 100.0;
60
61 /* update rate for the DAC, typically much higher than
62    the frequency of the sine wave. */
63 double update_frequency         = 50000.0;
64
65 /* peak-to-peak amplitude, in DAC units (i.e., 0-4095) */
66 double amplitude                = 4000;
67
68 /* offset, in DAC units */
69 double offset                   = 2048;
70
71 /* This is the size of chunks we deal with when creating and
72    outputting data.  This *could* be 1, but that would be
73    inefficient */
74 #define BUF_LEN         4096
75
76 int subdevice;
77 int external_trigger_number = 0;
78
79 sampl_t data[BUF_LEN];
80
81 void dds_output(sampl_t *buf,int n);
82 void dds_init(void);
83
84 /* This define determines which waveform to use. */
85 #define dds_init_function dds_init_sine
86
87 void dds_init_sine(void);
88 void dds_init_pseudocycloid(void);
89 void dds_init_sawtooth(void);
90
91 int main(int argc, char *argv[])
92 {
93         comedi_cmd cmd;
94         int err;
95         int n,m;
96         int total=0;
97         comedi_t *dev;
98         unsigned int chanlist[1];
99
100         parse_options(argc,argv);
101
102         if(value){
103                 waveform_frequency = value;
104         }
105
106         dev = comedi_open(filename);
107
108         subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO,0);
109
110         memset(&cmd,0,sizeof(cmd));
111         cmd.subdev = subdevice;
112         cmd.flags = 0;
113         cmd.start_src = TRIG_NOW;
114         cmd.start_arg = 0;
115         cmd.scan_begin_src = TRIG_TIMER;
116         cmd.scan_begin_arg = 1e9/update_frequency;
117         cmd.convert_src = TRIG_NOW;
118         cmd.convert_arg = 0;
119         cmd.scan_end_src = TRIG_COUNT;
120         cmd.scan_end_arg = 1;
121         cmd.stop_src = TRIG_NONE;
122         cmd.stop_arg = 0;
123
124         cmd.chanlist = chanlist;
125         cmd.chanlist_len = 1;
126
127         chanlist[0] = CR_PACK(channel,range,aref);
128
129         dds_init();
130
131         dds_output(data,BUF_LEN);
132         dds_output(data,BUF_LEN);
133
134         m=write(comedi_fileno(dev),data,BUF_LEN*sizeof(sampl_t));
135         perror("write");
136         printf("m=%d\n",m);
137
138         if ((err = comedi_command(dev, &cmd)) < 0) {
139                 comedi_perror("comedi_command");
140                 exit(1);
141         }
142         while(1){
143                 dds_output(data,BUF_LEN);
144                 n=BUF_LEN*sizeof(sampl_t);
145                 while(n>0){
146                         m=write(comedi_fileno(dev),(void *)data+(BUF_LEN*sizeof(sampl_t)-n),n);
147                         if(m<0){
148                                 perror("write");
149                                 exit(0);
150                         }
151                         //printf("m=%d\n",m);
152                         n-=m;
153                 }
154                 total+=BUF_LEN;
155                 //printf("%d\n",total);
156         }
157
158         return 0;
159 }
160
161
162
163 #define WAVEFORM_SHIFT 16
164 #define WAVEFORM_LEN (1<<WAVEFORM_SHIFT)
165 #define WAVEFORM_MASK (WAVEFORM_LEN-1)
166
167
168 sampl_t waveform[WAVEFORM_LEN];
169
170 unsigned int acc;
171 unsigned int adder;
172
173 void dds_init(void)
174 {
175         int i;
176
177         adder=waveform_frequency/update_frequency*(1<<16)*(1<<WAVEFORM_SHIFT);
178
179         dds_init_function();
180
181         /* this is due to a bug in the NI-E driver */
182         if(range){
183                 for(i=0;i<WAVEFORM_LEN;i++){
184                         waveform[i]^=0x800;
185                 }
186         }
187 }
188
189 void dds_output(sampl_t *buf,int n)
190 {
191         int i;
192         sampl_t *p=buf;
193
194         for(i=0;i<n;i++){
195                 *p=waveform[(acc>>16)&WAVEFORM_MASK];
196                 p++;
197                 acc+=adder;
198         }
199 }
200
201
202 void dds_init_sine(void)
203 {
204         int i;
205
206         for(i=0;i<WAVEFORM_LEN;i++){
207                 waveform[i]=rint(offset+0.5*amplitude*cos(i*2*M_PI/WAVEFORM_LEN));
208         }
209 }
210
211 /* Yes, I know this is not the proper equation for a
212    cycloid.  Fix it. */
213 void dds_init_pseudocycloid(void)
214 {
215         int i;
216         double t;
217
218         for(i=0;i<WAVEFORM_LEN/2;i++){
219                 t=2*((double)i)/WAVEFORM_LEN;
220                 waveform[i]=rint(offset+amplitude*sqrt(1-4*t*t));
221         }
222         for(i=WAVEFORM_LEN/2;i<WAVEFORM_LEN;i++){
223                 t=2*(1-((double)i)/WAVEFORM_LEN);
224                 waveform[i]=rint(offset+amplitude*sqrt(1-t*t));
225         }
226 }
227
228 void dds_init_sawtooth(void)
229 {
230         int i;
231
232         for(i=0;i<WAVEFORM_LEN;i++){
233                 waveform[i]=rint(offset+amplitude*((double)i)/WAVEFORM_LEN);
234         }
235 }
236