RTXI 1.3
comedilib/demo/tut2.c
Go to the documentation of this file.
00001 /*
00002  * Tutorial example #2
00003  * Part of Comedilib
00004  *
00005  * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
00006  * Copyright (c) 2008 Frank Mori Hess <fmhess@users.sourceforge.net>
00007  *
00008  * This file may be freely modified, distributed, and combined with
00009  * other software, as long as proper attribution is given in the
00010  * source code.
00011  */
00012 
00013 #include <stdio.h>              /* for printf() */
00014 #include <stdlib.h>
00015 #include <comedilib.h>
00016 
00017 int subdev = 0;                 /* change this to your input subdevice */
00018 int chan = 0;                   /* change this to your channel */
00019 int range = 0;                  /* more on this later */
00020 int aref = AREF_GROUND;         /* more on this later */
00021 const char filename[] = "/dev/comedi0";
00022 
00023 /* figure out if we are talking to a hardware-calibrated or software-calibrated board,
00024         then obtain a comedi_polynomial_t which can be used with comedi_to_physical */
00025 int get_converter(comedi_t *device, unsigned subdevice, unsigned channel,
00026         unsigned range, comedi_polynomial_t *converter)
00027 {
00028         int retval;
00029         int flags;
00030 
00031         flags = comedi_get_subdevice_flags(device, subdevice);
00032         if(flags < 0)
00033         {
00034                 comedi_perror("comedi_get_subdevice_flags");
00035                 return -1;
00036         }
00037 
00038         if(flags & SDF_SOFT_CALIBRATED) /* board uses software calibration */
00039         {
00040                 char *calibration_file_path = comedi_get_default_calibration_path(device);
00041 
00042                 /* parse a calibration file which was produced by the
00043                         comedi_soft_calibrate program */
00044                 comedi_calibration_t* parsed_calibration =
00045                         comedi_parse_calibration_file(calibration_file_path);
00046                 free(calibration_file_path);
00047                 if(parsed_calibration == NULL)
00048                 {
00049                         comedi_perror("comedi_parse_calibration_file");
00050                         return -1;
00051                 }
00052 
00053                 /* get the comedi_polynomial_t for the subdevice/channel/range
00054                         we are interested in */
00055                 retval = comedi_get_softcal_converter(subdevice, channel, range,
00056                         COMEDI_TO_PHYSICAL, parsed_calibration, converter);
00057                 comedi_cleanup_calibration(parsed_calibration);
00058                 if(retval < 0)
00059                 {
00060                         comedi_perror("comedi_get_softcal_converter");
00061                         return -1;
00062                 }
00063         }else /* board uses hardware calibration */
00064         {
00065                 retval = comedi_get_hardcal_converter(device, subdevice, channel, range,
00066                         COMEDI_TO_PHYSICAL, converter);
00067                 if(retval < 0)
00068                 {
00069                         comedi_perror("comedi_get_hardcal_converter");
00070                         return -1;
00071                 }
00072         }
00073 
00074         return 0;
00075 }
00076 
00077 int main(int argc, char *argv[])
00078 {
00079         comedi_t *it;
00080         lsampl_t data;
00081         double physical_value;
00082         int retval;
00083         comedi_polynomial_t converter;
00084 
00085         it = comedi_open(filename);
00086         if(it == NULL)
00087         {
00088                 comedi_perror(filename);
00089                 return -1;
00090         }
00091 
00092         retval = comedi_data_read(it, subdev, chan, range, aref, &data);
00093         if(retval < 0)
00094         {
00095                 comedi_perror(filename);
00096                 return -1;
00097         }
00098 
00099         retval = get_converter(it, subdev, chan, range, &converter);
00100         if(retval < 0)
00101         {
00102                 return -1;
00103         }
00104 
00105         physical_value = comedi_to_physical(data, &converter);
00106         printf("%d %g\n", data, physical_value);
00107 
00108         return 0;
00109 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines