comedilib/demo/tut2.c

69 lines
1.8 KiB
C
Raw Permalink Normal View History

2000-10-19 06:28:27 +00:00
/*
* Tutorial example #2
* Part of Comedilib
*
* Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
* Copyright (c) 2008 Frank Mori Hess <fmhess@users.sourceforge.net>
2000-10-19 06:28:27 +00:00
*
* This file may be freely modified, distributed, and combined with
* other software, as long as proper attribution is given in the
* source code.
*/
2000-02-02 05:14:23 +00:00
2000-11-30 01:29:10 +00:00
#include <stdio.h> /* for printf() */
#include <stdlib.h>
2000-10-19 06:28:27 +00:00
#include <comedilib.h>
#include <ctype.h>
#include <math.h>
2000-02-02 05:14:23 +00:00
2000-11-30 01:29:10 +00:00
int subdev = 0; /* change this to your input subdevice */
int chan = 0; /* change this to your channel */
int range = 0; /* more on this later */
int aref = AREF_GROUND; /* more on this later */
2005-02-24 20:46:56 +00:00
const char filename[] = "/dev/comedi0";
2000-02-02 05:14:23 +00:00
2000-11-30 01:29:10 +00:00
int main(int argc, char *argv[])
2000-02-02 05:14:23 +00:00
{
comedi_t *device;
2000-11-30 01:29:10 +00:00
lsampl_t data;
double physical_value;
2005-02-24 20:46:56 +00:00
int retval;
comedi_range *range_info;
lsampl_t maxdata;
device = comedi_open(filename);
if (device == NULL) {
2005-02-24 20:46:56 +00:00
comedi_perror(filename);
return 1;
}
retval = comedi_data_read(device, subdev, chan, range, aref,
&data);
if (retval < 0) {
2005-02-24 20:46:56 +00:00
comedi_perror(filename);
return 1;
2005-02-24 20:46:56 +00:00
}
comedi_set_global_oor_behavior(COMEDI_OOR_NAN);
range_info = comedi_get_range(device, subdev, chan, range);
maxdata = comedi_get_maxdata(device, subdev, chan);
printf("[0,%d] -> [%g,%g]\n", maxdata,
range_info->min, range_info->max);
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);
switch(range_info->unit) {
case UNIT_volt: printf(" V"); break;
case UNIT_mA: printf(" mA"); break;
case UNIT_none: break;
default: printf(" (unknown unit %d)",
range_info->unit);
}
printf(" (%lu in raw units)\n", (unsigned long)data);
2005-02-24 20:46:56 +00:00
}
2000-11-30 01:29:10 +00:00
return 0;
2000-02-02 05:14:23 +00:00
}