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)
60 char **strs = malloc (sizeof (char *)), *str;
63 while ((str = strtok(line, " \t"))) {
65 strs = realloc(strs, (n + 2) * sizeof (char *));
66 strs[n] = strdup(str);
80 flash(struct cc_usb *usb)
82 struct flash *head = NULL, **tail = &head;
83 cc_usb_printf(usb, "c s\nv\n");
88 cc_usb_getline(usb, line, sizeof (line));
89 b = malloc (sizeof (struct flash));
90 strcpy(b->line, line);
95 if (strstr(line, "software-version"))
102 find_flash(struct flash *b, char *word0) {
103 for (;b; b = b->next) {
104 if (strstr(b->line, word0))
113 struct termios termios, termios_save;
116 tcgetattr(0, &termios);
117 termios_save = termios;
119 tcsetattr(0, TCSAFLUSH, &termios);
120 read(0, buf, sizeof (buf));
121 tcsetattr(0, TCSAFLUSH, &termios_save);
125 do_cal(struct cc_usb *usb) {
133 cc_usb_printf(usb, "E 1\nc a 0\n");
136 int c = cc_usb_getchar_timeout(usb, 20*1000);
140 else if (l < sizeof (line) - 1)
143 putchar(c); fflush(stdout);
144 if (strstr(line, "press a key...")) {
146 cc_usb_printf(usb, " ");
150 else if (strstr(line, "Invalid"))
152 if (running && strstr(line, ">")) {
157 cc_usb_printf(usb, "E 0\n");
160 printf("Calibration failed\n");
166 accel = find_flash(b, "Accel cal");
168 printf("no response\n");
172 printf ("Accel cal +1g: %s -1g: %s\n",
175 printf ("Saving..."); fflush(stdout);
176 cc_usb_printf (usb, "c w\n");
185 main (int argc, char **argv)
189 struct cc_usb *cc = NULL;
194 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
211 ao_verbose = verbose;
214 ccdbg_add_debug(CC_DEBUG_BITBANG);
217 tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
219 tty = cc_usbdevs_find_by_arg(device, "TeleMega");
221 tty = getenv("ALTOS_TTY");
225 cc = cc_usb_open(tty);