From: Frank Mori Hess Date: Wed, 3 Jan 2007 21:14:53 +0000 (+0000) Subject: Made parse_options() write options to a struct instead of passing X-Git-Tag: v0_8_0~43 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=fbdf65b4f517ad0b93a15204417147a0a9202ebd;p=comedilib.git Made parse_options() write options to a struct instead of passing them through global variables. --- diff --git a/demo/antialias.c b/demo/antialias.c index 1f1fb1e..08c6ec4 100644 --- a/demo/antialias.c +++ b/demo/antialias.c @@ -47,28 +47,30 @@ int main(int argc, char *argv[]) { lsampl_t data; int ret; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); + comedi_perror(options.filename); exit(0); } - data = value; - if(verbose){ + data = options.value; + if(options.verbose){ printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", - data,filename,subdevice,channel,range,aref); + data, options.filename, options.subdevice, options.channel, options.range, options.aref); } - ret=comedi_data_write(device,subdevice,channel,range,aref,data); + ret = comedi_data_write(device, options.subdevice, options.channel, options.range, options.aref, data); if(ret<0){ - comedi_perror(filename); + comedi_perror(options.filename); exit(0); } - printf("%d\n",data); + printf("%d\n", data); ao_antialias((1000<<16)+1000); diff --git a/demo/ao_mmap.c b/demo/ao_mmap.c index 5da1464..50ea279 100644 --- a/demo/ao_mmap.c +++ b/demo/ao_mmap.c @@ -56,7 +56,7 @@ static int comedi_internal_trigger(comedi_t *dev, unsigned int subd, unsigned in 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; @@ -82,47 +82,49 @@ int main(int argc, char *argv[]) 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) { @@ -140,8 +142,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) @@ -157,20 +159,20 @@ int main(int argc, char *argv[]) exit(1); } printf("marking %i samples as written\n", num_samples); - ret = comedi_mark_buffer_written(dev, subdevice, size); + 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, subdevice, 0); + ret = comedi_internal_trigger(dev, options.subdevice, 0); if(ret<0){ comedi_perror("comedi_internal_trigger"); exit(1); } while(1) { - int bytes_marked = comedi_get_buffer_contents(dev,subdevice); + int bytes_marked = comedi_get_buffer_contents(dev, options.subdevice); int bytes_unmarked = size - bytes_marked; if(bytes_marked < 0) { @@ -180,7 +182,7 @@ int main(int argc, char *argv[]) if(bytes_unmarked > 0) { // this keeps comedi from reporting a buffer underrun - if(comedi_mark_buffer_written(dev, subdevice, bytes_unmarked) < 0) + if(comedi_mark_buffer_written(dev, options.subdevice, bytes_unmarked) < 0) { comedi_perror("comedi_mark_buffer_written"); exit(1); diff --git a/demo/ao_waveform.c b/demo/ao_waveform.c index 0e9f6bc..2c24895 100644 --- a/demo/ao_waveform.c +++ b/demo/ao_waveform.c @@ -70,13 +70,12 @@ double offset = 2048; inefficient */ #define BUF_LEN 0x8000 -int subdevice; int external_trigger_number = 0; sampl_t data[BUF_LEN]; void dds_output(sampl_t *buf,int n); -void dds_init(void); +void dds_init(double waveform_frequency, double update_frequency); /* This define determines which waveform to use. */ #define dds_init_function dds_init_sine @@ -113,50 +112,54 @@ int main(int argc, char *argv[]) unsigned int maxdata; comedi_range *rng; int ret; + 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; - if(value){ - waveform_frequency = value; + if(options.value){ + waveform_frequency = options.value; } - 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; + 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[1] = CR_PACK(channel+1,range,aref); + chanlist[0] = CR_PACK(options.channel, options.range, options.aref); + chanlist[1] = CR_PACK(options.channel + 1, options.range, options.aref); - dds_init(); + dds_init(waveform_frequency, options.freq); dump_cmd(stdout,&cmd); @@ -191,8 +194,8 @@ int main(int argc, char *argv[]) } printf("m=%d\n",m); - ret = comedi_internal_trigger(dev, subdevice, 0); - if(ret<0){ + ret = comedi_internal_trigger(dev, options.subdevice, 0); + if(ret < 0){ perror("comedi_internal_trigger\n"); exit(1); } @@ -228,9 +231,9 @@ sampl_t waveform[WAVEFORM_LEN]; unsigned int acc; unsigned int adder; -void dds_init(void) +void dds_init(double waveform_frequency, double update_frequency) { - adder=waveform_frequency/freq*(1<<16)*(1< @@ -29,24 +29,26 @@ int main(int argc, char *argv[]) { lsampl_t data; int ret; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device=comedi_open(filename); + device=comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - data = value; - if(verbose){ + data = options.value; + if(options.verbose){ printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", - data,filename,subdevice,channel,range,aref); + data, options.filename, options.subdevice, options.channel, options.range, options.aref); } - ret=comedi_apply_calibration(device,subdevice,channel,range,aref,NULL); - if(ret<0){ - comedi_perror(filename); + ret = comedi_apply_calibration(device, options.subdevice, options.channel, options.range, options.aref, NULL); + if(ret < 0){ + comedi_perror(options.filename); exit(0); } diff --git a/demo/choose_clock.c b/demo/choose_clock.c index eae42f3..58ed44f 100644 --- a/demo/choose_clock.c +++ b/demo/choose_clock.c @@ -34,20 +34,24 @@ int main(int argc, char *argv[]) { unsigned period_ns; int retval; + lsampl_t clock_selection; + struct parsed_options options; - freq = 0.; - parse_options(argc,argv); + init_parsed_options(&options); + options.freq = 0.; + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - if(freq > 0.) - period_ns = 1e9 / freq; + if(options.freq > 0.) + period_ns = 1e9 / options.freq; else period_ns = 0; - printf("Selecting master clock %d on subdevice %d.\n", value, subdevice); + clock_selection = options.value; + printf("Selecting master clock %d on subdevice %d.\n", clock_selection, options.subdevice); if(period_ns) { printf("Clock period = %d nanoseconds.\n", period_ns); @@ -59,11 +63,11 @@ int main(int argc, char *argv[]) lsampl_t data[3]; memset(&insn, 0, sizeof(comedi_insn)); insn.insn = INSN_CONFIG; - insn.subdev = subdevice; + insn.subdev = options.subdevice; insn.data = data; insn.n = sizeof(data) / sizeof(data[0]); data[0] = INSN_CONFIG_SET_CLOCK_SRC; - data[1] = value; + data[1] = clock_selection; data[2] = period_ns; retval = comedi_do_insn(device, &insn); diff --git a/demo/choose_routing.c b/demo/choose_routing.c index 4e137b6..1aa6b10 100644 --- a/demo/choose_routing.c +++ b/demo/choose_routing.c @@ -34,30 +34,34 @@ int main(int argc, char *argv[]) { unsigned period_ns; int retval; + lsampl_t routing; + struct parsed_options options; - freq = 0.; - parse_options(argc,argv); + init_parsed_options(&options); + options.freq = 0.; + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - if(freq > 0.) - period_ns = 1e9 / freq; + if(options.freq > 0.) + period_ns = 1e9 / options.freq; else period_ns = 0; - printf("Selecting routing %d for channel %d on subdevice %d.\n", value, channel, subdevice); + routing = options.value; + printf("Selecting routing %d for channel %d on subdevice %d.\n", routing, options.channel, options.subdevice); comedi_insn insn; lsampl_t data[2]; memset(&insn, 0, sizeof(comedi_insn)); insn.insn = INSN_CONFIG; - insn.subdev = subdevice; - insn.chanspec = channel; + insn.subdev = options.subdevice; + insn.chanspec = options.channel; insn.data = data; insn.n = sizeof(data) / sizeof(data[0]); data[0] = INSN_CONFIG_SET_ROUTING; - data[1] = value; + data[1] = routing; retval = comedi_do_insn(device, &insn); if(retval < 0) comedi_perror("comedi_do_insn"); diff --git a/demo/cmd.c b/demo/cmd.c index ee0ad8c..51b7e3c 100644 --- a/demo/cmd.c +++ b/demo/cmd.c @@ -34,12 +34,12 @@ static comedi_range * range_info[N_CHANS]; static lsampl_t maxdata[N_CHANS]; -int prepare_cmd_lib(comedi_t *dev,int subdevice,comedi_cmd *cmd); -int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd); +int prepare_cmd_lib(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd); +int prepare_cmd_lib(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd); void do_cmd(comedi_t *dev,comedi_cmd *cmd); -void print_datum(lsampl_t raw, int i); +void print_datum(lsampl_t raw, int channel_index, short physical); char *cmdtest_messages[]={ "success", @@ -60,26 +60,28 @@ int main(int argc, char *argv[]) struct timeval start,end; int subdev_flags; lsampl_t raw; - - parse_options(argc,argv); + struct parsed_options options; - /* The following global variables used in this demo are - * defined in common.c, and can be modified by command line + init_parsed_options(&options); + parse_options(&options, argc, argv); + + /* The following variables used in this demo + * can be modified by command line * options. When modifying this demo, you may want to * change them here. */ - //filename = "/dev/comedi0"; - //subdevice = 0; - //channel = 0; - //range = 0; - //aref = AREF_GROUND; - //n_chan = 4; - //n_scan = 1000; - //freq = 1000.0; + //options.filename = "/dev/comedi0"; + //options.subdevice = 0; + //options.channel = 0; + //options.range = 0; + //options.aref = AREF_GROUND; + //options.n_chan = 4; + //options.n_scan = 1000; + //options.freq = 1000.0; /* open the device */ - dev = comedi_open(filename); + dev = comedi_open(options.filename); if(!dev){ - comedi_perror(filename); + comedi_perror(options.filename); exit(1); } @@ -87,20 +89,20 @@ int main(int argc, char *argv[]) comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); /* Set up channel list */ - for(i=0;ichanlist = chanlist; - cmd->chanlist_len = n_chan; + cmd->chanlist = chanlist; + cmd->chanlist_len = n_chan; cmd->scan_end_arg = n_chan; - if(cmd->stop_src==TRIG_COUNT)cmd->stop_arg = n_scan; + if(cmd->stop_src == TRIG_COUNT) cmd->stop_arg = n_scan; return 0; } @@ -230,7 +232,7 @@ int prepare_cmd_lib(comedi_t *dev,int subdevice,comedi_cmd *cmd) * Set up a command by hand. This will not work on some devices. * There is no single command that will work on all devices. */ -int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd) +int prepare_cmd(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd) { memset(cmd,0,sizeof(*cmd)); @@ -242,7 +244,7 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd) /* Wake up at the end of every scan */ //cmd->flags |= TRIG_WAKE_EOS; - + /* Use a real-time interrupt, if available */ //cmd->flags |= TRIG_RT; @@ -283,7 +285,7 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd) * by the device, but it will be adjusted to the nearest supported * value by comedi_command_test(). */ cmd->scan_begin_src = TRIG_TIMER; - cmd->scan_begin_arg = 1e9/freq; /* in ns */ + cmd->scan_begin_arg = period_nanosec; /* in ns */ /* The timing between each sample in a scan is controlled by convert. * TRIG_TIMER: Conversion events occur periodically. @@ -325,12 +327,12 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd) return 0; } -void print_datum(lsampl_t raw, int i) { +void print_datum(lsampl_t raw, int channel_index, short physical) { double physical_value; if(!physical) { printf("%d ",raw); } else { - physical_value = comedi_to_phys(raw,range_info[i],maxdata[i]); + physical_value = comedi_to_phys(raw, range_info[channel_index], maxdata[channel_index]); printf("%#8.6g ",physical_value); } } diff --git a/demo/common.c b/demo/common.c index c9cef46..c22470b 100644 --- a/demo/common.c +++ b/demo/common.c @@ -15,69 +15,67 @@ #include #include "examples.h" +static char * const default_filename = "/dev/comedi0"; -char *filename="/dev/comedi0"; -int verbose = 0; - -int value=0; -int subdevice=0; -int channel=0; -int aref=AREF_GROUND; -int range=0; -int n_chan=4; -int n_scan=1000; -double freq=1000.0; -int physical = 0; - +void init_parsed_options(struct parsed_options *options) +{ + memset(options, 0, sizeof(struct parsed_options)); + options->filename = default_filename; + options->aref = AREF_GROUND; + options->n_chan = 4; + options->n_scan = 1000; + options->freq = 1000.0; + options->physical = 0; + options->value = 0.; +} -int parse_options(int argc, char *argv[]) +int parse_options(struct parsed_options *options, int argc, char *argv[]) { int c; - while (-1 != (c = getopt(argc, argv, "a:c:s:r:f:n:N:F:pvdgom"))) { switch (c) { case 'f': - filename = optarg; + options->filename = optarg; break; case 's': - subdevice = strtoul(optarg,NULL,0); + options->subdevice = strtoul(optarg, NULL, 0); break; case 'c': - channel = strtoul(optarg,NULL,0); + options->channel = strtoul(optarg, NULL, 0); break; case 'a': - aref = strtoul(optarg,NULL,0); + options->aref = strtoul(optarg, NULL, 0); break; case 'r': - range = strtoul(optarg,NULL,0); + options->range = strtoul(optarg, NULL, 0); break; case 'n': - n_chan = strtoul(optarg,NULL,0); + options->n_chan = strtoul(optarg, NULL, 0); break; case 'N': - n_scan = strtoul(optarg,NULL,0); + options->n_scan = strtoul(optarg, NULL, 0); break; case 'F': - freq = strtoul(optarg,NULL,0); + options->freq = strtod(optarg, NULL); break; case 'p': - physical = 1; + options->physical = 1; break; case 'v': - verbose = 1; + ++options->verbose; break; case 'd': - aref = AREF_DIFF; + options->aref = AREF_DIFF; break; case 'g': - aref = AREF_GROUND; + options->aref = AREF_GROUND; break; case 'o': - aref = AREF_OTHER; + options->aref = AREF_OTHER; break; case 'm': - aref = AREF_COMMON; + options->aref = AREF_COMMON; break; default: printf("bad option\n"); @@ -86,7 +84,7 @@ int parse_options(int argc, char *argv[]) } if(optind < argc) { /* data value */ - sscanf(argv[optind++],"%d",&value); + options->value = strtod(argv[optind++], NULL); } return argc; diff --git a/demo/dio.c b/demo/dio.c index 51425af..5c39117 100644 --- a/demo/dio.c +++ b/demo/dio.c @@ -31,30 +31,32 @@ int main(int argc, char *argv[]) { int ret; int stype; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - stype = comedi_get_subdevice_type(device,subdevice); - if(stype!=COMEDI_SUBD_DIO){ - printf("%d is not a digital I/O subdevice\n",subdevice); - exit(0); + stype = comedi_get_subdevice_type(device, options.subdevice); + if(stype != COMEDI_SUBD_DIO){ + printf("%d is not a digital I/O subdevice\n", options.subdevice); + exit(-1); } - printf("configuring pin %d or subdevice %d ", channel, subdevice); - if(value) + printf("configuring pin %d or subdevice %d ", options.channel, options.subdevice); + if(options.value) { printf("for output.\n"); - ret=comedi_dio_config(device,subdevice,channel, COMEDI_OUTPUT); + ret = comedi_dio_config(device, options.subdevice, options.channel, COMEDI_OUTPUT); }else { printf("for input.\n"); - ret=comedi_dio_config(device,subdevice,channel, COMEDI_INPUT); + ret = comedi_dio_config(device, options.subdevice, options.channel, COMEDI_INPUT); } return 0; } diff --git a/demo/eeprom_dump.c b/demo/eeprom_dump.c index 3168666..f104d97 100644 --- a/demo/eeprom_dump.c +++ b/demo/eeprom_dump.c @@ -12,7 +12,7 @@ #include #include "examples.h" -int read_eeprom(comedi_t *it,unsigned int **eeprom); +int read_eeprom(comedi_t *it,unsigned int **eeprom, struct parsed_options options); void dump_eeprom(unsigned int *eeprom,int len); comedi_t *device; @@ -21,16 +21,19 @@ int main(int argc, char *argv[]) { int len; unsigned int *eeprom; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + options.subdevice = -1; + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - len=read_eeprom(device,&eeprom); + len = read_eeprom(device, &eeprom, options); dump_eeprom(eeprom,len); return 0; @@ -39,33 +42,36 @@ int main(int argc, char *argv[]) -int read_eeprom(comedi_t *it,unsigned int **eeprom) +int read_eeprom(comedi_t *it, unsigned int **eeprom, struct parsed_options options) { - int subd; int n,i,ret; lsampl_t data; unsigned int *ptr; lsampl_t maxdata; - subd=comedi_find_subdevice_by_type(it,COMEDI_SUBD_MEMORY,0); - if(subd<0){ - fprintf(stderr,"No memory subdevice\n"); - return 0; + if(options.subdevice < 0) + { + options.subdevice = comedi_find_subdevice_by_type(it, COMEDI_SUBD_MEMORY, 0); + if(options.subdevice < 0){ + fprintf(stderr,"No memory subdevice\n"); + return 0; + } } - n=comedi_get_n_channels(it,subd); - maxdata=comedi_get_maxdata(it,subd,0); + n = comedi_get_n_channels(it, options.subdevice); + maxdata = comedi_get_maxdata(it, options.subdevice, 0); - if(maxdata!=0xff){ - fprintf(stderr,"Memory subdevice has strange maxdata, aborting\n"); + if(maxdata != 0xff){ + fprintf(stderr,"Demo only supports 8-bit memory subdevice has strange maxdata, aborting\n"); + exit(-1); } - ptr=malloc(sizeof(unsigned int)*n); + ptr = malloc(sizeof(unsigned int) * n); - for(i=0;i>=1) *t++=(bits&bit)?'1':'0'; *t=0; - + return s; } @@ -137,7 +139,7 @@ void get_command_stuff(comedi_t *it,int s) printf(" convert: %s\n",cmd_src(cmd.convert_src,buf)); printf(" scan_end: %s\n",cmd_src(cmd.scan_end_src,buf)); printf(" stop: %s\n",cmd_src(cmd.stop_src,buf)); - + probe_max_1chan(it,s); } } diff --git a/demo/inp.c b/demo/inp.c index bd8baea..1719ed3 100644 --- a/demo/inp.c +++ b/demo/inp.c @@ -33,40 +33,42 @@ int main(int argc, char *argv[]) comedi_range * range_info; lsampl_t maxdata; double physical_value; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - if(verbose){ + if(options.verbose){ printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", - filename,subdevice,channel,range,aref); + options.filename, options.subdevice, options.channel, options.range, options.aref); } - ret=comedi_data_read(device,subdevice,channel,range,aref,&data); - if(ret<0){ - comedi_perror(filename); - exit(0); + ret = comedi_data_read(device, options.subdevice, options.channel, options.range, options.aref, &data); + if(ret < 0){ + comedi_perror(options.filename); + exit(-1); } - if(physical) { + if(options.physical) { comedi_set_global_oor_behavior(COMEDI_OOR_NAN); - range_info = comedi_get_range(device,subdevice,channel,range); - maxdata = comedi_get_maxdata(device,subdevice,channel); - if(verbose) { + range_info = comedi_get_range(device, options.subdevice, options.channel, options.range); + maxdata = comedi_get_maxdata(device, options.subdevice, options.channel); + if(options.verbose) { printf("[0,%d] -> [%g,%g]\n", maxdata, range_info->min, range_info->max); } - physical_value = comedi_to_phys(data,range_info,maxdata); + physical_value = comedi_to_phys(data, range_info, maxdata); if(isnan(physical_value)) { printf("Out of range [%g,%g]", range_info->min, range_info->max); } else { - printf("%g",physical_value); + printf("%g", physical_value); switch(range_info->unit) { case UNIT_volt: printf(" V"); break; case UNIT_mA: printf(" mA"); break; @@ -74,7 +76,7 @@ int main(int argc, char *argv[]) default: printf(" (unknown unit %d)", range_info->unit); } - if(verbose) { + if(options.verbose) { printf(" (%d raw units)", data); } } diff --git a/demo/inpn.c b/demo/inpn.c index 2676810..82e6e2f 100644 --- a/demo/inpn.c +++ b/demo/inpn.c @@ -36,36 +36,40 @@ int main(int argc, char *argv[]) int maxdata; lsampl_t data; double voltage; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + options.subdevice = -1; + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - subdevice=comedi_find_subdevice_by_type(device,COMEDI_SUBD_AI,0); - if(subdevice<0){ - printf("no analog input subdevice found\n"); - exit(0); + if(options.subdevice < 0) + { + options.subdevice = comedi_find_subdevice_by_type(device, COMEDI_SUBD_AI, 0); + if(options.subdevice<0){ + printf("no analog input subdevice found\n"); + exit(-1); + } } + n_chans = comedi_get_n_channels(device, options.subdevice); + for(chan = 0; chan < n_chans; ++chan){ + printf("%d: ", chan); - n_chans=comedi_get_n_channels(device,subdevice); - for(chan=0;chan MAX_SAMPLES ){ + init_parsed_options(&options); + options.n_scan = 10; /* override default n_scan value to something more suitable */ + parse_options(&options, argc, argv); + if(options.n_scan > MAX_SAMPLES ){ fprintf( stderr, "Requested too many samples, reducing to %i\n", MAX_SAMPLES ); - n_scan = MAX_SAMPLES; + options.n_scan = MAX_SAMPLES; } - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - if(verbose){ + if(options.verbose){ printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", - filename,subdevice,channel,range,aref); + options.filename, options.subdevice, options.channel, options.range, options.aref); } /* Set up a the "instruction list", which is just a pointer @@ -79,10 +81,10 @@ int main(int argc, char *argv[]) /* Instruction 1: do 10 analog input reads */ insn[1].insn=INSN_READ; - insn[1].n=n_scan; + insn[1].n = options.n_scan; insn[1].data=data; - insn[1].subdev=subdevice; - insn[1].chanspec=CR_PACK(channel,range,aref); + insn[1].subdev = options.subdevice; + insn[1].chanspec=CR_PACK(options.channel, options.range, options.aref); /* Instruction 2: perform a gettimeofday() */ insn[2].insn=INSN_GTOD; @@ -91,18 +93,18 @@ int main(int argc, char *argv[]) ret=comedi_do_insnlist(device,&il); if(ret<0){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - printf("initial time: %ld.%06ld\n",t1.tv_sec,t1.tv_usec); - for(i=0;isubdev = subdevice; + cmd->subdev = subdevice; /* flags */ cmd->flags = TRIG_WAKE_EOS; diff --git a/demo/mmap.c b/demo/mmap.c index 526aa3a..ca3534f 100644 --- a/demo/mmap.c +++ b/demo/mmap.c @@ -30,8 +30,8 @@ unsigned int chanlist[256]; void *map; -int prepare_cmd_lib(comedi_t *dev,int subdevice,comedi_cmd *cmd); -int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd); +int prepare_cmd_lib(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd); +int prepare_cmd(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd); int main(int argc, char *argv[]) @@ -42,45 +42,47 @@ int main(int argc, char *argv[]) int front, back; int ret; int i; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - dev = comedi_open(filename); + dev = comedi_open(options.filename); if(!dev){ - comedi_perror(filename); + comedi_perror(options.filename); exit(1); } - size = comedi_get_buffer_size(dev,subdevice); - fprintf(stderr,"buffer size is %d\n",size); + size = comedi_get_buffer_size(dev, options.subdevice); + fprintf(stderr,"buffer size is %d\n", size); - map=mmap(NULL,size,PROT_READ,MAP_SHARED,comedi_fileno(dev),0); - fprintf(stderr,"map=%p\n",map); + map = mmap(NULL,size,PROT_READ,MAP_SHARED, comedi_fileno(dev), 0); + fprintf(stderr, "map=%p\n", map); if( map == MAP_FAILED ){ perror( "mmap" ); exit(1); } - for(i=0;ichanlist_len = n_chan; cmd->scan_end_arg = n_chan; - if(cmd->stop_src==TRIG_COUNT)cmd->stop_arg = n_scan; + if(cmd->stop_src == TRIG_COUNT) cmd->stop_arg = n_scan; return 0; } -int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd) +int prepare_cmd(comedi_t *dev, int subdevice, int n_scan, int n_chan, unsigned period_nanosec, comedi_cmd *cmd) { memset(cmd,0,sizeof(*cmd)); @@ -149,7 +151,7 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd) cmd->start_arg = 0; cmd->scan_begin_src = TRIG_TIMER; - cmd->scan_begin_arg = 1e9/freq; + cmd->scan_begin_arg = period_nanosec; cmd->convert_src = TRIG_TIMER; cmd->convert_arg = 1; diff --git a/demo/outp.c b/demo/outp.c index b123eee..e0b3dfd 100644 --- a/demo/outp.c +++ b/demo/outp.c @@ -29,28 +29,30 @@ int main(int argc, char *argv[]) { lsampl_t data; int ret; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - data = value; - if(verbose){ + data = options.value; + if(options.verbose){ printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", - data,filename,subdevice,channel,range,aref); + data, options.filename, options.subdevice, options.channel, options.range, options.aref); } - ret=comedi_data_write(device,subdevice,channel,range,aref,data); - if(ret<0){ - comedi_perror(filename); - exit(0); + ret = comedi_data_write(device, options.subdevice, options.channel, options.range, options.aref, data); + if(ret < 0){ + comedi_perror(options.filename); + exit(-1); } - printf("%d\n",data); + printf("%d\n", data); return 0; } diff --git a/demo/poll.c b/demo/poll.c index 78c2bd3..d678b19 100644 --- a/demo/poll.c +++ b/demo/poll.c @@ -33,45 +33,40 @@ #define BUFSZ 1000 sampl_t buf[BUFSZ]; -int n_chans = 1; -int n_scans = 10; +const int n_chans = 1; +const int n_scans = 10; unsigned int chanlist[4]; comedi_t *device; -void prepare_cmd(comedi_t *dev,comedi_cmd *cmd); +void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice); void do_cmd(comedi_t *dev,comedi_cmd *cmd); -#define sec_to_nsec(x) ((x)*1000000000) -#define sec_to_usec(x) ((x)*1000000) -#define sec_to_msec(x) ((x)*1000) -#define msec_to_nsec(x) ((x)*1000000) -#define msec_to_usec(x) ((x)*1000) -#define usec_to_nsec(x) ((x)*1000) - int main(int argc, char *argv[]) { comedi_cmd cmd; int i; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device = comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - perror(filename); + perror(options.filename); exit(1); } - fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK); + fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK); - for(i=0;i= 0) pin_data = options.channel; ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK); if(ret<0)perror("fcntl"); - prepare_cmd(dev,&cmd); + prepare_cmd(dev, &cmd, options.subdevice); do_cmd(dev,&cmd); @@ -152,7 +149,7 @@ void do_cmd(comedi_t *dev,comedi_cmd *cmd) go = 0; }else{ int i; - + total+=ret; for(i=0;i0xa000); @@ -198,7 +195,7 @@ unsigned int chanlist[16]; * of scans measured is 10. This is analogous to the old mode2 * acquisition. */ -void prepare_cmd(comedi_t *dev,comedi_cmd *cmd) +void prepare_cmd(comedi_t *dev,comedi_cmd *cmd, int subdevice) { memset(cmd,0,sizeof(comedi_cmd)); diff --git a/demo/select.c b/demo/select.c index b1bfdcc..739b65b 100644 --- a/demo/select.c +++ b/demo/select.c @@ -33,45 +33,40 @@ #define BUFSZ 1000 sampl_t buf[BUFSZ]; -int n_chans = 1; -int n_scans = 10; +const int n_chans = 1; +const int n_scans = 10; unsigned int chanlist[4]; comedi_t *device; -void prepare_cmd(comedi_t *dev,comedi_cmd *cmd); +void prepare_cmd(comedi_t *dev,comedi_cmd *cmd, int subdevice); void do_cmd(comedi_t *dev,comedi_cmd *cmd); -#define sec_to_nsec(x) ((x)*1000000000) -#define sec_to_usec(x) ((x)*1000000) -#define sec_to_msec(x) ((x)*1000) -#define msec_to_nsec(x) ((x)*1000000) -#define msec_to_usec(x) ((x)*1000) -#define usec_to_nsec(x) ((x)*1000) - int main(int argc, char *argv[]) { comedi_cmd cmd; int i; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device = comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - perror(filename); + perror(options.filename); exit(1); } - fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK); + fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK); - for(i=0;i @@ -28,33 +28,35 @@ int main(int argc, char *argv[]) int ret; comedi_sv_t sv; double volts; + struct parsed_options options; - parse_options(argc,argv); + init_parsed_options(&options); + parse_options(&options, argc, argv); - device=comedi_open(filename); + device = comedi_open(options.filename); if(!device){ - comedi_perror(filename); - exit(0); + comedi_perror(options.filename); + exit(-1); } - if(verbose){ + if(options.verbose){ printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", - filename,subdevice,channel,range,aref); + options.filename, options.subdevice, options.channel, options.range, options.aref); } - comedi_sv_init(&sv,device,subdevice,channel); + comedi_sv_init(&sv, device, options.subdevice, options.channel); - sv.range=range; - sv.aref=aref; - sv.n=100; + sv.range = options.range; + sv.aref = options.aref; + sv.n = 100; - ret=comedi_sv_measure(&sv,&volts); - if(ret<0){ + ret = comedi_sv_measure(&sv, &volts); + if(ret < 0){ comedi_perror("comedi_sv_measure()"); - exit(0); + exit(-1); } - printf("%g\n",volts); + printf("%g\n", volts); return 0; }