Made parse_options() write options to a struct instead of passing

them through global variables.
This commit is contained in:
Frank Mori Hess 2007-01-03 21:14:53 +00:00
parent c7a581399a
commit fbdf65b4f5
24 changed files with 503 additions and 467 deletions

View file

@ -47,28 +47,30 @@ int main(int argc, char *argv[])
{ {
lsampl_t data; lsampl_t data;
int ret; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(0);
} }
data = value; data = options.value;
if(verbose){ if(options.verbose){
printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", 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){ if(ret<0){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(0);
} }
printf("%d\n",data); printf("%d\n", data);
ao_antialias((1000<<16)+1000); ao_antialias((1000<<16)+1000);

View file

@ -82,47 +82,49 @@ int main(int argc, char *argv[])
double amplitude; double amplitude;
/* offset, in DAC units */ /* offset, in DAC units */
double offset; double offset;
int subdevice; struct parsed_options options;
init_parsed_options(&options);
parse_options(argc,argv); options.subdevice = -1;
parse_options(&options, argc, argv);
/* Force n_chan to be 1 */ /* 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){ if(dev == NULL){
fprintf(stderr, "error opening %s\n", filename); fprintf(stderr, "error opening %s\n", options.filename);
return -1; 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); maxdata = comedi_get_maxdata(dev, options.subdevice, 0);
rng = comedi_get_range(dev,subdevice,0,0); rng = comedi_get_range(dev, options.subdevice, 0, 0);
offset = (double)comedi_from_phys(0.0, rng, maxdata); offset = (double)comedi_from_phys(0.0, rng, maxdata);
amplitude = (double)comedi_from_phys(1.0, rng, maxdata) - offset; amplitude = (double)comedi_from_phys(1.0, rng, maxdata) - offset;
memset(&cmd,0,sizeof(cmd)); memset(&cmd,0,sizeof(cmd));
cmd.subdev = subdevice; cmd.subdev = options.subdevice;
cmd.flags = 0; cmd.flags = 0;
cmd.start_src = TRIG_INT; cmd.start_src = TRIG_INT;
cmd.start_arg = 0; cmd.start_arg = 0;
cmd.scan_begin_src = TRIG_TIMER; 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_src = TRIG_NOW;
cmd.convert_arg = 0; cmd.convert_arg = 0;
cmd.scan_end_src = TRIG_COUNT; 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_src = TRIG_NONE;
cmd.stop_arg = 0; cmd.stop_arg = 0;
cmd.chanlist = chanlist; 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); err = comedi_command_test(dev, &cmd);
if (err < 0) { if (err < 0) {
@ -141,7 +143,7 @@ int main(int argc, char *argv[])
exit(1); 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); fprintf(stderr, "buffer size is %d\n", size);
map = mmap(NULL, size, PROT_WRITE, MAP_SHARED, comedi_fileno(dev), 0); map = mmap(NULL, size, PROT_WRITE, MAP_SHARED, comedi_fileno(dev), 0);
if(map == MAP_FAILED) if(map == MAP_FAILED)
@ -157,20 +159,20 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
printf("marking %i samples as written\n", num_samples); 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) if(ret < 0)
{ {
comedi_perror("comedi_mark_buffer_written"); comedi_perror("comedi_mark_buffer_written");
exit(1); exit(1);
} }
ret = comedi_internal_trigger(dev, subdevice, 0); ret = comedi_internal_trigger(dev, options.subdevice, 0);
if(ret<0){ if(ret<0){
comedi_perror("comedi_internal_trigger"); comedi_perror("comedi_internal_trigger");
exit(1); exit(1);
} }
while(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; int bytes_unmarked = size - bytes_marked;
if(bytes_marked < 0) if(bytes_marked < 0)
{ {
@ -180,7 +182,7 @@ int main(int argc, char *argv[])
if(bytes_unmarked > 0) if(bytes_unmarked > 0)
{ {
// this keeps comedi from reporting a buffer underrun // 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"); comedi_perror("comedi_mark_buffer_written");
exit(1); exit(1);

View file

@ -70,13 +70,12 @@ double offset = 2048;
inefficient */ inefficient */
#define BUF_LEN 0x8000 #define BUF_LEN 0x8000
int subdevice;
int external_trigger_number = 0; int external_trigger_number = 0;
sampl_t data[BUF_LEN]; sampl_t data[BUF_LEN];
void dds_output(sampl_t *buf,int n); 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. */ /* This define determines which waveform to use. */
#define dds_init_function dds_init_sine #define dds_init_function dds_init_sine
@ -113,50 +112,54 @@ int main(int argc, char *argv[])
unsigned int maxdata; unsigned int maxdata;
comedi_range *rng; comedi_range *rng;
int ret; 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 */ /* Force n_chan to be 1 */
n_chan = 1; options.n_chan = 1;
if(value){ if(options.value){
waveform_frequency = value; waveform_frequency = options.value;
} }
dev = comedi_open(filename); dev = comedi_open(options.filename);
if(dev == NULL){ if(dev == NULL){
fprintf(stderr, "error opening %s\n", filename); fprintf(stderr, "error opening %s\n", options.filename);
return -1; 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); maxdata = comedi_get_maxdata(dev, options.subdevice, 0);
rng = comedi_get_range(dev,subdevice,0,0); rng = comedi_get_range(dev, options.subdevice, 0, 0);
offset = (double)comedi_from_phys(0.0,rng,maxdata); offset = (double)comedi_from_phys(0.0, rng, maxdata);
amplitude = (double)comedi_from_phys(1.0,rng,maxdata) - offset; amplitude = (double)comedi_from_phys(1.0, rng, maxdata) - offset;
memset(&cmd,0,sizeof(cmd)); memset(&cmd,0,sizeof(cmd));
cmd.subdev = subdevice; cmd.subdev = options.subdevice;
cmd.flags = 0; cmd.flags = 0;
cmd.start_src = TRIG_INT; cmd.start_src = TRIG_INT;
cmd.start_arg = 0; cmd.start_arg = 0;
cmd.scan_begin_src = TRIG_TIMER; 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_src = TRIG_NOW;
cmd.convert_arg = 0; cmd.convert_arg = 0;
cmd.scan_end_src = TRIG_COUNT; 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_src = TRIG_NONE;
cmd.stop_arg = 0; cmd.stop_arg = 0;
cmd.chanlist = chanlist; 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);
chanlist[1] = CR_PACK(channel+1,range,aref); chanlist[1] = CR_PACK(options.channel + 1, options.range, options.aref);
dds_init(); dds_init(waveform_frequency, options.freq);
dump_cmd(stdout,&cmd); dump_cmd(stdout,&cmd);
@ -191,8 +194,8 @@ int main(int argc, char *argv[])
} }
printf("m=%d\n",m); printf("m=%d\n",m);
ret = comedi_internal_trigger(dev, subdevice, 0); ret = comedi_internal_trigger(dev, options.subdevice, 0);
if(ret<0){ if(ret < 0){
perror("comedi_internal_trigger\n"); perror("comedi_internal_trigger\n");
exit(1); exit(1);
} }
@ -228,9 +231,9 @@ sampl_t waveform[WAVEFORM_LEN];
unsigned int acc; unsigned int acc;
unsigned int adder; unsigned int adder;
void dds_init(void) void dds_init(double waveform_frequency, double update_frequency)
{ {
adder=waveform_frequency/freq*(1<<16)*(1<<WAVEFORM_SHIFT); adder = waveform_frequency / update_frequency * (1 << 16) * (1 << WAVEFORM_SHIFT);
dds_init_function(); dds_init_function();
} }

View file

@ -1,5 +1,5 @@
/* /*
* demo for changing between different calibrations * demo for changing between different (hardware-based) calibrations
* Part of Comedilib * Part of Comedilib
* *
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org> * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
@ -29,24 +29,26 @@ int main(int argc, char *argv[])
{ {
lsampl_t data; lsampl_t data;
int ret; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
data = value; data = options.value;
if(verbose){ if(options.verbose){
printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", 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); ret = comedi_apply_calibration(device, options.subdevice, options.channel, options.range, options.aref, NULL);
if(ret<0){ if(ret < 0){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(0);
} }

View file

@ -34,20 +34,24 @@ int main(int argc, char *argv[])
{ {
unsigned period_ns; unsigned period_ns;
int retval; int retval;
lsampl_t clock_selection;
struct parsed_options options;
freq = 0.; init_parsed_options(&options);
parse_options(argc,argv); options.freq = 0.;
parse_options(&options, argc, argv);
device=comedi_open(filename); device = comedi_open(options.filename);
if(!device){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
if(freq > 0.) if(options.freq > 0.)
period_ns = 1e9 / freq; period_ns = 1e9 / options.freq;
else else
period_ns = 0; 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) if(period_ns)
{ {
printf("Clock period = %d nanoseconds.\n", period_ns); printf("Clock period = %d nanoseconds.\n", period_ns);
@ -59,11 +63,11 @@ int main(int argc, char *argv[])
lsampl_t data[3]; lsampl_t data[3];
memset(&insn, 0, sizeof(comedi_insn)); memset(&insn, 0, sizeof(comedi_insn));
insn.insn = INSN_CONFIG; insn.insn = INSN_CONFIG;
insn.subdev = subdevice; insn.subdev = options.subdevice;
insn.data = data; insn.data = data;
insn.n = sizeof(data) / sizeof(data[0]); insn.n = sizeof(data) / sizeof(data[0]);
data[0] = INSN_CONFIG_SET_CLOCK_SRC; data[0] = INSN_CONFIG_SET_CLOCK_SRC;
data[1] = value; data[1] = clock_selection;
data[2] = period_ns; data[2] = period_ns;
retval = comedi_do_insn(device, &insn); retval = comedi_do_insn(device, &insn);

View file

@ -34,30 +34,34 @@ int main(int argc, char *argv[])
{ {
unsigned period_ns; unsigned period_ns;
int retval; int retval;
lsampl_t routing;
struct parsed_options options;
freq = 0.; init_parsed_options(&options);
parse_options(argc,argv); options.freq = 0.;
parse_options(&options, argc, argv);
device=comedi_open(filename); device = comedi_open(options.filename);
if(!device){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
if(freq > 0.) if(options.freq > 0.)
period_ns = 1e9 / freq; period_ns = 1e9 / options.freq;
else else
period_ns = 0; 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; comedi_insn insn;
lsampl_t data[2]; lsampl_t data[2];
memset(&insn, 0, sizeof(comedi_insn)); memset(&insn, 0, sizeof(comedi_insn));
insn.insn = INSN_CONFIG; insn.insn = INSN_CONFIG;
insn.subdev = subdevice; insn.subdev = options.subdevice;
insn.chanspec = channel; insn.chanspec = options.channel;
insn.data = data; insn.data = data;
insn.n = sizeof(data) / sizeof(data[0]); insn.n = sizeof(data) / sizeof(data[0]);
data[0] = INSN_CONFIG_SET_ROUTING; data[0] = INSN_CONFIG_SET_ROUTING;
data[1] = value; data[1] = routing;
retval = comedi_do_insn(device, &insn); retval = comedi_do_insn(device, &insn);
if(retval < 0) comedi_perror("comedi_do_insn"); if(retval < 0) comedi_perror("comedi_do_insn");

View file

@ -34,12 +34,12 @@ static comedi_range * range_info[N_CHANS];
static lsampl_t maxdata[N_CHANS]; static lsampl_t maxdata[N_CHANS];
int prepare_cmd_lib(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,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 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[]={ char *cmdtest_messages[]={
"success", "success",
@ -60,26 +60,28 @@ int main(int argc, char *argv[])
struct timeval start,end; struct timeval start,end;
int subdev_flags; int subdev_flags;
lsampl_t raw; lsampl_t raw;
struct parsed_options options;
parse_options(argc,argv); init_parsed_options(&options);
parse_options(&options, argc, argv);
/* The following global variables used in this demo are /* The following variables used in this demo
* defined in common.c, and can be modified by command line * can be modified by command line
* options. When modifying this demo, you may want to * options. When modifying this demo, you may want to
* change them here. */ * change them here. */
//filename = "/dev/comedi0"; //options.filename = "/dev/comedi0";
//subdevice = 0; //options.subdevice = 0;
//channel = 0; //options.channel = 0;
//range = 0; //options.range = 0;
//aref = AREF_GROUND; //options.aref = AREF_GROUND;
//n_chan = 4; //options.n_chan = 4;
//n_scan = 1000; //options.n_scan = 1000;
//freq = 1000.0; //options.freq = 1000.0;
/* open the device */ /* open the device */
dev = comedi_open(filename); dev = comedi_open(options.filename);
if(!dev){ if(!dev){
comedi_perror(filename); comedi_perror(options.filename);
exit(1); exit(1);
} }
@ -87,20 +89,20 @@ int main(int argc, char *argv[])
comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);
/* Set up channel list */ /* Set up channel list */
for(i=0;i<n_chan;i++){ for(i = 0; i < options.n_chan; i++){
chanlist[i]=CR_PACK(channel+i,range,aref); chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
range_info[i]=comedi_get_range(dev,subdevice,channel,range); range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range);
maxdata[i]=comedi_get_maxdata(dev,subdevice,channel); maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel);
} }
/* prepare_cmd_lib() uses a Comedilib routine to find a /* prepare_cmd_lib() uses a Comedilib routine to find a
* good command for the device. prepare_cmd() explicitly * good command for the device. prepare_cmd() explicitly
* creates a command, which may not work for your device. */ * creates a command, which may not work for your device. */
prepare_cmd_lib(dev,subdevice,cmd); prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
//prepare_cmd(dev,subdevice,cmd); //prepare_cmd(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
fprintf(stderr,"command before testing:\n"); fprintf(stderr, "command before testing:\n");
dump_cmd(stderr,cmd); dump_cmd(stderr, cmd);
/* comedi_command_test() tests a command to see if the /* comedi_command_test() tests a command to see if the
* trigger sources and arguments are valid for the subdevice. * trigger sources and arguments are valid for the subdevice.
@ -112,56 +114,56 @@ int main(int argc, char *argv[])
* can test it multiple times until it passes. Typically, * can test it multiple times until it passes. Typically,
* if you can't get a valid command in two tests, the original * if you can't get a valid command in two tests, the original
* command wasn't specified very well. */ * command wasn't specified very well. */
ret = comedi_command_test(dev,cmd); ret = comedi_command_test(dev, cmd);
if(ret<0){ if(ret < 0){
comedi_perror("comedi_command_test"); comedi_perror("comedi_command_test");
if(errno==EIO){ if(errno == EIO){
fprintf(stderr,"Ummm... this subdevice doesn't support commands\n"); fprintf(stderr,"Ummm... this subdevice doesn't support commands\n");
} }
exit(1); exit(1);
} }
fprintf(stderr,"first test returned %d (%s)\n",ret, fprintf(stderr,"first test returned %d (%s)\n", ret,
cmdtest_messages[ret]); cmdtest_messages[ret]);
dump_cmd(stderr,cmd); dump_cmd(stderr, cmd);
ret = comedi_command_test(dev,cmd); ret = comedi_command_test(dev, cmd);
if(ret<0){ if(ret < 0){
comedi_perror("comedi_command_test"); comedi_perror("comedi_command_test");
exit(1); exit(1);
} }
fprintf(stderr,"second test returned %d (%s)\n",ret, fprintf(stderr,"second test returned %d (%s)\n", ret,
cmdtest_messages[ret]); cmdtest_messages[ret]);
if(ret!=0){ if(ret!=0){
dump_cmd(stderr,cmd); dump_cmd(stderr, cmd);
fprintf(stderr,"Error preparing command\n"); fprintf(stderr, "Error preparing command\n");
exit(1); exit(1);
} }
/* this is only for informational purposes */ /* this is only for informational purposes */
gettimeofday(&start,NULL); gettimeofday(&start, NULL);
fprintf(stderr,"start time: %ld.%06ld\n",start.tv_sec,start.tv_usec); fprintf(stderr,"start time: %ld.%06ld\n", start.tv_sec, start.tv_usec);
/* start the command */ /* start the command */
ret=comedi_command(dev,cmd); ret = comedi_command(dev, cmd);
if(ret<0){ if(ret < 0){
comedi_perror("comedi_command"); comedi_perror("comedi_command");
exit(1); exit(1);
} }
subdev_flags = comedi_get_subdevice_flags(dev, subdevice); subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice);
while(1){ while(1){
ret=read(comedi_fileno(dev),buf,BUFSZ); ret = read(comedi_fileno(dev),buf,BUFSZ);
if(ret<0){ if(ret < 0){
/* some error occurred */ /* some error occurred */
perror("read"); perror("read");
break; break;
}else if(ret==0){ }else if(ret == 0){
/* reached stop condition */ /* reached stop condition */
break; break;
}else{ }else{
static int col = 0; static int col = 0;
int bytes_per_sample; int bytes_per_sample;
total+=ret; total += ret;
if(verbose)fprintf(stderr,"read %d %d\n",ret,total); if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total);
if(subdev_flags & SDF_LSAMPL) if(subdev_flags & SDF_LSAMPL)
bytes_per_sample = sizeof(lsampl_t); bytes_per_sample = sizeof(lsampl_t);
else else
@ -172,9 +174,9 @@ int main(int argc, char *argv[])
} else { } else {
raw = ((sampl_t *)buf)[i]; raw = ((sampl_t *)buf)[i];
} }
print_datum(raw,col); print_datum(raw, col, options.physical);
col++; col++;
if(col==n_chan){ if(col == options.n_chan){
printf("\n"); printf("\n");
col=0; col=0;
} }
@ -184,15 +186,15 @@ int main(int argc, char *argv[])
/* this is only for informational purposes */ /* this is only for informational purposes */
gettimeofday(&end,NULL); gettimeofday(&end,NULL);
fprintf(stderr,"end time: %ld.%06ld\n",end.tv_sec,end.tv_usec); fprintf(stderr,"end time: %ld.%06ld\n", end.tv_sec, end.tv_usec);
end.tv_sec-=start.tv_sec; end.tv_sec -= start.tv_sec;
if(end.tv_usec<start.tv_usec){ if(end.tv_usec < start.tv_usec){
end.tv_sec--; end.tv_sec--;
end.tv_usec+=1000000; end.tv_usec += 1000000;
} }
end.tv_usec-=start.tv_usec; end.tv_usec -= start.tv_usec;
fprintf(stderr,"time: %ld.%06ld\n",end.tv_sec,end.tv_usec); fprintf(stderr,"time: %ld.%06ld\n", end.tv_sec, end.tv_usec);
return 0; return 0;
} }
@ -201,7 +203,7 @@ int main(int argc, char *argv[])
* This prepares a command in a pretty generic way. We ask the * This prepares a command in a pretty generic way. We ask the
* library to create a stock command that supports periodic * library to create a stock command that supports periodic
* sampling of data, then modify the parts we want. */ * sampling of data, then modify the parts we want. */
int prepare_cmd_lib(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 ret; int ret;
@ -210,18 +212,18 @@ int prepare_cmd_lib(comedi_t *dev,int subdevice,comedi_cmd *cmd)
/* This comedilib function will get us a generic timed /* This comedilib function will get us a generic timed
* command for a particular board. If it returns -1, * command for a particular board. If it returns -1,
* that's bad. */ * that's bad. */
ret = comedi_get_cmd_generic_timed(dev,subdevice,cmd,1e9/freq); ret = comedi_get_cmd_generic_timed(dev, subdevice,cmd, period_nanosec);
if(ret<0){ if(ret<0){
printf("comedi_get_cmd_generic_timed failed\n"); printf("comedi_get_cmd_generic_timed failed\n");
return ret; return ret;
} }
/* Modify parts of the command */ /* Modify parts of the command */
cmd->chanlist = chanlist; cmd->chanlist = chanlist;
cmd->chanlist_len = n_chan; cmd->chanlist_len = n_chan;
cmd->scan_end_arg = 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; 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. * Set up a command by hand. This will not work on some devices.
* There is no single command that will work on all 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)); memset(cmd,0,sizeof(*cmd));
@ -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 * by the device, but it will be adjusted to the nearest supported
* value by comedi_command_test(). */ * value by comedi_command_test(). */
cmd->scan_begin_src = TRIG_TIMER; 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. /* The timing between each sample in a scan is controlled by convert.
* TRIG_TIMER: Conversion events occur periodically. * TRIG_TIMER: Conversion events occur periodically.
@ -325,12 +327,12 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd)
return 0; return 0;
} }
void print_datum(lsampl_t raw, int i) { void print_datum(lsampl_t raw, int channel_index, short physical) {
double physical_value; double physical_value;
if(!physical) { if(!physical) {
printf("%d ",raw); printf("%d ",raw);
} else { } 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); printf("%#8.6g ",physical_value);
} }
} }

View file

@ -15,69 +15,67 @@
#include <stdlib.h> #include <stdlib.h>
#include "examples.h" #include "examples.h"
static char * const default_filename = "/dev/comedi0";
char *filename="/dev/comedi0"; void init_parsed_options(struct parsed_options *options)
int verbose = 0; {
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 value=0; int parse_options(struct parsed_options *options, int argc, char *argv[])
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;
int parse_options(int argc, char *argv[])
{ {
int c; int c;
while (-1 != (c = getopt(argc, argv, "a:c:s:r:f:n:N:F:pvdgom"))) { while (-1 != (c = getopt(argc, argv, "a:c:s:r:f:n:N:F:pvdgom"))) {
switch (c) { switch (c) {
case 'f': case 'f':
filename = optarg; options->filename = optarg;
break; break;
case 's': case 's':
subdevice = strtoul(optarg,NULL,0); options->subdevice = strtoul(optarg, NULL, 0);
break; break;
case 'c': case 'c':
channel = strtoul(optarg,NULL,0); options->channel = strtoul(optarg, NULL, 0);
break; break;
case 'a': case 'a':
aref = strtoul(optarg,NULL,0); options->aref = strtoul(optarg, NULL, 0);
break; break;
case 'r': case 'r':
range = strtoul(optarg,NULL,0); options->range = strtoul(optarg, NULL, 0);
break; break;
case 'n': case 'n':
n_chan = strtoul(optarg,NULL,0); options->n_chan = strtoul(optarg, NULL, 0);
break; break;
case 'N': case 'N':
n_scan = strtoul(optarg,NULL,0); options->n_scan = strtoul(optarg, NULL, 0);
break; break;
case 'F': case 'F':
freq = strtoul(optarg,NULL,0); options->freq = strtod(optarg, NULL);
break; break;
case 'p': case 'p':
physical = 1; options->physical = 1;
break; break;
case 'v': case 'v':
verbose = 1; ++options->verbose;
break; break;
case 'd': case 'd':
aref = AREF_DIFF; options->aref = AREF_DIFF;
break; break;
case 'g': case 'g':
aref = AREF_GROUND; options->aref = AREF_GROUND;
break; break;
case 'o': case 'o':
aref = AREF_OTHER; options->aref = AREF_OTHER;
break; break;
case 'm': case 'm':
aref = AREF_COMMON; options->aref = AREF_COMMON;
break; break;
default: default:
printf("bad option\n"); printf("bad option\n");
@ -86,7 +84,7 @@ int parse_options(int argc, char *argv[])
} }
if(optind < argc) { if(optind < argc) {
/* data value */ /* data value */
sscanf(argv[optind++],"%d",&value); options->value = strtod(argv[optind++], NULL);
} }
return argc; return argc;

View file

@ -31,30 +31,32 @@ int main(int argc, char *argv[])
{ {
int ret; int ret;
int stype; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
stype = comedi_get_subdevice_type(device,subdevice); stype = comedi_get_subdevice_type(device, options.subdevice);
if(stype!=COMEDI_SUBD_DIO){ if(stype != COMEDI_SUBD_DIO){
printf("%d is not a digital I/O subdevice\n",subdevice); printf("%d is not a digital I/O subdevice\n", options.subdevice);
exit(0); exit(-1);
} }
printf("configuring pin %d or subdevice %d ", channel, subdevice); printf("configuring pin %d or subdevice %d ", options.channel, options.subdevice);
if(value) if(options.value)
{ {
printf("for output.\n"); 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 }else
{ {
printf("for input.\n"); 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; return 0;
} }

View file

@ -12,7 +12,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "examples.h" #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); void dump_eeprom(unsigned int *eeprom,int len);
comedi_t *device; comedi_t *device;
@ -21,16 +21,19 @@ int main(int argc, char *argv[])
{ {
int len; int len;
unsigned int *eeprom; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
len=read_eeprom(device,&eeprom); len = read_eeprom(device, &eeprom, options);
dump_eeprom(eeprom,len); dump_eeprom(eeprom,len);
return 0; 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; int n,i,ret;
lsampl_t data; lsampl_t data;
unsigned int *ptr; unsigned int *ptr;
lsampl_t maxdata; lsampl_t maxdata;
subd=comedi_find_subdevice_by_type(it,COMEDI_SUBD_MEMORY,0); if(options.subdevice < 0)
if(subd<0){ {
fprintf(stderr,"No memory subdevice\n"); options.subdevice = comedi_find_subdevice_by_type(it, COMEDI_SUBD_MEMORY, 0);
return 0; if(options.subdevice < 0){
fprintf(stderr,"No memory subdevice\n");
return 0;
}
} }
n=comedi_get_n_channels(it,subd); n = comedi_get_n_channels(it, options.subdevice);
maxdata=comedi_get_maxdata(it,subd,0); maxdata = comedi_get_maxdata(it, options.subdevice, 0);
if(maxdata!=0xff){ if(maxdata != 0xff){
fprintf(stderr,"Memory subdevice has strange maxdata, aborting\n"); 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<n;i++){ for(i = 0; i < n; i++){
ret=comedi_data_read(it,subd,i,0,0,&data); ret = comedi_data_read(it, options.subdevice, i, 0, 0, &data);
ptr[i]=data; ptr[i] = data;
if(ret<0){ if(ret < 0){
comedi_perror("comedi_data_read"); comedi_perror("comedi_data_read");
return 0; return 0;
} }

View file

@ -8,24 +8,27 @@
* Definitions of some of the common code. * Definitions of some of the common code.
*/ */
extern char *filename;
extern int verbose_flag;
extern comedi_t *device; extern comedi_t *device;
extern int value; struct parsed_options
extern int subdevice; {
extern int channel; char *filename;
extern int aref; double value;
extern int range; int subdevice;
extern int physical; int channel;
extern int verbose; int aref;
extern int n_chan; int range;
extern int n_scan; int physical;
extern double freq; int verbose;
int n_chan;
int n_scan;
double freq;
};
int parse_options(int argc, char *argv[]); extern void init_parsed_options(struct parsed_options *options);
char *cmd_src(int src,char *buf); extern int parse_options(struct parsed_options *options, int argc, char *argv[]);
void dump_cmd(FILE *file,comedi_cmd *cmd); extern char *cmd_src(int src,char *buf);
extern void dump_cmd(FILE *file,comedi_cmd *cmd);
#define sec_to_nsec(x) ((x)*1000000000) #define sec_to_nsec(x) ((x)*1000000000)

View file

@ -49,24 +49,26 @@ int main(int argc,char *argv[])
int n_ranges; int n_ranges;
int subdev_flags; int subdev_flags;
comedi_range *rng; comedi_range *rng;
struct parsed_options options;
parse_options(argc,argv); init_parsed_options(&options);
parse_options(&options, argc, argv);
it=comedi_open(filename); it = comedi_open(options.filename);
if(!it){ if(!it){
comedi_perror(filename); comedi_perror(options.filename);
exit(1); exit(1);
} }
printf("overall info:\n"); printf("overall info:\n");
printf(" version code: 0x%06x\n",comedi_get_version_code(it)); printf(" version code: 0x%06x\n", comedi_get_version_code(it));
printf(" driver name: %s\n",comedi_get_driver_name(it)); printf(" driver name: %s\n", comedi_get_driver_name(it));
printf(" board name: %s\n",comedi_get_board_name(it)); printf(" board name: %s\n", comedi_get_board_name(it));
printf(" number of subdevices: %d\n",n_subdevices=comedi_get_n_subdevices(it)); printf(" number of subdevices: %d\n", n_subdevices = comedi_get_n_subdevices(it));
for(i=0;i<n_subdevices;i++){ for(i = 0; i < n_subdevices; i++){
printf("subdevice %d:\n",i); printf("subdevice %d:\n",i);
type=comedi_get_subdevice_type(it,i); type = comedi_get_subdevice_type(it, i);
printf(" type: %d (%s)\n",type,subdevice_types[type]); printf(" type: %d (%s)\n",type,subdevice_types[type]);
if(type==COMEDI_SUBD_UNUSED) if(type==COMEDI_SUBD_UNUSED)
continue; continue;

View file

@ -33,40 +33,42 @@ int main(int argc, char *argv[])
comedi_range * range_info; comedi_range * range_info;
lsampl_t maxdata; lsampl_t maxdata;
double physical_value; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
if(verbose){ if(options.verbose){
printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", 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); ret = comedi_data_read(device, options.subdevice, options.channel, options.range, options.aref, &data);
if(ret<0){ if(ret < 0){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
if(physical) { if(options.physical) {
comedi_set_global_oor_behavior(COMEDI_OOR_NAN); comedi_set_global_oor_behavior(COMEDI_OOR_NAN);
range_info = comedi_get_range(device,subdevice,channel,range); range_info = comedi_get_range(device, options.subdevice, options.channel, options.range);
maxdata = comedi_get_maxdata(device,subdevice,channel); maxdata = comedi_get_maxdata(device, options.subdevice, options.channel);
if(verbose) { if(options.verbose) {
printf("[0,%d] -> [%g,%g]\n", maxdata, printf("[0,%d] -> [%g,%g]\n", maxdata,
range_info->min, range_info->max); 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)) { if(isnan(physical_value)) {
printf("Out of range [%g,%g]", printf("Out of range [%g,%g]",
range_info->min, range_info->max); range_info->min, range_info->max);
} else { } else {
printf("%g",physical_value); printf("%g", physical_value);
switch(range_info->unit) { switch(range_info->unit) {
case UNIT_volt: printf(" V"); break; case UNIT_volt: printf(" V"); break;
case UNIT_mA: printf(" mA"); break; case UNIT_mA: printf(" mA"); break;
@ -74,7 +76,7 @@ int main(int argc, char *argv[])
default: printf(" (unknown unit %d)", default: printf(" (unknown unit %d)",
range_info->unit); range_info->unit);
} }
if(verbose) { if(options.verbose) {
printf(" (%d raw units)", data); printf(" (%d raw units)", data);
} }
} }

View file

@ -36,36 +36,40 @@ int main(int argc, char *argv[])
int maxdata; int maxdata;
lsampl_t data; lsampl_t data;
double voltage; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
subdevice=comedi_find_subdevice_by_type(device,COMEDI_SUBD_AI,0); if(options.subdevice < 0)
if(subdevice<0){ {
printf("no analog input subdevice found\n"); options.subdevice = comedi_find_subdevice_by_type(device, COMEDI_SUBD_AI, 0);
exit(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); n_ranges = comedi_get_n_ranges(device, options.subdevice, chan);
for(chan=0;chan<n_chans;chan++){
printf("%d: ",chan);
n_ranges=comedi_get_n_ranges(device,subdevice,chan); maxdata = comedi_get_maxdata(device, options.subdevice, chan);
for(range = 0; range < n_ranges; range++){
maxdata=comedi_get_maxdata(device,subdevice,chan); comedi_data_read(device, options.subdevice, chan, options.range, options.aref, &data);
for(range=0;range<n_ranges;range++){ voltage = comedi_to_phys(data, comedi_get_range(device, options.subdevice, chan, options.range), maxdata);
comedi_data_read(device,subdevice,chan,range,aref,&data); printf("%g ", voltage);
voltage=comedi_to_phys(data,comedi_get_range(device,subdevice,chan,range),maxdata);
printf("%g ",voltage);
} }
printf("\n"); printf("\n");
} }
return 0;
exit(0);
} }

View file

@ -47,23 +47,25 @@ int main(int argc, char *argv[])
comedi_insnlist il; comedi_insnlist il;
struct timeval t1,t2; struct timeval t1,t2;
lsampl_t data[MAX_SAMPLES]; lsampl_t data[MAX_SAMPLES];
struct parsed_options options;
n_scan = 10; /* override default n_scan value to something more suitable */ init_parsed_options(&options);
parse_options(argc,argv); options.n_scan = 10; /* override default n_scan value to something more suitable */
if( n_scan > MAX_SAMPLES ){ parse_options(&options, argc, argv);
if(options.n_scan > MAX_SAMPLES ){
fprintf( stderr, "Requested too many samples, reducing to %i\n", 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
if(verbose){ if(options.verbose){
printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", 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 /* 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 */ /* Instruction 1: do 10 analog input reads */
insn[1].insn=INSN_READ; insn[1].insn=INSN_READ;
insn[1].n=n_scan; insn[1].n = options.n_scan;
insn[1].data=data; insn[1].data=data;
insn[1].subdev=subdevice; insn[1].subdev = options.subdevice;
insn[1].chanspec=CR_PACK(channel,range,aref); insn[1].chanspec=CR_PACK(options.channel, options.range, options.aref);
/* Instruction 2: perform a gettimeofday() */ /* Instruction 2: perform a gettimeofday() */
insn[2].insn=INSN_GTOD; insn[2].insn=INSN_GTOD;
@ -91,18 +93,18 @@ int main(int argc, char *argv[])
ret=comedi_do_insnlist(device,&il); ret=comedi_do_insnlist(device,&il);
if(ret<0){ if(ret<0){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
printf("initial time: %ld.%06ld\n",t1.tv_sec,t1.tv_usec); printf("initial time: %ld.%06ld\n", t1.tv_sec, t1.tv_usec);
for(i=0;i<n_scan;i++){ for(i = 0; i < options.n_scan; i++){
printf("%d\n",data[i]); printf("%d\n", data[i]);
} }
printf("final time: %ld.%06ld\n",t2.tv_sec,t2.tv_usec); printf("final time: %ld.%06ld\n", t2.tv_sec, t2.tv_usec);
printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec)*1000000+ printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec) * 1000000 +
(t2.tv_usec-t1.tv_usec)); (t2.tv_usec - t1.tv_usec));
return 0; return 0;
} }

View file

@ -58,7 +58,7 @@ sampl_t buf[BUFSZ];
unsigned int chanlist[16]; unsigned int chanlist[16];
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); void do_cmd(comedi_t *dev,comedi_cmd *cmd);
void do_toggle(void); void do_toggle(void);
@ -125,19 +125,19 @@ void do_toggle(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *fn = NULL;
int ret; int ret;
comedi_cmd cmd; comedi_cmd cmd;
struct parsed_options options;
fn = "/dev/comedi1"; init_parsed_options(&options);
parse_options(&options, argc, argv);
device = comedi_open(fn); device = comedi_open(options.filename);
if(!device){ if(!device){
perror(fn); perror(options.filename);
exit(1); exit(1);
} }
subdevice = 3;
out_subd = 0; out_subd = 0;
config_output(); config_output();
@ -156,7 +156,7 @@ int main(int argc, char *argv[])
} }
#endif #endif
prepare_cmd(device,&cmd); prepare_cmd(device, &cmd, options.subdevice);
do_cmd(device,&cmd); do_cmd(device,&cmd);
@ -245,12 +245,12 @@ void do_cmd(comedi_t *dev,comedi_cmd *cmd)
* of scans measured is 10. This is analogous to the old mode2 * of scans measured is 10. This is analogous to the old mode2
* acquisition. * 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(*cmd)); memset(cmd,0,sizeof(*cmd));
/* the subdevice that the command is sent to */ /* the subdevice that the command is sent to */
cmd->subdev = subdevice; cmd->subdev = subdevice;
/* flags */ /* flags */
cmd->flags = TRIG_WAKE_EOS; cmd->flags = TRIG_WAKE_EOS;

View file

@ -30,8 +30,8 @@ unsigned int chanlist[256];
void *map; void *map;
int prepare_cmd_lib(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,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[]) int main(int argc, char *argv[])
@ -42,45 +42,47 @@ int main(int argc, char *argv[])
int front, back; int front, back;
int ret; int ret;
int i; 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){ if(!dev){
comedi_perror(filename); comedi_perror(options.filename);
exit(1); 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); fprintf(stderr,"buffer size is %d\n", size);
map=mmap(NULL,size,PROT_READ,MAP_SHARED,comedi_fileno(dev),0); map = mmap(NULL,size,PROT_READ,MAP_SHARED, comedi_fileno(dev), 0);
fprintf(stderr,"map=%p\n",map); fprintf(stderr, "map=%p\n", map);
if( map == MAP_FAILED ){ if( map == MAP_FAILED ){
perror( "mmap" ); perror( "mmap" );
exit(1); exit(1);
} }
for(i=0;i<n_chan;i++){ for(i = 0; i < options.n_chan; i++){
chanlist[i]=CR_PACK(channel+i,range,aref); chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
} }
//prepare_cmd_lib(dev,subdevice,cmd); //prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
prepare_cmd(dev,subdevice,cmd); prepare_cmd(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
ret = comedi_command_test(dev,cmd); ret = comedi_command_test(dev, cmd);
ret = comedi_command_test(dev,cmd); ret = comedi_command_test(dev, cmd);
if(ret!=0){ if(ret != 0){
fprintf(stderr,"command_test failed\n"); fprintf(stderr,"command_test failed\n");
exit(1); exit(1);
} }
dump_cmd(stderr,cmd); dump_cmd(stderr, cmd);
ret = comedi_command(dev,cmd); ret = comedi_command(dev, cmd);
if(ret<0){ if(ret < 0){
comedi_perror("comedi_command"); comedi_perror("comedi_command");
exit(1); exit(1);
} }
@ -88,27 +90,27 @@ int main(int argc, char *argv[])
front = 0; front = 0;
back = 0; back = 0;
while(1){ while(1){
front += comedi_get_buffer_contents(dev,subdevice); front += comedi_get_buffer_contents(dev, options.subdevice);
if(verbose)fprintf(stderr,"front = %d, back = %d\n",front,back); if(options.verbose) fprintf(stderr, "front = %d, back = %d\n", front, back);
if(front<back)break; if(front < back) break;
if(front==back){ if(front == back){
//comedi_poll(dev,subdevice); //comedi_poll(dev, options.subdevice);
usleep(10000); usleep(10000);
continue; continue;
} }
for(i=back;i<front;i+=sizeof(sampl_t)){ for(i = back; i < front; i += sizeof(sampl_t)){
static int col = 0; static int col = 0;
printf("%d ",*(sampl_t *)(map+(i%size))); printf("%d ",*(sampl_t *)(map + (i % size)));
col++; col++;
if(col==n_chan){ if(col == options.n_chan){
printf("\n"); printf("\n");
col=0; col = 0;
} }
} }
ret = comedi_mark_buffer_read(dev,subdevice,front-back); ret = comedi_mark_buffer_read(dev, options.subdevice, front - back);
if(ret<0){ if(ret < 0){
comedi_perror("comedi_mark_buffer_read"); comedi_perror("comedi_mark_buffer_read");
break; break;
} }
@ -118,11 +120,11 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
int prepare_cmd_lib(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 ret; int ret;
ret = comedi_get_cmd_generic_timed(dev,subdevice,cmd,1e9/freq); ret = comedi_get_cmd_generic_timed(dev, subdevice, cmd, period_nanosec);
if(ret<0){ if(ret<0){
comedi_perror("comedi_get_cmd_generic_timed\n"); comedi_perror("comedi_get_cmd_generic_timed\n");
return ret; return ret;
@ -132,12 +134,12 @@ int prepare_cmd_lib(comedi_t *dev,int subdevice,comedi_cmd *cmd)
cmd->chanlist_len = n_chan; cmd->chanlist_len = n_chan;
cmd->scan_end_arg = 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; 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)); 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->start_arg = 0;
cmd->scan_begin_src = TRIG_TIMER; 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_src = TRIG_TIMER;
cmd->convert_arg = 1; cmd->convert_arg = 1;

View file

@ -29,28 +29,30 @@ int main(int argc, char *argv[])
{ {
lsampl_t data; lsampl_t data;
int ret; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
data = value; data = options.value;
if(verbose){ if(options.verbose){
printf("writing %d to device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", 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){ if(ret < 0){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
printf("%d\n",data); printf("%d\n", data);
return 0; return 0;
} }

View file

@ -33,45 +33,40 @@
#define BUFSZ 1000 #define BUFSZ 1000
sampl_t buf[BUFSZ]; sampl_t buf[BUFSZ];
int n_chans = 1; const int n_chans = 1;
int n_scans = 10; const int n_scans = 10;
unsigned int chanlist[4]; unsigned int chanlist[4];
comedi_t *device; 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); 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[]) int main(int argc, char *argv[])
{ {
comedi_cmd cmd; comedi_cmd cmd;
int i; 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){ if(!device){
perror(filename); perror(options.filename);
exit(1); exit(1);
} }
fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK); fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK);
for(i=0;i<n_chans;i++){ for(i = 0; i < n_chans; i++){
chanlist[i]=CR_PACK(channel+i,range,aref); chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
} }
prepare_cmd(device,&cmd); prepare_cmd(device, &cmd, options.subdevice);
do_cmd(device,&cmd); do_cmd(device, &cmd);
return 0; return 0;
} }
@ -163,7 +158,7 @@ void do_cmd(comedi_t *dev,comedi_cmd *cmd)
* of scans measured is 10. This is analogous to the old mode2 * of scans measured is 10. This is analogous to the old mode2
* acquisition. * 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(*cmd)); memset(cmd,0,sizeof(*cmd));

View file

@ -37,36 +37,33 @@ comedi_t *device;
#define BUFSZ 1024 #define BUFSZ 1024
sampl_t buf[BUFSZ]; sampl_t buf[BUFSZ];
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); void do_cmd(comedi_t *dev,comedi_cmd *cmd);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *fn = NULL;
comedi_t *dev; comedi_t *dev;
comedi_cmd cmd; comedi_cmd cmd;
int ret; int ret;
struct parsed_options options;
parse_options(argc,argv); init_parsed_options(&options);
options.channel = -1;
parse_options(&options, argc, argv);
//fn = "/dev/comedi1"; dev = comedi_open(options.filename);
fn = "/dev/comedi0";
dev = comedi_open(fn);
if(!dev){ if(!dev){
perror(fn); perror(options.filename);
exit(1); exit(1);
} }
device = dev; device = dev;
subdevice = 0; if(options.channel >= 0) pin_data = options.channel;
if(channel)pin_data=channel;
ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK); ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK);
if(ret<0)perror("fcntl"); if(ret<0)perror("fcntl");
prepare_cmd(dev,&cmd); prepare_cmd(dev, &cmd, options.subdevice);
do_cmd(dev,&cmd); do_cmd(dev,&cmd);
@ -198,7 +195,7 @@ unsigned int chanlist[16];
* of scans measured is 10. This is analogous to the old mode2 * of scans measured is 10. This is analogous to the old mode2
* acquisition. * 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)); memset(cmd,0,sizeof(comedi_cmd));

View file

@ -33,45 +33,40 @@
#define BUFSZ 1000 #define BUFSZ 1000
sampl_t buf[BUFSZ]; sampl_t buf[BUFSZ];
int n_chans = 1; const int n_chans = 1;
int n_scans = 10; const int n_scans = 10;
unsigned int chanlist[4]; unsigned int chanlist[4];
comedi_t *device; 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); 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[]) int main(int argc, char *argv[])
{ {
comedi_cmd cmd; comedi_cmd cmd;
int i; 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){ if(!device){
perror(filename); perror(options.filename);
exit(1); exit(1);
} }
fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK); fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK);
for(i=0;i<n_chans;i++){ for(i = 0; i < n_chans; i++){
chanlist[i]=CR_PACK(channel+i,range,aref); chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
} }
prepare_cmd(device,&cmd); prepare_cmd(device, &cmd, options.subdevice);
do_cmd(device,&cmd); do_cmd(device, &cmd);
return 0; return 0;
} }
@ -161,7 +156,7 @@ void do_cmd(comedi_t *dev,comedi_cmd *cmd)
* of scans measured is 10. This is analogous to the old mode2 * of scans measured is 10. This is analogous to the old mode2
* acquisition. * 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(*cmd)); memset(cmd,0,sizeof(*cmd));

View file

@ -32,7 +32,7 @@ int wait2 = usec_to_nsec(0);
comedi_t *device; comedi_t *device;
void write_bits(int bits); void write_bits(int subdevice, int bits);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -40,31 +40,39 @@ int main(int argc, char *argv[])
int ret; int ret;
int stype; int stype;
int i; int i;
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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
}
if(options.subdevice < 0)
{
options.subdevice = comedi_find_subdevice_by_type(device, COMEDI_SUBD_DIO, 0);
if(options.subdevice < 0){
fprintf(stderr,"No dio subdevice found.\n");
exit(-1);
}
}
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);
} }
subdevice = 2; printf("configuring pin %d for output...\n", chan_dat);
ret = comedi_dio_config(device, options.subdevice, chan_dat, COMEDI_OUTPUT);
stype = comedi_get_subdevice_type(device,subdevice); printf("configuring pin %d for output...\n", chan_clk);
if(stype!=COMEDI_SUBD_DIO){ ret = comedi_dio_config(device, options.subdevice, chan_clk, COMEDI_OUTPUT);
printf("%d is not a digital I/O subdevice\n",subdevice);
exit(0);
}
printf("configuring pin %d for output...\n",chan_dat); for(i = 0; i < 0x100; i++){
ret=comedi_dio_config(device,subdevice,chan_dat,COMEDI_OUTPUT); write_bits(options.subdevice, i);
printf("configuring pin %d for output...\n",chan_clk);
ret=comedi_dio_config(device,subdevice,chan_clk,COMEDI_OUTPUT);
for(i=0;i<0x100;i++){
write_bits(i);
} }
//write_bits(0xa5); //write_bits(0xa5);
@ -72,7 +80,7 @@ int main(int argc, char *argv[])
} }
void write_bits(int bits) void write_bits(int subdevice, int bits)
{ {
comedi_insnlist il; comedi_insnlist il;
comedi_insn insn[5]; comedi_insn insn[5];

View file

@ -69,38 +69,35 @@ int count;
#define BUFSZ 1024 #define BUFSZ 1024
sampl_t buf[BUFSZ]; sampl_t buf[BUFSZ];
void do_cmd_1(comedi_t *dev); void do_cmd_1(comedi_t *dev, int subdevice);
void do_cmd_2(comedi_t *dev); void do_cmd_2(comedi_t *dev);
void do_cmd(comedi_t *dev,comedi_cmd *cmd); void do_cmd(comedi_t *dev,comedi_cmd *cmd);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *fn = NULL;
comedi_t *dev;
struct sigaction sa; struct sigaction sa;
int ret; int ret;
sigset_t sigset; sigset_t sigset;
int flags; int flags;
struct parsed_options options;
//fn = "/dev/comedi1"; init_parsed_options(&options);
fn = "/dev/comedi0"; parse_options(&options, argc, argv);
dev = comedi_open(fn); device = comedi_open(options.filename);
if(!dev){ if(!device){
perror(fn); perror(options.filename);
exit(1); exit(1);
} }
device = dev;
subdevice = 0;
out_subd = 2; out_subd = 2;
config_output(); config_output();
fcntl(comedi_fileno(dev),F_SETOWN,getpid()); fcntl(comedi_fileno(device), F_SETOWN, getpid());
flags = fcntl(comedi_fileno(dev),F_GETFL); flags = fcntl(comedi_fileno(device),F_GETFL);
ret = fcntl(comedi_fileno(dev),F_SETFL,flags|O_ASYNC); ret = fcntl(comedi_fileno(device),F_SETFL,flags|O_ASYNC);
//ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK|O_ASYNC); //ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC);
if(ret<0)perror("fcntl"); if(ret<0)perror("fcntl");
memset(&sa,0,sizeof(sa)); memset(&sa,0,sizeof(sa));
@ -123,7 +120,7 @@ int main(int argc, char *argv[])
} }
#endif #endif
do_cmd_1(dev); do_cmd_1(device, options.subdevice);
return 0; return 0;
} }
@ -196,14 +193,14 @@ unsigned int chanlist[0];
* of scans measured is 10. This is analogous to the old mode2 * of scans measured is 10. This is analogous to the old mode2
* acquisition. * acquisition.
*/ */
void do_cmd_1(comedi_t *dev) void do_cmd_1(comedi_t *dev, int subdevice)
{ {
comedi_cmd cmd; comedi_cmd cmd;
memset(&cmd,0,sizeof(cmd)); memset(&cmd,0,sizeof(cmd));
/* the subdevice that the command is sent to */ /* the subdevice that the command is sent to */
cmd.subdev = subdevice; cmd.subdev = subdevice;
/* flags */ /* flags */
cmd.flags = TRIG_WAKE_EOS; cmd.flags = TRIG_WAKE_EOS;

View file

@ -28,33 +28,35 @@ int main(int argc, char *argv[])
int ret; int ret;
comedi_sv_t sv; comedi_sv_t sv;
double volts; 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){ if(!device){
comedi_perror(filename); comedi_perror(options.filename);
exit(0); exit(-1);
} }
if(verbose){ if(options.verbose){
printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n", 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.range = options.range;
sv.aref=aref; sv.aref = options.aref;
sv.n=100; sv.n = 100;
ret=comedi_sv_measure(&sv,&volts); ret = comedi_sv_measure(&sv, &volts);
if(ret<0){ if(ret < 0){
comedi_perror("comedi_sv_measure()"); comedi_perror("comedi_sv_measure()");
exit(0); exit(-1);
} }
printf("%g\n",volts); printf("%g\n", volts);
return 0; return 0;
} }