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' },
42 { .name = "rplus", .has_arg = 1, .val = 'a' },
43 { .name = "rminus", .has_arg = 1, .val = 'b' },
44 { .name = "adcmax", .has_arg = 1, .val = 'm' },
48 static void usage(char *program)
50 fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [--tty=<tty>] [--rplus=val] [--rminus=val] [--adcmax=val] main|drogue\n", program);
55 done(struct cc_usb *cc, int code)
57 /* cc_usb_printf(cc, "a\n"); */
70 map_igniter_name(char *adc_name, char *igniter_name)
72 char *colon = strchr(adc_name, ':');
76 if (strlen(adc_name) == 1 && isupper(adc_name[0])) {
77 igniter_name[0] = '0' + adc_name[0] - 'A';
78 igniter_name[1] = '\0';
80 strcpy(igniter_name, adc_name);
86 other_igniter_name(const char *name)
88 if (!strcmp(name, "drogue"))
90 if (!strcmp(name, "apogee"))
95 static struct igniter *
96 igniters(struct cc_usb *usb)
98 struct igniter *head = NULL, **tail = &head;
99 cc_usb_printf(usb, "t\na\nv\n");
105 char igniter_name[512];
107 cc_usb_getline(usb, line, sizeof (line));
108 if (strstr(line, "software-version"))
110 if (sscanf(line, "Igniter: %s Status: %s", name, status) == 2) {
111 struct igniter *i = calloc (1, sizeof (struct igniter));
112 strcpy(i->name, name);
113 strcpy(i->status, status);
118 if (strncmp(line, "tick:", 5) == 0) {
121 bool found_igniter = false;
123 while ((tok = strtok(l, " ")) != NULL) {
127 for (i = head; i; i = i->next)
128 if (!strcmp(i->name, igniter_name) ||
129 !strcmp(i->name, other_igniter_name(igniter_name)))
134 found_igniter = false;
136 strcpy(adc_name, tok);
137 found_igniter = map_igniter_name(adc_name, igniter_name);
146 free_igniters(struct igniter *i) {
156 static struct igniter *
157 find_igniter(struct igniter *i, char *name)
159 for (; i; i = i->next)
160 if (strcmp(i->name, name) == 0) {
161 printf("igniter %s adc %d\n", i->name, i->adc);
167 static const double ref_volts = 3.3;
170 compute_voltage(int adc, double rplus, double rminus, int adc_max)
172 return (double) adc / (double) adc_max * ref_volts * (rplus + rminus) / rminus;
176 do_igniter(struct cc_usb *usb, char *name, double rplus, double rminus, int adc_max)
178 struct igniter *all = igniters(usb);
179 struct igniter *this = find_igniter(all, name);
183 printf("no igniter %s found in", name);
184 for (i = all; i; i = i->next)
185 printf(" %s", i->name);
190 if (rplus && rminus && adc_max) {
191 volts = compute_voltage(this->adc, rplus, rminus, adc_max);
192 if (volts < 1 || volts > 4) {
193 printf("igniter %s voltage is %f, not in range of 1-4 volts\n", this->name, volts);
198 if (strcmp(this->status, "ready") != 0) {
199 printf("igniter %s status is %s\n", this->name, this->status);
203 cc_usb_printf(usb, "i DoIt %s\n", this->name);
211 main (int argc, char **argv)
216 struct cc_usb *cc = NULL;
224 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:a:b:m:", options, NULL)) != -1) {
236 rplus = strtod(optarg, NULL);
239 rminus = strtod(optarg, NULL);
242 adcmax = strtol(optarg, NULL, 0);
250 ao_verbose = verbose;
253 ccdbg_add_debug(CC_DEBUG_BITBANG);
256 tty = cc_usbdevs_find_by_arg(device, "TeleMega-v1.0");
258 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum-v2.0");
260 tty = cc_usbdevs_find_by_arg(device, "TeleMini-v2.0");
262 tty = cc_usbdevs_find_by_arg(device, "EasyMega-v1.0");
264 tty = cc_usbdevs_find_by_arg(device, "EasyMetrum-v1.0");
266 tty = cc_usbdevs_find_by_arg(device, "EasyMini-v1.0");
268 tty = getenv("ALTOS_TTY");
272 cc = cc_usb_open(tty);
277 for (i = optind; i < argc; i++) {
278 char *name = argv[i];
280 if (!do_igniter(cc, name, rplus, rminus, adcmax))