Applied patch from David MacMahon <davidm -at- astro -dot- berkeley -dot- edu>.
authorIan Abbott <abbotti@mev.co.uk>
Thu, 20 Jul 2006 11:51:54 +0000 (11:51 +0000)
committerIan Abbott <abbotti@mev.co.uk>
Thu, 20 Jul 2006 11:51:54 +0000 (11:51 +0000)
Adds physical units support to comedilib demo programs.
I changed one of the format strings to help line up values better in the
'cmd' example.

demo/README
demo/cmd.c
demo/common.c
demo/examples.h
demo/inp.c

index 714f344b7b9103fa04190448374a13f57d2a142e..3a35519b9fbaee7efd571a93798b7a9fca9097a4 100644 (file)
@@ -127,4 +127,5 @@ The options are:
   -g           set analog reference to ground
   -o           set analog reference to other
   -m           set analog reference to common
+  -p           display values in physical units
 
index 95f29a257afdc3f00743b8dc04d6f3b7fcf82a6c..ee0ad8c0b1db67702caa1485a53bd12460928d48 100644 (file)
@@ -29,7 +29,9 @@
 char buf[BUFSZ];
 
 #define N_CHANS 256
-unsigned int chanlist[N_CHANS];
+static unsigned int chanlist[N_CHANS];
+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);
@@ -37,6 +39,8 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd);
 
 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
 
+void print_datum(lsampl_t raw, int i);
+
 char *cmdtest_messages[]={
        "success",
        "invalid source",
@@ -55,6 +59,7 @@ int main(int argc, char *argv[])
        int i;
        struct timeval start,end;
        int subdev_flags;
+       lsampl_t raw;
        
        parse_options(argc,argv);
 
@@ -78,9 +83,14 @@ int main(int argc, char *argv[])
                exit(1);
        }
 
+       // Print numbers for clipped inputs
+       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);
        }
 
        /* prepare_cmd_lib() uses a Comedilib routine to find a
@@ -157,10 +167,12 @@ int main(int argc, char *argv[])
                        else
                                bytes_per_sample = sizeof(sampl_t);
                        for(i = 0; i < ret / bytes_per_sample; i++){
-                               if(subdev_flags & SDF_LSAMPL)
-                                       printf("%d ",((lsampl_t *)buf)[i]);
-                               else
-                                       printf("%d ",((sampl_t *)buf)[i]);
+                               if(subdev_flags & SDF_LSAMPL) {
+                                       raw = ((lsampl_t *)buf)[i];
+                               } else {
+                                       raw = ((sampl_t *)buf)[i];
+                               }
+                               print_datum(raw,col);
                                col++;
                                if(col==n_chan){
                                        printf("\n");
@@ -313,4 +325,12 @@ int prepare_cmd(comedi_t *dev,int subdevice,comedi_cmd *cmd)
        return 0;
 }
 
-
+void print_datum(lsampl_t raw, int i) {
+       double physical_value;
+       if(!physical) {
+               printf("%d ",raw);
+       } else {
+               physical_value = comedi_to_phys(raw,range_info[i],maxdata[i]);
+               printf("%#8.6g ",physical_value);
+       }
+}
index a2e73d8497701cb08203632a1855f7efed335c99..c9cef46805e05a0963b27c0b74c2ecc2608fe786 100644 (file)
@@ -17,7 +17,7 @@
 
 
 char *filename="/dev/comedi0";
-int verbose;
+int verbose = 0;
 
 int value=0;
 int subdevice=0;
@@ -27,6 +27,7 @@ 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[])
@@ -34,7 +35,7 @@ int parse_options(int argc, char *argv[])
        int c;
 
 
-       while (-1 != (c = getopt(argc, argv, "a:c:s:r:f:n:N:F:vdgom"))) {
+       while (-1 != (c = getopt(argc, argv, "a:c:s:r:f:n:N:F:pvdgom"))) {
                switch (c) {
                case 'f':
                        filename = optarg;
@@ -60,6 +61,9 @@ int parse_options(int argc, char *argv[])
                case 'F':
                        freq = strtoul(optarg,NULL,0);
                        break;
+               case 'p':
+                       physical = 1;
+                       break;
                case 'v':
                        verbose = 1;
                        break;
index 65dcb59f23b8857595376faa009baa94d760761f..5e9e527d902ddd61d2da1522ece32d1c3bccf2c9 100644 (file)
@@ -17,6 +17,7 @@ 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;
index 0ab270cc7c6e3ec66643512cf407e6020d7e37fe..bd8baea4e9e6032689e3aa94c046e7cd87582e27 100644 (file)
@@ -20,6 +20,7 @@
 #include <errno.h>
 #include <getopt.h>
 #include <ctype.h>
+#include <math.h>
 #include "examples.h"
 
 comedi_t *device;
@@ -29,6 +30,9 @@ int main(int argc, char *argv[])
 {
        lsampl_t data;
        int ret;
+       comedi_range * range_info;
+       lsampl_t maxdata;
+       double physical_value;
 
        parse_options(argc,argv);
 
@@ -49,7 +53,35 @@ int main(int argc, char *argv[])
                exit(0);
        }
 
-       printf("%d\n",data);
+       if(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) {
+                       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);
+                       }
+                       if(verbose) {
+                               printf(" (%d raw units)", data);
+                       }
+               }
+       } else {
+               printf("%d",data);
+       }
+       printf("\n");
 
        return 0;
 }