From f6a4444de89c9e45ce1cc37835433652db3a259b Mon Sep 17 00:00:00 2001 From: Frank Mori Hess Date: Fri, 8 Feb 2008 16:23:36 +0000 Subject: [PATCH] Switched to use comedi_to_physical instead of deprecated comedi_to_phys. --- demo/tut2.c | 96 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 23 deletions(-) diff --git a/demo/tut2.c b/demo/tut2.c index 4416602..0e9372c 100644 --- a/demo/tut2.c +++ b/demo/tut2.c @@ -3,6 +3,7 @@ * Part of Comedilib * * Copyright (c) 1999,2000 David A. Schleef + * Copyright (c) 2008 Frank Mori Hess * * This file may be freely modified, distributed, and combined with * other software, as long as proper attribution is given in the @@ -10,6 +11,7 @@ */ #include /* for printf() */ +#include #include int subdev = 0; /* change this to your input subdevice */ @@ -18,41 +20,89 @@ int range = 0; /* more on this later */ int aref = AREF_GROUND; /* more on this later */ const char filename[] = "/dev/comedi0"; +/* initialize a comedi_polynomial_t so it can be used with comedi_to_physical */ +int get_converter(comedi_t *device, unsigned subdevice, unsigned channel, + unsigned range, comedi_polynomial_t *converter) +{ + int retval; + int flags; + + flags = comedi_get_subdevice_flags(device, subdevice); + if(flags < 0) + { + comedi_perror("comedi_get_subdevice_flags"); + return -1; + } + + if(flags & SDF_SOFT_CALIBRATED) /* board uses software calibration */ + { + char *calibration_file_path = comedi_get_default_calibration_path(device); + + /* parse a calibration file which was produced by the + comedi_soft_calibrate program */ + comedi_calibration_t* parsed_calibration = + comedi_parse_calibration_file(calibration_file_path); + free(calibration_file_path); + if(parsed_calibration == NULL) + { + comedi_perror("comedi_parse_calibration_file"); + return -1; + } + + /* get the comedi_polynomial_t for the subdevice/channel/range + we are interested in */ + retval = comedi_get_softcal_converter(subdevice, channel, range, + COMEDI_TO_PHYSICAL, parsed_calibration, converter); + comedi_cleanup_calibration(parsed_calibration); + if(retval < 0) + { + comedi_perror("comedi_get_softcal_converter"); + return -1; + } + }else /* board uses hardware calibration */ + { + retval = comedi_get_hardcal_converter(device, subdevice, channel, range, + COMEDI_TO_PHYSICAL, converter); + if(retval < 0) + { + comedi_perror("comedi_get_hardcal_converter"); + return -1; + } + } + + return 0; +} + int main(int argc, char *argv[]) { - comedi_t *cf; + comedi_t *it; lsampl_t data; - int maxdata; - double volts; - comedi_range *cr; + double physical_value; int retval; - - cf = comedi_open(filename); - if(cf == NULL) - { - comedi_perror(filename); - return 1; - } - maxdata = comedi_get_maxdata(cf, subdev, chan); - if(maxdata == 0) + comedi_polynomial_t converter; + + it = comedi_open(filename); + if(it == NULL) { comedi_perror(filename); - return 1; - } - cr = comedi_get_range(cf, subdev, chan, range); - if(cr == NULL) + return -1; + } + + retval = comedi_data_read(it, subdev, chan, range, aref, &data); + if(retval < 0) { comedi_perror(filename); - return 1; + return -1; } - retval = comedi_data_read(cf, subdev, chan, range, aref, &data); + + retval = get_converter(it, subdev, chan, range, &converter); if(retval < 0) { - comedi_perror(filename); - return 1; + return -1; } - volts = comedi_to_phys(data, cr, maxdata); - printf("%d %g\n", data, volts); + + physical_value = comedi_to_physical(data, &converter); + printf("%d %g\n", data, physical_value); return 0; } -- 2.26.2