Module: xenomai-3
Branch: master
Commit: 68033dcc5bf6c732eba6a43390aca34c9f490e98
URL:    
http://git.xenomai.org/?p=xenomai-3.git;a=commit;h=68033dcc5bf6c732eba6a43390aca34c9f490e98

Author: Jorge Ramirez-Ortiz <j...@xenomai.org>
Date:   Fri Sep 12 11:55:40 2014 -0400

utils/analogy: calibration - use iniparser

---

 utils/analogy/Makefile.am         |    4 ++-
 utils/analogy/analogy_calibrate.c |   57 ++++++++++++++++++++++++++++++++-----
 utils/analogy/analogy_calibrate.h |   11 ++++++-
 utils/analogy/calibration_ni_m.c  |   40 ++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 9 deletions(-)

diff --git a/utils/analogy/Makefile.am b/utils/analogy/Makefile.am
index fe2f317..40c09f3 100644
--- a/utils/analogy/Makefile.am
+++ b/utils/analogy/Makefile.am
@@ -12,7 +12,9 @@ bin_PROGRAMS = \
 CPPFLAGS =                                             \
        @XENO_USER_CFLAGS@                              \
        -ggdb                                           \
-       -I$(top_srcdir)/include
+       -I$(top_srcdir)/include                         \
+       -I$(top_srcdir)/lib/boilerplate 
+
 
 LDFLAGS =
 
diff --git a/utils/analogy/analogy_calibrate.c 
b/utils/analogy/analogy_calibrate.c
index 586a38a..b2ac286 100644
--- a/utils/analogy/analogy_calibrate.c
+++ b/utils/analogy/analogy_calibrate.c
@@ -31,7 +31,7 @@
 #include "analogy_calibrate.h"
 #include "calibration_ni_m.h"
 
-
+struct apply_calibration_params params = {.name = NULL,} ;
 struct timespec calibration_start_time;
 static const char *revision = "0.0.1";
 a4l_desc_t descriptor;
@@ -57,28 +57,65 @@ static const struct option options[] = {
                .flag = NULL,
        },
        {
+#define apply_opt      3
+               .name = "apply",
+               .has_arg = 1,
+               .flag = NULL,
+       },
+       {
                .name = NULL,
        }
 };
 
-static void print_usage(void)
+static void
+print_usage(void)
 {
        fprintf(stderr, "Usage: analogy_calibrate \n"
-              "  --help                        : this menu \n"
-              "  --device /dev/analogyX        : analogy device to calibrate 
\n"
-              "  --output filename             : calibration results \n"
+              "  --help                                        : this menu \n"
+              "  --device /dev/analogyX                        : analogy 
device to calibrate \n"
+              "  --output filename                             : calibration 
results \n"
+              "  --apply filename:subd,channel,range,aref      : apply the 
calibration file \n"
+              "          ex: /home/foo/calib.rc:0,1,255,255 - use 255 for dont 
care \n"
              );
 }
 
+static int
+apply_calibration_set_globals(char *info)
+{
+       char *p;
+
+       params.name = strtok(info, ":");
+       p = strtok(NULL, ",");
+       if (!p)
+               error(EXIT, 0, "missing --apply parameter \n");
+       params.subd = strtol(p, NULL, 0);
+
+       p = strtok(NULL, ",");
+       if (!p)
+               error(EXIT, 0, "missing --apply parameter \n");
+       params.channel = strtol(p, NULL, 0);
+
+       p = strtok(NULL, ",");
+       if (!p)
+               error(EXIT, 0, "missing --apply parameter \n");
+       params.range = strtol(p, NULL, 0);
+
+       p = strtok(NULL, "");
+       if (!p)
+               error(EXIT, 0, "missing --apply parameter \n");
+       params.aref = strtol(p, NULL, 0);
+
+       return 0;
+}
+
 static void __attribute__ ((constructor)) __analogy_calibrate_init(void)
 {
        clock_gettime(CLOCK_MONOTONIC, &calibration_start_time);
 }
-
 int main(int argc, char *argv[])
 {
        struct sched_param param = {.sched_priority = 99};
-       char *device = NULL, *file = NULL;
+       char *device = NULL, *file = NULL, *apply_info = NULL;
        int v, i, fd, err = 0;
        struct rlimit rl;
 
@@ -103,12 +140,18 @@ int main(int argc, char *argv[])
                                error(EXIT, errno, "calibration file");
                        __debug("calibration output: %s \n", file);
                        break;
+               case apply_opt:
+                       apply_info = optarg;
+                       break;
                default:
                        print_usage();
                        exit(EXIT_FAILURE);
                }
        }
 
+       if (apply_info)
+               apply_calibration_set_globals(apply_info);
+
        err = getrlimit(RLIMIT_STACK, &rl);
        if (!err) {
                if (rl.rlim_cur < rl.rlim_max) {
diff --git a/utils/analogy/analogy_calibrate.h 
b/utils/analogy/analogy_calibrate.h
index 1fb548e..c2b539a 100644
--- a/utils/analogy/analogy_calibrate.h
+++ b/utils/analogy/analogy_calibrate.h
@@ -38,8 +38,17 @@ extern struct timespec calibration_start_time;
 extern a4l_desc_t descriptor;
 extern FILE *cal;
 
-#define ARRAY_LEN(a)  (sizeof(a) / sizeof((a)[0]))
+struct apply_calibration_params {
+       int channel;
+       char *name;
+       int range;
+       int subd;
+       int aref;
+};
 
+extern struct apply_calibration_params params;
+
+#define ARRAY_LEN(a)  (sizeof(a) / sizeof((a)[0]))
 
 #define RETURN  1
 #define CONT    0
diff --git a/utils/analogy/calibration_ni_m.c b/utils/analogy/calibration_ni_m.c
index 05e5f24..fa5b4e6 100644
--- a/utils/analogy/calibration_ni_m.c
+++ b/utils/analogy/calibration_ni_m.c
@@ -26,9 +26,13 @@
 #include <gsl/gsl_matrix.h>
 #include <gsl/gsl_vector.h>
 #include <rtdm/analogy.h>
+#include <wordexp.h>
 #include <math.h>
 
 #include "calibration_ni_m.h"
+#include "iniparser/iniparser.h"
+
+extern char *apply_name;
 
 struct list ai_calibration_list;
 struct list ao_calibration_list;
@@ -42,6 +46,40 @@ static struct subdev_ops ops;
 static struct eeprom eeprom;
 static struct gnumath math;
 
+
+static int
+apply_calibration(void)
+{
+       const char *filename;
+       dictionary *d;
+       wordexp_t exp;
+       int ret = 0;
+
+       if (params.name == NULL)
+               return 0;
+
+       ret = wordexp(params.name, &exp, WRDE_NOCMD|WRDE_UNDEF);
+       if (ret) {
+               ret = ret == WRDE_NOSPACE ? -ENOMEM : -EINVAL;
+               error(EXIT, 0, "cant apply calibration (%d) \n", ret);
+       }
+
+       if (exp.we_wordc != 1)
+               error(EXIT, 0, "weird expansion of %s as rc file \n", 
params.name);
+
+       filename = exp.we_wordv[0];
+       if (access(filename, R_OK))
+               error(EXIT, 0, "cant access %s for reading \n", params.name);
+
+       d = iniparser_load(filename);
+       wordfree(&exp);
+
+       if (d == NULL)
+               error(EXIT, 0, "iniparser loading error for %s \n", 
params.name);
+
+       return ret;
+}
+
 /*
  * generate the calibration file
  */
@@ -1389,6 +1427,8 @@ int ni_m_software_calibrate(void)
 
        write_calibration(&ao_calibration_list, &ao_subd);
 
+       apply_calibration();
+
        return 0;
 }
 


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to