doc/drivers.txt: Rebuilt from comedi
[comedilib.git] / demo / ao_mmap.c
index 82493e05ec6bbb5490d8d06e9699432001f5df96..b29593813543f39e942f08fe45aa336096dbaa8c 100644 (file)
 #include "examples.h"
 
 
-static int comedi_internal_trigger(comedi_t *dev, unsigned int subd, unsigned int trignum)
-{
-       comedi_insn insn;
-       lsampl_t data[1];
-
-       memset(&insn, 0, sizeof(comedi_insn));
-       insn.insn = INSN_INTTRIG;
-       insn.subdev = subd;
-       insn.data = data;
-       insn.n = 1;
-
-       data[0] = trignum;
-
-       return comedi_do_insn(dev, &insn);
-}
-
 static void write_waveform(sampl_t *buffer, int size, double amplitude, double offset, int maxdata)
 {
        int i;
-       
+
        for(i = 0; i < size; ++i)
        {
                double temp = (amplitude / 2.) * sin((2. * M_PI * i) / size) + offset;
@@ -70,60 +54,61 @@ int main(int argc, char *argv[])
 {
        comedi_cmd cmd;
        int err;
-       int n,m;
-       int total=0;
        comedi_t *dev;
        unsigned int chanlist[16];
        unsigned int maxdata;
        comedi_range *rng;
        int ret;
        int size;
+       int num_samples;
        sampl_t *map;
        /* peak-to-peak amplitude, in DAC units (i.e., 0-4095) */
        double amplitude;
        /* offset, in DAC units */
        double offset;
-       int subdevice;
+       struct parsed_options options;
 
-       
-       parse_options(argc,argv);
+       init_parsed_options(&options);
+       options.subdevice = -1;
+       parse_options(&options, argc, argv);
 
        /* Force n_chan to be 1 */
-       n_chan = 1;
+       options.n_chan = 1;
 
-       dev = comedi_open(filename);
+       dev = comedi_open(options.filename);
        if(dev == NULL){
-               fprintf(stderr, "error opening %s\n", filename);
+               fprintf(stderr, "error opening %s\n", options.filename);
                return -1;
        }
-       subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO,0);
+       if(options.subdevice < 0)
+               options.subdevice = comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO, 0);
 
-       maxdata = comedi_get_maxdata(dev,subdevice,0);
-       rng = comedi_get_range(dev,subdevice,0,0);
+       maxdata = comedi_get_maxdata(dev, options.subdevice, 0);
+       rng = comedi_get_range(dev, options.subdevice, 0, 0);
 
        offset = (double)comedi_from_phys(0.0, rng, maxdata);
        amplitude = (double)comedi_from_phys(1.0, rng, maxdata) - offset;
 
        memset(&cmd,0,sizeof(cmd));
-       cmd.subdev = subdevice;
+       cmd.subdev = options.subdevice;
        cmd.flags = 0;
        cmd.start_src = TRIG_INT;
        cmd.start_arg = 0;
        cmd.scan_begin_src = TRIG_TIMER;
-       cmd.scan_begin_arg = 1e9/freq;
+       cmd.scan_begin_arg = 1e9 / options.freq;
        cmd.convert_src = TRIG_NOW;
        cmd.convert_arg = 0;
        cmd.scan_end_src = TRIG_COUNT;
-       cmd.scan_end_arg = n_chan;
+       cmd.scan_end_arg = options.n_chan;
        cmd.stop_src = TRIG_NONE;
        cmd.stop_arg = 0;
 
        cmd.chanlist = chanlist;
-       cmd.chanlist_len = n_chan;
+       cmd.chanlist_len = options.n_chan;
 
-       chanlist[0] = CR_PACK(channel,range,aref);
+       chanlist[0] = CR_PACK(options.channel, options.range, options.aref);
 
-       dump_cmd(stdout,&cmd);
+       dump_cmd(stdout, &cmd);
 
        err = comedi_command_test(dev, &cmd);
        if (err < 0) {
@@ -141,8 +126,8 @@ int main(int argc, char *argv[])
                comedi_perror("comedi_command");
                exit(1);
        }
-       
-       size = comedi_get_buffer_size(dev, subdevice);
+
+       size = comedi_get_buffer_size(dev, options.subdevice);
        fprintf(stderr, "buffer size is %d\n", size);
        map = mmap(NULL, size, PROT_WRITE, MAP_SHARED, comedi_fileno(dev), 0);
        if(map == MAP_FAILED)
@@ -150,31 +135,45 @@ int main(int argc, char *argv[])
                perror("mmap");
                exit(1);
        }
-       write_waveform(map, size, amplitude, offset, maxdata);
+       num_samples = size / sizeof(sampl_t);
+       write_waveform(map, num_samples, amplitude, offset, maxdata);
        if(msync(map, size, MS_SYNC) < 0)
        {
                perror("msync");
                exit(1);
        }
-       ret = comedi_internal_trigger(dev, subdevice, 0);
+       printf("marking %i samples as written\n", num_samples);
+       ret = comedi_mark_buffer_written(dev, options.subdevice, size);
+       if(ret < 0)
+       {
+               comedi_perror("comedi_mark_buffer_written");
+               exit(1);
+       }
+       ret = comedi_internal_trigger(dev, options.subdevice, 0);
        if(ret<0){
-               comedi_perror("comedi_internal_trigger\n");
+               comedi_perror("comedi_internal_trigger");
                exit(1);
        }
-       while(1){
-               int bytes_marked = comedi_get_buffer_contents(dev,subdevice);
-               if(bytes_marked < 1)
+       while(1)
+       {
+               int bytes_marked = comedi_get_buffer_contents(dev, options.subdevice);
+               int bytes_unmarked = size - bytes_marked;
+               if(bytes_marked < 0)
                {
                        comedi_perror("comedi_get_buffer_contents");
                        exit(1);
                }
-               int bytes_unmarked = size - bytes_marked;
-               // this keeps comedi from reporting a buffer underrun
-               if(comedi_mark_buffer_written(dev, subdevice, bytes_unmarked) < 0)
+               if(bytes_unmarked > 0)
                {
-                       comedi_perror("comedi_mark_buffer_written");
-                       exit(1);
-               }
+                       // this keeps comedi from reporting a buffer underrun
+                       if(comedi_mark_buffer_written(dev, options.subdevice, bytes_unmarked) < 0)
+                       {
+                               comedi_perror("comedi_mark_buffer_written");
+                               exit(1);
+                       }
+                       printf("marked %lui more samples as written\n", (unsigned long)bytes_unmarked / sizeof(sampl_t));
+               }else
+                       usleep(10000);
        }
        return 0;
 }