ao-tools/ao-mega: Clean up formatting of pyro status messages
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define _GNU_SOURCE
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include "cc.h"
25
26 static const struct option options[] = {
27         { 0, 0, 0, 0},
28 };
29
30 static void usage(char *program)
31 {
32         fprintf(stderr, "usage: %s\n"
33                 "\t{flight.mega} ...\n", program);
34         exit(1);
35 }
36
37 #define bool(b) ((b) ? "true" : "false")
38
39 static const char *state_names[] = {
40         "startup",
41         "idle",
42         "pad",
43         "boost",
44         "fast",
45         "coast",
46         "drogue",
47         "main",
48         "landed",
49         "invalid"
50 };
51
52
53 #define NUM_STATE       (sizeof state_names/sizeof state_names[0])
54
55 int
56 main (int argc, char **argv)
57 {
58         char    line[256];
59         int c, i, ret, j;
60         char *s;
61         FILE *file;
62         int serial;
63         const char *state;
64         while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) {
65                 switch (c) {
66                 default:
67                         usage(argv[0]);
68                         break;
69                 }
70         }
71         for (i = optind; i < argc; i++) {
72                 file = fopen(argv[i], "r");
73                 if (!file) {
74                         perror(argv[i]);
75                         ret++;
76                         continue;
77                 }
78                 s = strstr(argv[i], "-serial-");
79                 if (s)
80                         serial = atoi(s + 8);
81                 else
82                         serial = 0;
83                 while (fgets(line, sizeof (line), file)) {
84                         struct ao_log_mega      log;
85
86                         if (cc_mega_parse(line, &log)) {
87                                 if (log.is_config) {
88                                         printf ("config %2d %s", log.u.config_int.kind, line);
89                                 } else {
90                                         printf ("tick %5d ", log.tick);
91                                         switch (log.type) {
92                                         case AO_LOG_FLIGHT:
93                                                 printf ("flight %5u ground_accel %d ground_pres %u\n",
94                                                         log.u.flight.flight,
95                                                         log.u.flight.ground_accel,
96                                                         log.u.flight.ground_pres);
97                                                 break;
98                                         case AO_LOG_STATE:
99                                                 if (log.u.state.state < NUM_STATE)
100                                                         state = state_names[log.u.state.state];
101                                                 else
102                                                         state = "invalid";
103                                                 printf ("state %d (%s)\n", log.u.state.state, state);
104                                                 break;
105                                         case AO_LOG_SENSOR:
106                                                 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",
107                                                         log.u.sensor.pres,
108                                                         log.u.sensor.temp,
109                                                         log.u.sensor.accel_x,
110                                                         log.u.sensor.accel_y,
111                                                         log.u.sensor.accel_z,
112                                                         log.u.sensor.gyro_x,
113                                                         log.u.sensor.gyro_y,
114                                                         log.u.sensor.gyro_z,
115                                                         log.u.sensor.mag_x,
116                                                         log.u.sensor.mag_y,
117                                                         log.u.sensor.mag_z,
118                                                         log.u.sensor.accel);
119                                                 break;
120                                         case AO_LOG_TEMP_VOLT:
121                                                 printf ("batt %6d pbatt %6d n_sense %d",
122                                                         log.u.volt.v_batt,
123                                                         log.u.volt.v_pbatt,
124                                                         log.u.volt.n_sense);
125                                                 for (j = 0; j < log.u.volt.n_sense; j++) {
126                                                         printf (" s%d %6d",
127                                                                 j, log.u.volt.sense[j]);
128                                                 }
129                                                 printf (" pyro %04x\n", log.u.volt.pyro);
130                                                 break;
131                                         default:
132                                                 printf ("type %c\n", log.type, log.tick);
133                                                 break;
134                                         }
135                                 }
136                         }
137                 }
138                 fclose (file);
139
140         }
141         return ret;
142 }