ao-tools: Clean up ao-sym structure an initializers
[fw/altos] / ao-tools / lib / cc-mega.c
1 /*
2  * Copyright © 2012 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 #include "cc.h"
19 #include <string.h>
20 #include <ctype.h>
21
22 static const char *
23 parse_hex(const char *data, int *result)
24 {
25         char    d[12];
26         int     x;
27         int     i;
28
29         while (isspace (*data))
30                 data++;
31         for (i = 0; i < sizeof (d) - 1 && isxdigit(*data); i++)
32                 d[i] = *data++;
33         d[i] = '\0';
34         if (sscanf(d, "%x", &x) != 1)
35                 return NULL;
36         *result = x;
37         return data;
38 }
39
40 static const char *
41 parse_uint16(const char *data, uint16_t *result)
42 {
43         int     x;
44         data = parse_hex(data, &x);
45         *result =x;
46         return data;
47 }
48
49 static const char *
50 parse_uint8(const char *data, uint8_t *result)
51 {
52         int     x;
53         data = parse_hex(data, &x);
54         *result =x;
55         return data;
56 }
57
58 static int
59 parse_eeprom(const char *input_line, struct ao_log_mega *l) {
60         const char      *line;
61         int     b;
62
63         if (input_line[1] != ' ')
64                 return 0;
65         if (!isupper(input_line[0]))
66                 return 0;
67
68         l->type = input_line[0];
69         l->is_config = 0;
70         line = input_line + 2;
71
72         line = parse_uint16(line, &l->tick);
73         for (b = 0; b < 28; b++) {
74                 if (!line)
75                         return 0;
76                 line = parse_uint8(line, &l->u.bytes[b]);
77         }
78         return 1;
79 }
80
81 #define YUP(t) do {                             \
82                 l->u.config_int.kind = (t);     \
83                 l->is_config = 1;               \
84                 return 1;                       \
85         } while (0);
86
87 static int
88 parse_config(const char *input_line, struct ao_log_mega *l) {
89         if (sscanf (input_line, "Config version: %d.%d",
90                     &l->u.config_int.data[0],
91                     &l->u.config_int.data[1]))
92                 YUP(AO_CONFIG_CONFIG);
93         if (sscanf (input_line, "Main deploy: %d",
94                     &l->u.config_int.data[0]))
95                 YUP(AO_CONFIG_MAIN);
96         if (sscanf (input_line, "Apogee delay: %d",
97                     &l->u.config_int.data[0]))
98                 YUP(AO_CONFIG_APOGEE);
99         if (sscanf (input_line, "Apogee lockout: %d",
100                     &l->u.config_int.data[0]))
101                 YUP(AO_CONFIG_LOCKOUT);
102         if (sscanf (input_line, "Frequency: %d",
103                     &l->u.config_int.data[0]))
104                 YUP(AO_CONFIG_FREQUENCY);
105         if (sscanf (input_line, "Radio enable:  %d",
106                     &l->u.config_int.data[0]))
107                 YUP(AO_CONFIG_RADIO_ENABLE);
108         if (sscanf (input_line, "Accel cal +1g: %d -1g: %d",
109                     &l->u.config_int.data[0],
110                     &l->u.config_int.data[1]))
111                 YUP(AO_CONFIG_ACCEL_CAL);
112         if (sscanf (input_line, "Radio cal: %d",
113                     &l->u.config_int.data[0]))
114                 YUP(AO_CONFIG_RADIO_CAL);
115         if (sscanf (input_line, "Max flight log: %d",
116                     &l->u.config_int.data[0]))
117                 YUP(AO_CONFIG_MAX_LOG);
118         if (sscanf (input_line, "Ignite mode: %d",
119                     &l->u.config_int.data[0]))
120                 YUP(AO_CONFIG_IGNITE_MODE);
121         if (sscanf (input_line, "Pad orientation: %d",
122                     &l->u.config_int.data[0]))
123                 YUP(AO_CONFIG_PAD_ORIENTATION);
124         if (sscanf (input_line, "serial-number %d",
125                     &l->u.config_int.data[0]))
126                 YUP(AO_CONFIG_SERIAL_NUMBER);
127         if (sscanf (input_line, "log-format %d",
128                     &l->u.config_int.data[0]))
129                 YUP(AO_CONFIG_LOG_FORMAT);
130         if (sscanf (input_line, "ms5607 reserved: %d",
131                     &l->u.config_int.data[0]))
132                 YUP(AO_CONFIG_MS5607_RESERVED);
133         if (sscanf (input_line, "ms5607 sens: %d",
134                     &l->u.config_int.data[0]))
135                 YUP(AO_CONFIG_MS5607_SENS);
136         if (sscanf (input_line, "ms5607 off: %d",
137                     &l->u.config_int.data[0]))
138                 YUP(AO_CONFIG_MS5607_OFF);
139         if (sscanf (input_line, "ms5607 tcs: %d",
140                     &l->u.config_int.data[0]))
141                 YUP(AO_CONFIG_MS5607_TCS);
142         if (sscanf (input_line, "ms5607 tco: %d",
143                     &l->u.config_int.data[0]))
144                 YUP(AO_CONFIG_MS5607_TCO);
145         if (sscanf (input_line, "ms5607 tref: %d",
146                     &l->u.config_int.data[0]))
147                 YUP(AO_CONFIG_MS5607_TREF);
148         if (sscanf (input_line, "ms5607 tempsens: %d",
149                     &l->u.config_int.data[0]))
150                 YUP(AO_CONFIG_MS5607_TEMPSENS);
151         if (sscanf (input_line, "ms5607 crc: %d",
152                     &l->u.config_int.data[0]))
153                 YUP(AO_CONFIG_MS5607_CRC);
154         return 0;
155 }
156
157 int
158 cc_mega_parse(const char *input_line, struct ao_log_mega *l) {
159         return parse_eeprom(input_line, l) || parse_config(input_line, l);
160 }