Made parse_options() write options to a struct instead of passing
them through global variables.
This commit is contained in:
parent
c7a581399a
commit
fbdf65b4f5
24 changed files with 503 additions and 467 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<<WAVEFORM_SHIFT);
|
||||
adder = waveform_frequency / update_frequency * (1 << 16) * (1 << WAVEFORM_SHIFT);
|
||||
|
||||
dds_init_function();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* demo for changing between different calibrations
|
||||
* demo for changing between different (hardware-based) calibrations
|
||||
* Part of Comedilib
|
||||
*
|
||||
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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");
|
||||
|
|
130
demo/cmd.c
130
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;i<n_chan;i++){
|
||||
chanlist[i]=CR_PACK(channel+i,range,aref);
|
||||
range_info[i]=comedi_get_range(dev,subdevice,channel,range);
|
||||
maxdata[i]=comedi_get_maxdata(dev,subdevice,channel);
|
||||
for(i = 0; i < options.n_chan; i++){
|
||||
chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
|
||||
range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range);
|
||||
maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel);
|
||||
}
|
||||
|
||||
/* prepare_cmd_lib() uses a Comedilib routine to find a
|
||||
* good command for the device. prepare_cmd() explicitly
|
||||
* creates a command, which may not work for your device. */
|
||||
prepare_cmd_lib(dev,subdevice,cmd);
|
||||
//prepare_cmd(dev,subdevice,cmd);
|
||||
|
||||
fprintf(stderr,"command before testing:\n");
|
||||
dump_cmd(stderr,cmd);
|
||||
prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
|
||||
//prepare_cmd(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
|
||||
|
||||
fprintf(stderr, "command before testing:\n");
|
||||
dump_cmd(stderr, cmd);
|
||||
|
||||
/* comedi_command_test() tests a command to see if the
|
||||
* 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,
|
||||
* if you can't get a valid command in two tests, the original
|
||||
* command wasn't specified very well. */
|
||||
ret = comedi_command_test(dev,cmd);
|
||||
if(ret<0){
|
||||
ret = comedi_command_test(dev, cmd);
|
||||
if(ret < 0){
|
||||
comedi_perror("comedi_command_test");
|
||||
if(errno==EIO){
|
||||
if(errno == EIO){
|
||||
fprintf(stderr,"Ummm... this subdevice doesn't support commands\n");
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
fprintf(stderr,"first test returned %d (%s)\n",ret,
|
||||
fprintf(stderr,"first test returned %d (%s)\n", ret,
|
||||
cmdtest_messages[ret]);
|
||||
dump_cmd(stderr,cmd);
|
||||
dump_cmd(stderr, cmd);
|
||||
|
||||
ret = comedi_command_test(dev,cmd);
|
||||
if(ret<0){
|
||||
ret = comedi_command_test(dev, cmd);
|
||||
if(ret < 0){
|
||||
comedi_perror("comedi_command_test");
|
||||
exit(1);
|
||||
}
|
||||
fprintf(stderr,"second test returned %d (%s)\n",ret,
|
||||
fprintf(stderr,"second test returned %d (%s)\n", ret,
|
||||
cmdtest_messages[ret]);
|
||||
if(ret!=0){
|
||||
dump_cmd(stderr,cmd);
|
||||
fprintf(stderr,"Error preparing command\n");
|
||||
dump_cmd(stderr, cmd);
|
||||
fprintf(stderr, "Error preparing command\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* this is only for informational purposes */
|
||||
gettimeofday(&start,NULL);
|
||||
fprintf(stderr,"start time: %ld.%06ld\n",start.tv_sec,start.tv_usec);
|
||||
gettimeofday(&start, NULL);
|
||||
fprintf(stderr,"start time: %ld.%06ld\n", start.tv_sec, start.tv_usec);
|
||||
|
||||
/* start the command */
|
||||
ret=comedi_command(dev,cmd);
|
||||
if(ret<0){
|
||||
ret = comedi_command(dev, cmd);
|
||||
if(ret < 0){
|
||||
comedi_perror("comedi_command");
|
||||
exit(1);
|
||||
}
|
||||
subdev_flags = comedi_get_subdevice_flags(dev, subdevice);
|
||||
subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice);
|
||||
while(1){
|
||||
ret=read(comedi_fileno(dev),buf,BUFSZ);
|
||||
if(ret<0){
|
||||
ret = read(comedi_fileno(dev),buf,BUFSZ);
|
||||
if(ret < 0){
|
||||
/* some error occurred */
|
||||
perror("read");
|
||||
break;
|
||||
}else if(ret==0){
|
||||
}else if(ret == 0){
|
||||
/* reached stop condition */
|
||||
break;
|
||||
}else{
|
||||
static int col = 0;
|
||||
int bytes_per_sample;
|
||||
total+=ret;
|
||||
if(verbose)fprintf(stderr,"read %d %d\n",ret,total);
|
||||
total += ret;
|
||||
if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total);
|
||||
if(subdev_flags & SDF_LSAMPL)
|
||||
bytes_per_sample = sizeof(lsampl_t);
|
||||
else
|
||||
|
@ -172,9 +174,9 @@ int main(int argc, char *argv[])
|
|||
} else {
|
||||
raw = ((sampl_t *)buf)[i];
|
||||
}
|
||||
print_datum(raw,col);
|
||||
print_datum(raw, col, options.physical);
|
||||
col++;
|
||||
if(col==n_chan){
|
||||
if(col == options.n_chan){
|
||||
printf("\n");
|
||||
col=0;
|
||||
}
|
||||
|
@ -184,15 +186,15 @@ int main(int argc, char *argv[])
|
|||
|
||||
/* this is only for informational purposes */
|
||||
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;
|
||||
if(end.tv_usec<start.tv_usec){
|
||||
end.tv_sec -= start.tv_sec;
|
||||
if(end.tv_usec < start.tv_usec){
|
||||
end.tv_sec--;
|
||||
end.tv_usec+=1000000;
|
||||
end.tv_usec += 1000000;
|
||||
}
|
||||
end.tv_usec-=start.tv_usec;
|
||||
fprintf(stderr,"time: %ld.%06ld\n",end.tv_sec,end.tv_usec);
|
||||
end.tv_usec -= start.tv_usec;
|
||||
fprintf(stderr,"time: %ld.%06ld\n", end.tv_sec, end.tv_usec);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -201,7 +203,7 @@ int main(int argc, char *argv[])
|
|||
* This prepares a command in a pretty generic way. We ask the
|
||||
* library to create a stock command that supports periodic
|
||||
* 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;
|
||||
|
||||
|
@ -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
|
||||
* command for a particular board. If it returns -1,
|
||||
* 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){
|
||||
printf("comedi_get_cmd_generic_timed failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Modify parts of the command */
|
||||
cmd->chanlist = 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,69 +15,67 @@
|
|||
#include <stdlib.h>
|
||||
#include "examples.h"
|
||||
|
||||
static char * const default_filename = "/dev/comedi0";
|
||||
|
||||
char *filename="/dev/comedi0";
|
||||
int verbose = 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 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;
|
||||
|
||||
|
||||
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;
|
||||
|
|
26
demo/dio.c
26
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;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include <stdlib.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);
|
||||
|
||||
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<n;i++){
|
||||
ret=comedi_data_read(it,subd,i,0,0,&data);
|
||||
ptr[i]=data;
|
||||
if(ret<0){
|
||||
for(i = 0; i < n; i++){
|
||||
ret = comedi_data_read(it, options.subdevice, i, 0, 0, &data);
|
||||
ptr[i] = data;
|
||||
if(ret < 0){
|
||||
comedi_perror("comedi_data_read");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -8,24 +8,27 @@
|
|||
* Definitions of some of the common code.
|
||||
*/
|
||||
|
||||
extern char *filename;
|
||||
extern int verbose_flag;
|
||||
extern comedi_t *device;
|
||||
|
||||
extern int value;
|
||||
extern int subdevice;
|
||||
extern int channel;
|
||||
extern int aref;
|
||||
extern int range;
|
||||
extern int physical;
|
||||
extern int verbose;
|
||||
extern int n_chan;
|
||||
extern int n_scan;
|
||||
extern double freq;
|
||||
struct parsed_options
|
||||
{
|
||||
char *filename;
|
||||
double value;
|
||||
int subdevice;
|
||||
int channel;
|
||||
int aref;
|
||||
int range;
|
||||
int physical;
|
||||
int verbose;
|
||||
int n_chan;
|
||||
int n_scan;
|
||||
double freq;
|
||||
};
|
||||
|
||||
int parse_options(int argc, char *argv[]);
|
||||
char *cmd_src(int src,char *buf);
|
||||
void dump_cmd(FILE *file,comedi_cmd *cmd);
|
||||
extern void init_parsed_options(struct parsed_options *options);
|
||||
extern int parse_options(struct parsed_options *options, int argc, char *argv[]);
|
||||
extern char *cmd_src(int src,char *buf);
|
||||
extern void dump_cmd(FILE *file,comedi_cmd *cmd);
|
||||
|
||||
|
||||
#define sec_to_nsec(x) ((x)*1000000000)
|
||||
|
|
32
demo/info.c
32
demo/info.c
|
@ -49,24 +49,26 @@ int main(int argc,char *argv[])
|
|||
int n_ranges;
|
||||
int subdev_flags;
|
||||
comedi_range *rng;
|
||||
|
||||
parse_options(argc,argv);
|
||||
struct parsed_options options;
|
||||
|
||||
it=comedi_open(filename);
|
||||
init_parsed_options(&options);
|
||||
parse_options(&options, argc, argv);
|
||||
|
||||
it = comedi_open(options.filename);
|
||||
if(!it){
|
||||
comedi_perror(filename);
|
||||
comedi_perror(options.filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("overall info:\n");
|
||||
printf(" version code: 0x%06x\n",comedi_get_version_code(it));
|
||||
printf(" driver name: %s\n",comedi_get_driver_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));
|
||||
|
||||
for(i=0;i<n_subdevices;i++){
|
||||
printf(" version code: 0x%06x\n", comedi_get_version_code(it));
|
||||
printf(" driver name: %s\n", comedi_get_driver_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));
|
||||
|
||||
for(i = 0; i < n_subdevices; 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]);
|
||||
if(type==COMEDI_SUBD_UNUSED)
|
||||
continue;
|
||||
|
@ -106,7 +108,7 @@ int main(int argc,char *argv[])
|
|||
printf(" command:\n");
|
||||
get_command_stuff(it,i);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -114,11 +116,11 @@ char *tobinary(char *s,int bits,int n)
|
|||
{
|
||||
int bit=1<<n;
|
||||
char *t=s;
|
||||
|
||||
|
||||
for(;bit;bit>>=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);
|
||||
}
|
||||
}
|
||||
|
|
36
demo/inp.c
36
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);
|
||||
}
|
||||
}
|
||||
|
|
44
demo/inpn.c
44
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<n_chans;chan++){
|
||||
printf("%d: ",chan);
|
||||
n_ranges = comedi_get_n_ranges(device, options.subdevice, chan);
|
||||
|
||||
n_ranges=comedi_get_n_ranges(device,subdevice,chan);
|
||||
|
||||
maxdata=comedi_get_maxdata(device,subdevice,chan);
|
||||
for(range=0;range<n_ranges;range++){
|
||||
comedi_data_read(device,subdevice,chan,range,aref,&data);
|
||||
voltage=comedi_to_phys(data,comedi_get_range(device,subdevice,chan,range),maxdata);
|
||||
printf("%g ",voltage);
|
||||
maxdata = comedi_get_maxdata(device, options.subdevice, chan);
|
||||
for(range = 0; range < n_ranges; range++){
|
||||
comedi_data_read(device, options.subdevice, chan, options.range, options.aref, &data);
|
||||
voltage = comedi_to_phys(data, comedi_get_range(device, options.subdevice, chan, options.range), maxdata);
|
||||
printf("%g ", voltage);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
44
demo/insn.c
44
demo/insn.c
|
@ -9,7 +9,7 @@
|
|||
* source code.
|
||||
*/
|
||||
/*
|
||||
This example shows how to use instructions, i.e., comedi_insns.
|
||||
This example shows how to use instructions, i.e., comedi_insns.
|
||||
|
||||
Using instructions directly, as in this example, is not recommended
|
||||
for the beginner. Use the higher-level functions such as
|
||||
|
@ -47,23 +47,25 @@ int main(int argc, char *argv[])
|
|||
comedi_insnlist il;
|
||||
struct timeval t1,t2;
|
||||
lsampl_t data[MAX_SAMPLES];
|
||||
struct parsed_options options;
|
||||
|
||||
n_scan = 10; /* override default n_scan value to something more suitable */
|
||||
parse_options(argc,argv);
|
||||
if( n_scan > 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;i<n_scan;i++){
|
||||
printf("%d\n",data[i]);
|
||||
printf("initial time: %ld.%06ld\n", t1.tv_sec, t1.tv_usec);
|
||||
for(i = 0; i < options.n_scan; 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+
|
||||
(t2.tv_usec-t1.tv_usec));
|
||||
printf("difference (us): %ld\n",(t2.tv_sec-t1.tv_sec) * 1000000 +
|
||||
(t2.tv_usec - t1.tv_usec));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Requirements:
|
||||
* Requirements:
|
||||
* - A board with a digital output subdevice and a subdevice that
|
||||
* can trigger on an external digital line. A parallel port
|
||||
* satisfies these requirements.
|
||||
|
@ -58,7 +58,7 @@ sampl_t buf[BUFSZ];
|
|||
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_toggle(void);
|
||||
|
||||
|
@ -125,19 +125,19 @@ void do_toggle(void)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *fn = NULL;
|
||||
int ret;
|
||||
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){
|
||||
perror(fn);
|
||||
perror(options.filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
subdevice = 3;
|
||||
out_subd = 0;
|
||||
|
||||
config_output();
|
||||
|
@ -156,7 +156,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
#endif
|
||||
|
||||
prepare_cmd(device,&cmd);
|
||||
prepare_cmd(device, &cmd, options.subdevice);
|
||||
|
||||
do_cmd(device,&cmd);
|
||||
|
||||
|
@ -224,7 +224,7 @@ void do_cmd(comedi_t *dev,comedi_cmd *cmd)
|
|||
go = 0;
|
||||
}else{
|
||||
//int i;
|
||||
|
||||
|
||||
total+=ret;
|
||||
//printf("read %d %d\n",ret,total);
|
||||
//printf("count = %d\n",count);
|
||||
|
@ -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
|
||||
* 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));
|
||||
|
||||
/* the subdevice that the command is sent to */
|
||||
cmd->subdev = subdevice;
|
||||
cmd->subdev = subdevice;
|
||||
|
||||
/* flags */
|
||||
cmd->flags = TRIG_WAKE_EOS;
|
||||
|
|
74
demo/mmap.c
74
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;i<n_chan;i++){
|
||||
chanlist[i]=CR_PACK(channel+i,range,aref);
|
||||
for(i = 0; i < options.n_chan; i++){
|
||||
chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
|
||||
}
|
||||
|
||||
//prepare_cmd_lib(dev,subdevice,cmd);
|
||||
prepare_cmd(dev,subdevice,cmd);
|
||||
|
||||
ret = comedi_command_test(dev,cmd);
|
||||
//prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, 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);
|
||||
|
||||
if(ret!=0){
|
||||
ret = comedi_command_test(dev, cmd);
|
||||
|
||||
if(ret != 0){
|
||||
fprintf(stderr,"command_test failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
dump_cmd(stderr,cmd);
|
||||
dump_cmd(stderr, cmd);
|
||||
|
||||
ret = comedi_command(dev,cmd);
|
||||
if(ret<0){
|
||||
ret = comedi_command(dev, cmd);
|
||||
if(ret < 0){
|
||||
comedi_perror("comedi_command");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -88,27 +90,27 @@ int main(int argc, char *argv[])
|
|||
front = 0;
|
||||
back = 0;
|
||||
while(1){
|
||||
front += comedi_get_buffer_contents(dev,subdevice);
|
||||
if(verbose)fprintf(stderr,"front = %d, back = %d\n",front,back);
|
||||
if(front<back)break;
|
||||
if(front==back){
|
||||
//comedi_poll(dev,subdevice);
|
||||
front += comedi_get_buffer_contents(dev, options.subdevice);
|
||||
if(options.verbose) fprintf(stderr, "front = %d, back = %d\n", front, back);
|
||||
if(front < back) break;
|
||||
if(front == back){
|
||||
//comedi_poll(dev, options.subdevice);
|
||||
usleep(10000);
|
||||
continue;
|
||||
}
|
||||
|
||||
for(i=back;i<front;i+=sizeof(sampl_t)){
|
||||
for(i = back; i < front; i += sizeof(sampl_t)){
|
||||
static int col = 0;
|
||||
printf("%d ",*(sampl_t *)(map+(i%size)));
|
||||
printf("%d ",*(sampl_t *)(map + (i % size)));
|
||||
col++;
|
||||
if(col==n_chan){
|
||||
if(col == options.n_chan){
|
||||
printf("\n");
|
||||
col=0;
|
||||
col = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ret = comedi_mark_buffer_read(dev,subdevice,front-back);
|
||||
if(ret<0){
|
||||
ret = comedi_mark_buffer_read(dev, options.subdevice, front - back);
|
||||
if(ret < 0){
|
||||
comedi_perror("comedi_mark_buffer_read");
|
||||
break;
|
||||
}
|
||||
|
@ -118,11 +120,11 @@ int main(int argc, char *argv[])
|
|||
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;
|
||||
|
||||
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){
|
||||
comedi_perror("comedi_get_cmd_generic_timed\n");
|
||||
return ret;
|
||||
|
@ -132,12 +134,12 @@ int prepare_cmd_lib(comedi_t *dev,int subdevice,comedi_cmd *cmd)
|
|||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
26
demo/outp.c
26
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;
|
||||
}
|
||||
|
|
35
demo/poll.c
35
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<n_chans;i++){
|
||||
chanlist[i]=CR_PACK(channel+i,range,aref);
|
||||
for(i = 0; i < n_chans; i++){
|
||||
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;
|
||||
}
|
||||
|
@ -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
|
||||
* 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));
|
||||
|
||||
|
@ -179,7 +174,7 @@ void prepare_cmd(comedi_t *dev,comedi_cmd *cmd)
|
|||
src=TRIG_EXT and arg=3. */
|
||||
|
||||
/* In this case, we specify using TRIG_NOW to start
|
||||
* acquisition immediately when the command is issued.
|
||||
* acquisition immediately when the command is issued.
|
||||
* The argument of TRIG_NOW is "number of nsec after
|
||||
* NOW", but no driver supports it yet. Also, no driver
|
||||
* currently supports using a start_src other than
|
||||
|
|
|
@ -37,36 +37,33 @@ comedi_t *device;
|
|||
#define BUFSZ 1024
|
||||
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);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *fn = NULL;
|
||||
comedi_t *dev;
|
||||
comedi_cmd cmd;
|
||||
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";
|
||||
fn = "/dev/comedi0";
|
||||
|
||||
dev = comedi_open(fn);
|
||||
dev = comedi_open(options.filename);
|
||||
if(!dev){
|
||||
perror(fn);
|
||||
perror(options.filename);
|
||||
exit(1);
|
||||
}
|
||||
device = dev;
|
||||
|
||||
subdevice = 0;
|
||||
|
||||
if(channel)pin_data=channel;
|
||||
if(options.channel >= 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;i<ret/sizeof(sampl_t);i++){
|
||||
fprintf(stderr,"%d",buf[i]>0xa000);
|
||||
|
@ -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));
|
||||
|
||||
|
|
|
@ -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<n_chans;i++){
|
||||
chanlist[i]=CR_PACK(channel+i,range,aref);
|
||||
for(i = 0; i < n_chans; i++){
|
||||
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;
|
||||
}
|
||||
|
@ -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
|
||||
* 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));
|
||||
|
||||
|
@ -178,7 +173,7 @@ void prepare_cmd(comedi_t *dev,comedi_cmd *cmd)
|
|||
src=TRIG_EXT and arg=3. */
|
||||
|
||||
/* In this case, we specify using TRIG_NOW to start
|
||||
* acquisition immediately when the command is issued.
|
||||
* acquisition immediately when the command is issued.
|
||||
* The argument of TRIG_NOW is "number of nsec after
|
||||
* NOW", but no driver supports it yet. Also, no driver
|
||||
* currently supports using a start_src other than
|
||||
|
|
|
@ -32,7 +32,7 @@ int wait2 = usec_to_nsec(0);
|
|||
|
||||
comedi_t *device;
|
||||
|
||||
void write_bits(int bits);
|
||||
void write_bits(int subdevice, int bits);
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -40,31 +40,39 @@ int main(int argc, char *argv[])
|
|||
int ret;
|
||||
int stype;
|
||||
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){
|
||||
comedi_perror(filename);
|
||||
exit(0);
|
||||
comedi_perror(options.filename);
|
||||
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);
|
||||
if(stype!=COMEDI_SUBD_DIO){
|
||||
printf("%d is not a digital I/O subdevice\n",subdevice);
|
||||
exit(0);
|
||||
}
|
||||
printf("configuring pin %d for output...\n", chan_clk);
|
||||
ret = comedi_dio_config(device, options.subdevice, chan_clk, COMEDI_OUTPUT);
|
||||
|
||||
printf("configuring pin %d for output...\n",chan_dat);
|
||||
ret=comedi_dio_config(device,subdevice,chan_dat,COMEDI_OUTPUT);
|
||||
|
||||
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);
|
||||
for(i = 0; i < 0x100; i++){
|
||||
write_bits(options.subdevice, i);
|
||||
}
|
||||
//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_insn insn[5];
|
||||
|
|
31
demo/sigio.c
31
demo/sigio.c
|
@ -69,38 +69,35 @@ int count;
|
|||
#define BUFSZ 1024
|
||||
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(comedi_t *dev,comedi_cmd *cmd);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *fn = NULL;
|
||||
comedi_t *dev;
|
||||
struct sigaction sa;
|
||||
int ret;
|
||||
sigset_t sigset;
|
||||
int flags;
|
||||
struct parsed_options options;
|
||||
|
||||
//fn = "/dev/comedi1";
|
||||
fn = "/dev/comedi0";
|
||||
init_parsed_options(&options);
|
||||
parse_options(&options, argc, argv);
|
||||
|
||||
dev = comedi_open(fn);
|
||||
if(!dev){
|
||||
perror(fn);
|
||||
device = comedi_open(options.filename);
|
||||
if(!device){
|
||||
perror(options.filename);
|
||||
exit(1);
|
||||
}
|
||||
device = dev;
|
||||
|
||||
subdevice = 0;
|
||||
out_subd = 2;
|
||||
|
||||
config_output();
|
||||
|
||||
fcntl(comedi_fileno(dev),F_SETOWN,getpid());
|
||||
flags = fcntl(comedi_fileno(dev),F_GETFL);
|
||||
ret = fcntl(comedi_fileno(dev),F_SETFL,flags|O_ASYNC);
|
||||
//ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK|O_ASYNC);
|
||||
fcntl(comedi_fileno(device), F_SETOWN, getpid());
|
||||
flags = fcntl(comedi_fileno(device),F_GETFL);
|
||||
ret = fcntl(comedi_fileno(device),F_SETFL,flags|O_ASYNC);
|
||||
//ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC);
|
||||
if(ret<0)perror("fcntl");
|
||||
|
||||
memset(&sa,0,sizeof(sa));
|
||||
|
@ -123,7 +120,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
#endif
|
||||
|
||||
do_cmd_1(dev);
|
||||
do_cmd_1(device, options.subdevice);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -196,14 +193,14 @@ unsigned int chanlist[0];
|
|||
* of scans measured is 10. This is analogous to the old mode2
|
||||
* acquisition.
|
||||
*/
|
||||
void do_cmd_1(comedi_t *dev)
|
||||
void do_cmd_1(comedi_t *dev, int subdevice)
|
||||
{
|
||||
comedi_cmd cmd;
|
||||
|
||||
memset(&cmd,0,sizeof(cmd));
|
||||
|
||||
/* the subdevice that the command is sent to */
|
||||
cmd.subdev = subdevice;
|
||||
cmd.subdev = subdevice;
|
||||
|
||||
/* flags */
|
||||
cmd.flags = TRIG_WAKE_EOS;
|
||||
|
|
32
demo/sv.c
32
demo/sv.c
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Demo of the comedi_sv_*() functions
|
||||
*
|
||||
*
|
||||
* Part of Comedilib
|
||||
*
|
||||
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue