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 = "raw", .has_arg = 0, .val = 'r' },
42 { .name = "verbose", .has_arg = 0, .val = 'v' },
46 static void usage(char *program)
48 fprintf(stderr, "usage: %s [--verbose] [--device=<device>] [-tty=<tty>]\n", program);
53 done(struct cc_usb *cc, int code)
60 ends_with(char *whole, char *suffix)
62 int whole_len = strlen(whole);
63 int suffix_len = strlen(suffix);
65 if (suffix_len > whole_len)
67 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
71 starts_with(char *whole, char *prefix)
73 int whole_len = strlen(whole);
74 int prefix_len = strlen(prefix);
76 if (prefix_len > whole_len)
78 return strncmp(whole, prefix, prefix_len) == 0;
83 char **strs = malloc (sizeof (char *)), *str;
86 while ((str = strtok(line, " \t"))) {
88 strs = realloc(strs, (n + 2) * sizeof (char *));
89 strs[n] = strdup(str);
97 free_strs(char **strs) {
101 for (i = 0; (str = strs[i]) != NULL; i++)
112 static struct flash *
113 flash(struct cc_usb *usb)
115 struct flash *head = NULL, **tail = &head;
116 cc_usb_printf(usb, "c s\nv\n");
121 cc_usb_getline(usb, line, sizeof (line));
122 b = malloc (sizeof (struct flash));
123 strcpy(b->line, line);
128 if (strstr(line, "software-version"))
135 free_flash(struct flash *b) {
147 find_flash(struct flash *b, char *word0) {
149 for (;b; b = b->next) {
150 if (strstr(b->line, word0))
159 struct termios termios, termios_save;
162 tcgetattr(0, &termios);
163 termios_save = termios;
165 tcsetattr(0, TCSAFLUSH, &termios);
166 read(0, buf, sizeof (buf));
167 tcsetattr(0, TCSAFLUSH, &termios_save);
172 struct cc_usb *usb = NULL;
175 double measured_freq;
176 char **cur_freq_words;
177 char **cur_cal_words;
185 usb = cc_usb_open(tty);
190 cc_usb_printf(usb, "E 0\n");
193 cc_usb_printf(usb, "C 1\n");
196 printf("Generating RF carrier. Please enter measured frequency [enter for done]: ");
198 fgets(line, sizeof (line) - 1, stdin);
199 cc_usb_printf(usb, "C 0\n");
202 measured_freq = strtod(line, &line_end);
203 if (line_end == line)
208 cur_cal_words = find_flash(b, "Radio cal:");
209 cur_freq_words = find_flash(b, "Frequency:");
211 if (!cur_cal_words || !cur_freq_words) {
212 printf("no response\n");
217 cur_cal = atoi(cur_cal_words[2]);
218 cur_freq = atoi(cur_freq_words[1]);
220 printf ("Current radio calibration %d\n", cur_cal);
221 printf ("Current radio frequency: %d\n", cur_freq);
224 new_cal = floor ((((double) cur_freq / 1000.0) / measured_freq) * cur_cal + 0.5);
226 printf ("Programming flash with cal value %d\n", new_cal);
228 cc_usb_printf (usb, "c f %d\nc w\n", new_cal);
238 main (int argc, char **argv)
253 while ((c = getopt_long(argc, argv, "vrT:D:c:s:", options, NULL)) != -1) {
270 ao_verbose = verbose;
273 ccdbg_add_debug(CC_DEBUG_BITBANG);
276 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
278 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
280 tty = getenv("ALTOS_TTY");