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);
54 done(struct cc_usb *cc, int code)
61 ends_with(char *whole, char *suffix)
63 int whole_len = strlen(whole);
64 int suffix_len = strlen(suffix);
66 if (suffix_len > whole_len)
68 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
72 starts_with(char *whole, char *prefix)
74 int whole_len = strlen(whole);
75 int prefix_len = strlen(prefix);
77 if (prefix_len > whole_len)
79 return strncmp(whole, prefix, prefix_len) == 0;
84 char **strs = malloc (sizeof (char *)), *str;
87 while ((str = strtok(line, " \t"))) {
89 strs = realloc(strs, (n + 2) * sizeof (char *));
90 strs[n] = strdup(str);
98 free_strs(char **strs) {
102 for (i = 0; (str = strs[i]) != NULL; i++)
113 static struct flash *
114 flash(struct cc_usb *usb)
116 struct flash *head = NULL, **tail = &head;
117 cc_usb_printf(usb, "c s\nv\n");
122 cc_usb_getline(usb, line, sizeof (line));
123 b = malloc (sizeof (struct flash));
124 strcpy(b->line, line);
129 if (strstr(line, "software-version"))
136 free_flash(struct flash *b) {
148 find_flash(struct flash *b, char *word0) {
150 for (;b; b = b->next) {
151 if (strstr(b->line, word0))
160 struct termios termios, termios_save;
163 tcgetattr(0, &termios);
164 termios_save = termios;
166 tcsetattr(0, TCSAFLUSH, &termios);
167 read(0, buf, sizeof (buf));
168 tcsetattr(0, TCSAFLUSH, &termios_save);
172 do_save(struct cc_usb *usb)
176 printf("Saving calibration to device\n");
177 cc_usb_printf(usb, "c w\nv\n");
181 cc_usb_getline(usb, line, sizeof (line));
182 if (strstr(line, "Nothing to save"))
184 if (strstr(line, "Saved"))
186 if (strstr(line, "software-version"))
190 printf("Calibration save failed\n");
196 do_output(char *output, int cur_cal)
198 printf ("Saving calibration value to file \"%s\"\n", output);
200 FILE *out = fopen(output, "w");
208 if (fprintf(out, "%d\n", cur_cal) < 0) {
212 if (fflush(out) != 0) {
216 if (fclose(out) != 0) {
224 do_cal(char *tty, int save, char *output)
226 struct cc_usb *usb = NULL;
229 double measured_freq;
230 char **cur_freq_words;
231 char **cur_cal_words;
240 usb = cc_usb_open(tty);
243 fprintf(stderr, "failed to open device\n");
248 cc_usb_printf(usb, "E 0\n");
252 cur_cal_words = find_flash(b, "Radio cal:");
253 cur_freq_words = find_flash(b, "Frequency:");
255 if (!cur_cal_words || !cur_freq_words) {
256 fprintf(stderr, "no response\n");
261 cur_cal = atoi(cur_cal_words[2]);
262 cur_freq = atoi(cur_freq_words[1]);
264 printf ("Current radio calibration %d\n", cur_cal);
265 printf ("Current radio frequency: %7.3f\n", cur_freq / 1000.0);
268 cc_usb_printf(usb, "C 1\n");
271 printf("Generating RF carrier. Please enter measured frequency [enter for done]: ");
273 fgets(line, sizeof (line) - 1, stdin);
274 cc_usb_printf(usb, "C 0\n");
277 measured_freq = strtod(line, &line_end);
278 if (line_end == line)
281 new_cal = floor ((((double) cur_freq / 1000.0) / measured_freq) * cur_cal + 0.5);
283 if (new_cal == cur_cal) {
284 printf("Calibration value %d unchanged\n", cur_cal);
286 printf ("Setting cal value %d\n", new_cal);
288 cc_usb_printf (usb, "c f %d\n", new_cal);
300 printf("Calibration unchanged, not saving\n");
304 if (!do_output(output, cur_cal))
313 main (int argc, char **argv)
330 while ((c = getopt_long(argc, argv, "vnT:D:o:", options, NULL)) != -1) {
353 ao_verbose = verbose;
355 ccdbg_add_debug(CC_DEBUG_BITBANG);
358 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
360 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
362 tty = getenv("ALTOS_TTY");
366 if (!do_cal(tty, save, output))