ao-test-baro: Be more lax about altitude checks
[fw/altos] / ao-tools / ao-cal-freq / ao-cal-freq.c
index 464faf0f8320a63587cdf79b1e4a5d441e2ad24d..12c2a3ae9f6c845b809025a37068844d7d1c7e8b 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -38,13 +39,13 @@ static const struct option options[] = {
        { .name = "tty", .has_arg = 1, .val = 'T' },
        { .name = "device", .has_arg = 1, .val = 'D' },
        { .name = "raw", .has_arg = 0, .val = 'r' },
-       { .name = "verbose", .has_arg = 1, .val = 'v' },
+       { .name = "verbose", .has_arg = 0, .val = 'v' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>]\n", program);
+       fprintf(stderr, "usage: %s [--verbose] [--device=<device>] [-tty=<tty>]\n", program);
        exit(1);
 }
 
@@ -167,7 +168,8 @@ await_key(void)
 }
 
 int
-do_cal(struct cc_usb *usb) {
+do_cal(char *tty) {
+       struct cc_usb *usb = NULL;
        struct flash    *b;
        char    line[1024];
        double  measured_freq;
@@ -177,10 +179,17 @@ do_cal(struct cc_usb *usb) {
        int     cur_freq;
        int     cur_cal;
        int     new_cal;
-
-       cc_usb_printf(usb, "E 0\n");
+       int     ret = 1;
 
        for(;;) {
+               usb = cc_usb_open(tty);
+
+               if (!usb)
+                       exit(1);
+
+               cc_usb_printf(usb, "E 0\n");
+
+               cc_usb_sync(usb);
                cc_usb_printf(usb, "C 1\n");
                cc_usb_sync(usb);
 
@@ -201,7 +210,8 @@ do_cal(struct cc_usb *usb) {
 
                if (!cur_cal_words || !cur_freq_words) {
                        printf("no response\n");
-                       return 0;
+                       ret = 0;
+                       break;
                }
 
                cur_cal = atoi(cur_cal_words[2]);
@@ -217,8 +227,11 @@ do_cal(struct cc_usb *usb) {
 
                cc_usb_printf (usb, "c f %d\nc w\n", new_cal);
                cc_usb_sync(usb);
+               cc_usb_close(usb);
        }
-       return 1;
+       if (usb)
+               cc_usb_close(usb);
+       return ret;
 }
 
 int
@@ -231,14 +244,13 @@ main (int argc, char **argv)
        int                     i;
        int                     c;
        int                     tries;
-       struct cc_usb           *cc = NULL;
        char                    *tty = NULL;
        int                     success;
        int                     verbose = 0;
        int                     ret = 0;
        int                     expected_size;
 
-       while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "vrT:D:c:s:", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -269,12 +281,6 @@ main (int argc, char **argv)
        if (!tty)
                tty="/dev/ttyACM0";
 
-       cc = cc_usb_open(tty);
-
-       if (!cc)
-               exit(1);
-
-       if (!do_cal(cc))
+       if (!do_cal(tty))
                ret = 1;
-       done(cc, ret);
 }