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);
85 static struct igniter *
86 igniters(struct cc_usb *usb)
88 struct igniter *head = NULL, **tail = &head;
89 cc_usb_printf(usb, "t\na\nv\n");
95 char igniter_name[512];
97 cc_usb_getline(usb, line, sizeof (line));
98 if (strstr(line, "software-version"))
100 if (sscanf(line, "Igniter: %s Status: %s", name, status) == 2) {
101 struct igniter *i = calloc (1, sizeof (struct igniter));
102 strcpy(i->name, name);
103 strcpy(i->status, status);
108 if (strncmp(line, "tick:", 5) == 0) {
111 bool found_igniter = false;
113 while ((tok = strtok(l, " ")) != NULL) {
117 for (i = head; i; i = i->next)
118 if (!strcmp(i->name, igniter_name)) {
122 found_igniter = false;
124 strcpy(adc_name, tok);
125 found_igniter = map_igniter_name(adc_name, igniter_name);
134 free_igniters(struct igniter *i) {
144 static struct igniter *
145 find_igniter(struct igniter *i, char *name)
147 for (; i; i = i->next)
148 if (strcmp(i->name, name) == 0) {
149 printf("igniter %s adc %d\n", i->name, i->adc);
155 static const double ref_volts = 3.3;
158 compute_voltage(int adc, double rplus, double rminus, int adc_max)
160 return (double) adc / (double) adc_max * ref_volts * (rplus + rminus) / rminus;
164 do_igniter(struct cc_usb *usb, char *name, double rplus, double rminus, int adc_max)
166 struct igniter *all = igniters(usb);
167 struct igniter *this = find_igniter(all, name);
171 printf("no igniter %s found in", name);
172 for (i = all; i; i = i->next)
173 printf(" %s", i->name);
178 if (rplus && rminus && adc_max) {
179 volts = compute_voltage(this->adc, rplus, rminus, adc_max);
180 if (volts < 1 || volts > 4) {
181 printf("igniter %s voltage is %f, not in range of 1-4 volts\n", this->name, volts);
186 if (strcmp(this->status, "ready") != 0) {
187 printf("igniter %s status is %s\n", this->name, this->status);
191 cc_usb_printf(usb, "i DoIt %s\n", this->name);
199 main (int argc, char **argv)
204 struct cc_usb *cc = NULL;
212 while ((c = getopt_long(argc, argv, "rT:D:c:s:v:a:b:m:", options, NULL)) != -1) {
224 rplus = strtod(optarg, NULL);
227 rminus = strtod(optarg, NULL);
230 adcmax = strtol(optarg, NULL, 0);
238 ao_verbose = verbose;
241 ccdbg_add_debug(CC_DEBUG_BITBANG);
244 tty = cc_usbdevs_find_by_arg(device, "TeleMega-v1.0");
246 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum-v2.0");
248 tty = cc_usbdevs_find_by_arg(device, "TeleMini-v2.0");
250 tty = cc_usbdevs_find_by_arg(device, "EasyMega-v1.0");
252 tty = cc_usbdevs_find_by_arg(device, "EasyMetrum-v1.0");
254 tty = cc_usbdevs_find_by_arg(device, "EasyMini-v1.0");
256 tty = getenv("ALTOS_TTY");
260 cc = cc_usb_open(tty);
265 for (i = optind; i < argc; i++) {
266 char *name = argv[i];
268 if (!do_igniter(cc, name, rplus, rminus, adcmax))