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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
35 #include "ao-verbose.h"
37 static const struct option options[] = {
38 { .name = "tty", .has_arg = 1, .val = 'T' },
39 { .name = "device", .has_arg = 1, .val = 'D' },
40 { .name = "raw", .has_arg = 0, .val = 'r' },
41 { .name = "verbose", .has_arg = 1, .val = 'v' },
45 static void usage(char *program)
47 fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>]\n", program);
52 done(struct cc_usb *cc, int code)
59 ends_with(char *whole, char *suffix)
61 int whole_len = strlen(whole);
62 int suffix_len = strlen(suffix);
64 if (suffix_len > whole_len)
66 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
70 starts_with(char *whole, char *prefix)
72 int whole_len = strlen(whole);
73 int prefix_len = strlen(prefix);
75 if (prefix_len > whole_len)
77 return strncmp(whole, prefix, prefix_len) == 0;
82 char **strs = malloc (sizeof (char *)), *str;
85 while ((str = strtok(line, " \t"))) {
87 strs = realloc(strs, (n + 2) * sizeof (char *));
88 strs[n] = strdup(str);
96 free_strs(char **strs) {
100 for (i = 0; (str = strs[i]) != NULL; i++)
111 static struct flash *
112 flash(struct cc_usb *usb)
114 struct flash *head = NULL, **tail = &head;
115 cc_usb_printf(usb, "c s\nv\n");
120 cc_usb_getline(usb, line, sizeof (line));
121 b = malloc (sizeof (struct flash));
122 strcpy(b->line, line);
127 if (strstr(line, "software-version"))
134 free_flash(struct flash *b) {
146 find_flash(struct flash *b, char *word0) {
148 for (;b; b = b->next) {
149 if (strstr(b->line, word0))
158 struct termios termios, termios_save;
161 tcgetattr(0, &termios);
162 termios_save = termios;
164 tcsetattr(0, TCSAFLUSH, &termios);
165 read(0, buf, sizeof (buf));
166 tcsetattr(0, TCSAFLUSH, &termios_save);
170 do_cal(struct cc_usb *usb) {
173 double measured_freq;
174 char **cur_freq_words;
175 char **cur_cal_words;
181 cc_usb_printf(usb, "E 0\n");
184 cc_usb_printf(usb, "C 1\n");
187 printf("Generating RF carrier. Please enter measured frequency [enter for done]: ");
189 fgets(line, sizeof (line) - 1, stdin);
190 cc_usb_printf(usb, "C 0\n");
193 measured_freq = strtod(line, &line_end);
194 if (line_end == line)
199 cur_cal_words = find_flash(b, "Radio cal:");
200 cur_freq_words = find_flash(b, "Frequency:");
202 if (!cur_cal_words || !cur_freq_words) {
203 printf("no response\n");
207 cur_cal = atoi(cur_cal_words[2]);
208 cur_freq = atoi(cur_freq_words[1]);
210 printf ("Current radio calibration %d\n", cur_cal);
211 printf ("Current radio frequency: %d\n", cur_freq);
214 new_cal = floor ((((double) cur_freq / 1000.0) / measured_freq) * cur_cal + 0.5);
216 printf ("Programming flash with cal value %d\n", new_cal);
218 cc_usb_printf (usb, "c f %d\nc w\n", new_cal);
225 main (int argc, char **argv)
234 struct cc_usb *cc = NULL;
241 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
258 ao_verbose = verbose;
261 ccdbg_add_debug(CC_DEBUG_BITBANG);
264 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
266 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
268 tty = getenv("ALTOS_TTY");
272 cc = cc_usb_open(tty);