Cleaned up mixing of spaces/tabs for indentation probably done by emacs.
[comedilib.git] / demo / inp.c
1 /*
2  * A very small one-shot input demo
3  * Part of Comedilib
4  *
5  * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
6  *
7  * This file may be freely modified, distributed, and combined with
8  * other software, as long as proper attribution is given in the
9  * source code.
10  */
11 /*
12    A little input demo
13  */
14
15 #include <stdio.h>
16 #include <comedilib.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <ctype.h>
23 #include <math.h>
24 #include "examples.h"
25
26 comedi_t *device;
27
28
29 int main(int argc, char *argv[])
30 {
31         lsampl_t data;
32         int ret;
33         comedi_range * range_info;
34         lsampl_t maxdata;
35         double physical_value;
36         struct parsed_options options;
37
38         init_parsed_options(&options);
39         parse_options(&options, argc, argv);
40
41         device = comedi_open(options.filename);
42         if(!device){
43                 comedi_perror(options.filename);
44                 exit(-1);
45         }
46
47         if(options.verbose){
48                 printf("measuring device=%s subdevice=%d channel=%d range=%d analog reference=%d\n",
49                         options.filename, options.subdevice, options.channel, options.range, options.aref);
50         }
51
52         ret = comedi_data_read(device, options.subdevice, options.channel, options.range, options.aref, &data);
53         if(ret < 0){
54                 comedi_perror(options.filename);
55                 exit(-1);
56         }
57
58         if(options.physical) {
59                 comedi_set_global_oor_behavior(COMEDI_OOR_NAN);
60                 range_info = comedi_get_range(device, options.subdevice, options.channel, options.range);
61                 maxdata = comedi_get_maxdata(device, options.subdevice, options.channel);
62                 if(options.verbose) {
63                         printf("[0,%d] -> [%g,%g]\n", maxdata,
64                                 range_info->min, range_info->max);
65                 }
66                 physical_value = comedi_to_phys(data, range_info, maxdata);
67                 if(isnan(physical_value)) {
68                         printf("Out of range [%g,%g]",
69                                 range_info->min, range_info->max);
70                 } else {
71                         printf("%g", physical_value);
72                         switch(range_info->unit) {
73                                 case UNIT_volt: printf(" V"); break;
74                                 case UNIT_mA: printf(" mA"); break;
75                                 case UNIT_none: break;
76                                 default: printf(" (unknown unit %d)",
77                                         range_info->unit);
78                         }
79                         if(options.verbose) {
80                                 printf(" (%lu raw units)", (unsigned long)data);
81                         }
82                 }
83         } else {
84                 printf("%lu", (unsigned long)data);
85         }
86         printf("\n");
87
88         return 0;
89 }
90