2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
36 #include "ao-verbose.h"
38 static const struct option options[] = {
39 { .name = "tty", .has_arg = 1, .val = 'T' },
40 { .name = "device", .has_arg = 1, .val = 'D' },
41 { .name = "verbose", .has_arg = 0, .val = 'v' },
42 { .name = "output", .has_arg = 1, .val = 'o' },
43 { .name = "nosave", .has_arg = 0, .val = 'n' },
47 static void usage(char *program)
49 fprintf(stderr, "usage: %s [--verbose] [--nosave] [--device=<device>] [-tty=<tty>] [--output=<cal-value-file>]\n", program);
55 char **strs = malloc (sizeof (char *)), *str;
58 while ((str = strtok(line, " \t"))) {
60 strs = realloc(strs, (n + 2) * sizeof (char *));
61 strs[n] = strdup(str);
75 flash(struct cc_usb *usb)
77 struct flash *head = NULL, **tail = &head;
78 cc_usb_printf(usb, "c s\nv\n");
83 cc_usb_getline(usb, line, sizeof (line));
84 b = malloc (sizeof (struct flash));
85 strcpy(b->line, line);
90 if (strstr(line, "software-version"))
97 find_flash(struct flash *b, char *word0) {
98 for (;b; b = b->next) {
99 if (strstr(b->line, word0))
106 do_save(struct cc_usb *usb)
110 printf("Saving calibration to device\n");
111 cc_usb_printf(usb, "c w\nv\n");
115 cc_usb_getline(usb, line, sizeof (line));
116 if (strstr(line, "Nothing to save"))
118 if (strstr(line, "Saved"))
120 if (strstr(line, "software-version"))
124 printf("Calibration save failed\n");
130 do_output(char *output, int cur_cal)
132 printf ("Saving calibration value to file \"%s\"\n", output);
134 FILE *out = fopen(output, "w");
142 if (fprintf(out, "%d\n", cur_cal) < 0) {
146 if (fflush(out) != 0) {
150 if (fclose(out) != 0) {
158 do_cal(char *tty, int save, char *output)
160 struct cc_usb *usb = NULL;
163 double measured_freq;
164 char **cur_freq_words;
165 char **cur_cal_words;
174 usb = cc_usb_open(tty);
177 fprintf(stderr, "failed to open device\n");
182 cc_usb_printf(usb, "E 0\n");
186 cur_cal_words = find_flash(b, "Radio cal:");
187 cur_freq_words = find_flash(b, "Frequency:");
189 if (!cur_cal_words || !cur_freq_words) {
190 fprintf(stderr, "no response\n");
195 cur_cal = atoi(cur_cal_words[2]);
196 cur_freq = atoi(cur_freq_words[1]);
198 printf ("Current radio calibration %d\n", cur_cal);
199 printf ("Current radio frequency: %7.3f\n", cur_freq / 1000.0);
202 cc_usb_printf(usb, "C 1\n");
205 printf("Generating RF carrier. Please enter measured frequency [enter for done]: ");
207 fgets(line, sizeof (line) - 1, stdin);
208 cc_usb_printf(usb, "C 0\n");
211 measured_freq = strtod(line, &line_end);
212 if (line_end == line)
215 new_cal = floor ((((double) cur_freq / 1000.0) / measured_freq) * cur_cal + 0.5);
217 if (new_cal == cur_cal) {
218 printf("Calibration value %d unchanged\n", cur_cal);
220 printf ("Setting cal value %d\n", new_cal);
222 cc_usb_printf (usb, "c f %d\n", new_cal);
234 printf("Calibration unchanged, not saving\n");
238 if (!do_output(output, cur_cal))
247 main (int argc, char **argv)
257 while ((c = getopt_long(argc, argv, "vnT:D:o:", options, NULL)) != -1) {
280 ao_verbose = verbose;
282 ccdbg_add_debug(CC_DEBUG_BITBANG);
285 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
287 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
289 tty = getenv("ALTOS_TTY");
293 if (!do_cal(tty, save, output))