altos: Remove unused ao_adc_get from ao_adc_stm.c
[fw/altos] / ao-tools / ao-mega / ao-mega.c
1 /*
2  * Copyright © 2011 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 #define _GNU_SOURCE
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include "cc.h"
26
27 static const struct option options[] = {
28         { 0, 0, 0, 0},
29 };
30
31 static void usage(char *program)
32 {
33         fprintf(stderr, "usage: %s\n"
34                 "\t{flight.mega} ...\n", program);
35         exit(1);
36 }
37
38 #define bool(b) ((b) ? "true" : "false")
39
40 static const char *state_names[] = {
41         "startup",
42         "idle",
43         "pad",
44         "boost",
45         "fast",
46         "coast",
47         "drogue",
48         "main",
49         "landed",
50         "invalid"
51 };
52
53
54 #define NUM_STATE       (sizeof state_names/sizeof state_names[0])
55
56 int
57 main (int argc, char **argv)
58 {
59         char    line[256];
60         int c, i, ret, j;
61         char *s;
62         FILE *file;
63         int serial;
64         const char *state;
65         while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) {
66                 switch (c) {
67                 default:
68                         usage(argv[0]);
69                         break;
70                 }
71         }
72         for (i = optind; i < argc; i++) {
73                 file = fopen(argv[i], "r");
74                 if (!file) {
75                         perror(argv[i]);
76                         ret++;
77                         continue;
78                 }
79                 s = strstr(argv[i], "-serial-");
80                 if (s)
81                         serial = atoi(s + 8);
82                 else
83                         serial = 0;
84                 while (fgets(line, sizeof (line), file)) {
85                         struct ao_log_mega      log;
86
87                         if (cc_mega_parse(line, &log)) {
88                                 if (log.is_config) {
89                                         printf ("config %2d %s", log.u.config_int.kind, line);
90                                 } else {
91                                         printf ("tick %5d ", log.tick);
92                                         switch (log.type) {
93                                         case AO_LOG_FLIGHT:
94                                                 printf ("flight %5u ground_accel %d ground_pres %u\n",
95                                                         log.u.flight.flight,
96                                                         log.u.flight.ground_accel,
97                                                         log.u.flight.ground_pres);
98                                                 break;
99                                         case AO_LOG_STATE:
100                                                 if (log.u.state.state < NUM_STATE)
101                                                         state = state_names[log.u.state.state];
102                                                 else
103                                                         state = "invalid";
104                                                 printf ("state %d (%s)\n", log.u.state.state, state);
105                                                 break;
106                                         case AO_LOG_SENSOR:
107                                                 printf ("p %9u t %9u ax %6d ay %6d az %6d gx %6d gy %6d gz %6d mx %6d my %6d mz %6d a %6d\n",
108                                                         log.u.sensor.pres,
109                                                         log.u.sensor.temp,
110                                                         log.u.sensor.accel_x,
111                                                         log.u.sensor.accel_y,
112                                                         log.u.sensor.accel_z,
113                                                         log.u.sensor.gyro_x,
114                                                         log.u.sensor.gyro_y,
115                                                         log.u.sensor.gyro_z,
116                                                         log.u.sensor.mag_x,
117                                                         log.u.sensor.mag_y,
118                                                         log.u.sensor.mag_z,
119                                                         log.u.sensor.accel);
120                                                 break;
121                                         case AO_LOG_TEMP_VOLT:
122                                                 printf ("batt %6d pbatt %6d n_sense %d",
123                                                         log.u.volt.v_batt,
124                                                         log.u.volt.v_pbatt,
125                                                         log.u.volt.n_sense);
126                                                 for (j = 0; j < log.u.volt.n_sense; j++) {
127                                                         printf (" s%d %6d",
128                                                                 j, log.u.volt.sense[j]);
129                                                 }
130                                                 printf (" pyro %04x\n", log.u.volt.pyro);
131                                                 break;
132                                         default:
133                                                 printf ("type %c\n", log.type, log.tick);
134                                                 break;
135                                         }
136                                 }
137                         }
138                 }
139                 fclose (file);
140
141         }
142         return ret;
143 }