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.
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) {
178 cc_usb_printf(usb, "E 1\nc a 0\n");
181 int c = cc_usb_getchar_timeout(usb, 20*1000);
185 else if (l < sizeof (line) - 1)
188 putchar(c); fflush(stdout);
189 if (strstr(line, "press a key...")) {
191 cc_usb_printf(usb, " ");
195 else if (strstr(line, "Invalid"))
197 if (running && strstr(line, ">")) {
202 cc_usb_printf(usb, "E 0\n");
205 printf("Calibration failed\n");
211 accel = find_flash(b, "Accel cal");
213 printf("no response\n");
217 printf ("Accel cal +1g: %s -1g: %s\n",
220 printf ("Saving..."); fflush(stdout);
221 cc_usb_printf (usb, "c w\n");
230 main (int argc, char **argv)
239 struct cc_usb *cc = NULL;
246 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
263 ao_verbose = verbose;
266 ccdbg_add_debug(CC_DEBUG_BITBANG);
269 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
271 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
273 tty = getenv("ALTOS_TTY");
277 cc = cc_usb_open(tty);