ao-test-igniter: Also test ADC voltage for in-range of 1-4 volts
[fw/altos] / ao-tools / ao-test-igniter / ao-test-igniter.c
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <err.h>
20 #include <fcntl.h>
21 #include <gelf.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <sysexits.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <ctype.h>
31 #include "ao-elf.h"
32 #include "ccdbg.h"
33 #include "cc-usb.h"
34 #include "cc.h"
35 #include "ao-verbose.h"
36
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' },
45         { 0, 0, 0, 0},
46 };
47
48 static void usage(char *program)
49 {
50         fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [--tty=<tty>] [--rplus=val] [--rminus=val] [--adcmax=val] main|drogue\n", program);
51         exit(1);
52 }
53
54 static void
55 done(struct cc_usb *cc, int code)
56 {
57 /*      cc_usb_printf(cc, "a\n"); */
58         cc_usb_close(cc);
59         exit (code);
60 }
61
62 struct igniter {
63         struct igniter  *next;
64         char            name[512];
65         char            status[512];
66         int             adc;
67 };
68
69 static bool
70 map_igniter_name(char *adc_name, char *igniter_name)
71 {
72         char    *colon = strchr(adc_name, ':');
73         if (!colon)
74                 return false;
75         *colon = '\0';
76         if (strlen(adc_name) == 1 && isupper(adc_name[0])) {
77                 igniter_name[0] = '0' + adc_name[0] - 'A';
78                 igniter_name[1] = '\0';
79         } else {
80                 strcpy(igniter_name, adc_name);
81         }
82         return true;
83 }
84
85 static struct igniter *
86 igniters(struct cc_usb *usb)
87 {
88         struct igniter  *head = NULL, **tail = &head;
89         cc_usb_printf(usb, "t\na\nv\n");
90         for (;;) {
91                 char    line[512];
92                 char    name[512];
93                 char    status[512];
94                 char    adc_name[512];
95                 char    igniter_name[512];
96
97                 cc_usb_getline(usb, line, sizeof (line));
98                 if (strstr(line, "software-version"))
99                         break;
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);
104                         i->next = NULL;
105                         *tail = i;
106                         tail = &i->next;
107                 }
108                 if (strncmp(line, "tick:", 5) == 0) {
109                         char    *tok;
110                         char    *l = line;
111                         bool    found_igniter = false;
112
113                         while ((tok = strtok(l, " ")) != NULL) {
114                                 l = NULL;
115                                 if (found_igniter) {
116                                         struct igniter *i;
117                                         for (i = head; i; i = i->next)
118                                                 if (!strcmp(i->name, igniter_name)) {
119                                                         i->adc = atoi(tok);
120                                                         break;
121                                                 }
122                                         found_igniter = false;
123                                 } else {
124                                         strcpy(adc_name, tok);
125                                         found_igniter = map_igniter_name(adc_name, igniter_name);
126                                 }
127                         }
128                 }
129         }
130         return head;
131 }
132
133 static void
134 free_igniters(struct igniter *i) {
135         struct igniter *n;
136
137         while (i) {
138                 n = i->next;
139                 free(i);
140                 i = n;
141         }
142 }
143
144 static struct igniter *
145 find_igniter(struct igniter *i, char *name)
146 {
147         for (; i; i = i->next)
148                 if (strcmp(i->name, name) == 0) {
149                         printf("igniter %s adc %d\n", i->name, i->adc);
150                         return i;
151                 }
152         return NULL;
153 }
154
155 static const double ref_volts = 3.3;
156
157 static double
158 compute_voltage(int adc, double rplus, double rminus, int adc_max)
159 {
160         return (double) adc / (double) adc_max * ref_volts * (rplus + rminus) / rminus;
161 }
162
163 static int
164 do_igniter(struct cc_usb *usb, char *name, double rplus, double rminus, int adc_max)
165 {
166         struct igniter  *all = igniters(usb);
167         struct igniter  *this = find_igniter(all, name);
168         double volts = -1;
169         if (!this) {
170                 struct igniter  *i;
171                 printf("no igniter %s found in", name);
172                 for (i = all; i; i = i->next)
173                         printf(" %s", i->name);
174                 printf("\n");
175                 free_igniters(all);
176                 return 0;
177         }
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);
182                         free_igniters(all);
183                         return 0;
184                 }
185         }
186         if (strcmp(this->status, "ready") != 0) {
187                 printf("igniter %s status is %s\n", this->name, this->status);
188                 free_igniters(all);
189                 return 0;
190         }
191         cc_usb_printf(usb, "i DoIt %s\n", this->name);
192         cc_usb_sync(usb);
193         free_igniters(all);
194         usleep(200000);
195         return 1;
196 }
197
198 int
199 main (int argc, char **argv)
200 {
201         char                    *device = NULL;
202         int                     i;
203         int                     c;
204         struct cc_usb           *cc = NULL;
205         char                    *tty = NULL;
206         int                     verbose = 0;
207         int                     ret = 0;
208         double                  rplus = 0.0;
209         double                  rminus = 0.0;
210         int                     adcmax = 0;
211
212         while ((c = getopt_long(argc, argv, "rT:D:c:s:v:a:b:m:", options, NULL)) != -1) {
213                 switch (c) {
214                 case 'T':
215                         tty = optarg;
216                         break;
217                 case 'D':
218                         device = optarg;
219                         break;
220                 case 'v':
221                         verbose++;
222                         break;
223                 case 'a':
224                         rplus = strtod(optarg, NULL);
225                         break;
226                 case 'b':
227                         rminus = strtod(optarg, NULL);
228                         break;
229                 case 'm':
230                         adcmax = strtol(optarg, NULL, 0);
231                         break;
232                 default:
233                         usage(argv[0]);
234                         break;
235                 }
236         }
237
238         ao_verbose = verbose;
239
240         if (verbose > 1)
241                 ccdbg_add_debug(CC_DEBUG_BITBANG);
242
243         if (!tty)
244                 tty = cc_usbdevs_find_by_arg(device, "TeleMega-v1.0");
245         if (!tty)
246                 tty = cc_usbdevs_find_by_arg(device, "TeleMetrum-v2.0");
247         if (!tty)
248                 tty = cc_usbdevs_find_by_arg(device, "TeleMini-v2.0");
249         if (!tty)
250                 tty = cc_usbdevs_find_by_arg(device, "EasyMega-v1.0");
251         if (!tty)
252                 tty = cc_usbdevs_find_by_arg(device, "EasyMetrum-v1.0");
253         if (!tty)
254                 tty = cc_usbdevs_find_by_arg(device, "EasyMini-v1.0");
255         if (!tty)
256                 tty = getenv("ALTOS_TTY");
257         if (!tty)
258                 tty="/dev/ttyACM0";
259
260         cc = cc_usb_open(tty);
261
262         if (!cc)
263                 exit(1);
264
265         for (i = optind; i < argc; i++) {
266                 char    *name = argv[i];
267
268                 if (!do_igniter(cc, name, rplus, rminus, adcmax))
269                         ret++;
270         }
271         done(cc, ret);
272 }