From 8083aa731c99d09bdd4a8c216bb11f846734d7df Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 26 May 2013 18:57:58 -0600 Subject: [PATCH] ao-tools: Add ao-mega tool to parse TeleMega eeprom files Signed-off-by: Keith Packard --- ao-tools/ao-mega/Makefile.am | 12 +++ ao-tools/ao-mega/ao-mega.c | 142 +++++++++++++++++++++++++++++++ ao-tools/lib/Makefile.am | 1 + ao-tools/lib/cc-mega.c | 160 +++++++++++++++++++++++++++++++++++ 4 files changed, 315 insertions(+) create mode 100644 ao-tools/ao-mega/Makefile.am create mode 100644 ao-tools/ao-mega/ao-mega.c create mode 100644 ao-tools/lib/cc-mega.c diff --git a/ao-tools/ao-mega/Makefile.am b/ao-tools/ao-mega/Makefile.am new file mode 100644 index 00000000..22b62608 --- /dev/null +++ b/ao-tools/ao-mega/Makefile.am @@ -0,0 +1,12 @@ +bin_PROGRAMS=ao-mega + +AM_CFLAGS=-I$(top_srcdir)/ao-tools/lib $(LIBUSB_CFLAGS) +AO_POSTFLIGHT_LIBS=$(top_builddir)/ao-tools/lib/libao-tools.a + +ao_mega_DEPENDENCIES = $(AO_POSTFLIGHT_LIBS) + +ao_mega_LDADD=$(AO_POSTFLIGHT_LIBS) $(LIBUSB_LIBS) + +ao_mega_SOURCES = ao-mega.c + +man_MANS = ao-mega.1 diff --git a/ao-tools/ao-mega/ao-mega.c b/ao-tools/ao-mega/ao-mega.c new file mode 100644 index 00000000..e06df88d --- /dev/null +++ b/ao-tools/ao-mega/ao-mega.c @@ -0,0 +1,142 @@ +/* + * Copyright © 2011 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "cc.h" + +static const struct option options[] = { + { 0, 0, 0, 0}, +}; + +static void usage(char *program) +{ + fprintf(stderr, "usage: %s\n" + "\t{flight.mega} ...\n", program); + exit(1); +} + +#define bool(b) ((b) ? "true" : "false") + +static const char *state_names[] = { + "startup", + "idle", + "pad", + "boost", + "fast", + "coast", + "drogue", + "main", + "landed", + "invalid" +}; + + +#define NUM_STATE (sizeof state_names/sizeof state_names[0]) + +int +main (int argc, char **argv) +{ + char line[256]; + int c, i, ret, j; + char *s; + FILE *file; + int serial; + const char *state; + while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) { + switch (c) { + default: + usage(argv[0]); + break; + } + } + for (i = optind; i < argc; i++) { + file = fopen(argv[i], "r"); + if (!file) { + perror(argv[i]); + ret++; + continue; + } + s = strstr(argv[i], "-serial-"); + if (s) + serial = atoi(s + 8); + else + serial = 0; + while (fgets(line, sizeof (line), file)) { + struct ao_log_mega log; + + if (cc_mega_parse(line, &log)) { + if (log.is_config) { + printf ("kind %d\n", log.u.config_int.kind); + } else { + printf ("tick %5d ", log.tick); + switch (log.type) { + case AO_LOG_FLIGHT: + printf ("flight %5u ground_accel %d ground_pres %u\n", + log.u.flight.flight, + log.u.flight.ground_accel, + log.u.flight.ground_pres); + break; + case AO_LOG_STATE: + if (log.u.state.state < NUM_STATE) + state = state_names[log.u.state.state]; + else + state = "invalid"; + printf ("state %d (%s)\n", log.u.state.state, state); + break; + case AO_LOG_SENSOR: + 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", + log.u.sensor.pres, + log.u.sensor.temp, + log.u.sensor.accel_x, + log.u.sensor.accel_y, + log.u.sensor.accel_z, + log.u.sensor.gyro_x, + log.u.sensor.gyro_y, + log.u.sensor.gyro_z, + log.u.sensor.mag_x, + log.u.sensor.mag_y, + log.u.sensor.mag_z, + log.u.sensor.accel); + break; + case AO_LOG_TEMP_VOLT: + printf ("batt %6d pbatt %6d n_sense %d", + log.u.volt.v_batt, + log.u.volt.v_pbatt, + log.u.volt.n_sense); + for (j = 0; j < log.u.volt.n_sense; j++) { + printf (" s%d %6d", + j, log.u.volt.sense[j]); + } + printf ("\n"); + break; + default: + printf ("type %c\n", log.type, log.tick); + break; + } + } + } + } + fclose (file); + + } + return ret; +} diff --git a/ao-tools/lib/Makefile.am b/ao-tools/lib/Makefile.am index 1f8f2e42..fd4dab25 100644 --- a/ao-tools/lib/Makefile.am +++ b/ao-tools/lib/Makefile.am @@ -21,6 +21,7 @@ libao_tools_a_SOURCES = \ cc-convert.c \ cc-dsp.c \ cc-integrate.c \ + cc-mega.c \ cc-period.c \ cc-process.c \ cc-usb.c \ diff --git a/ao-tools/lib/cc-mega.c b/ao-tools/lib/cc-mega.c new file mode 100644 index 00000000..3aa24a6d --- /dev/null +++ b/ao-tools/lib/cc-mega.c @@ -0,0 +1,160 @@ +/* + * Copyright © 2012 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include "cc.h" +#include +#include + +static const char * +parse_hex(const char *data, int *result) +{ + char d[12]; + int x; + int i; + + while (isspace (*data)) + data++; + for (i = 0; i < sizeof (d) - 1 && isxdigit(*data); i++) + d[i] = *data++; + d[i] = '\0'; + if (sscanf(d, "%x", &x) != 1) + return NULL; + *result = x; + return data; +} + +static const char * +parse_uint16(const char *data, uint16_t *result) +{ + int x; + data = parse_hex(data, &x); + *result =x; + return data; +} + +static const char * +parse_uint8(const char *data, uint8_t *result) +{ + int x; + data = parse_hex(data, &x); + *result =x; + return data; +} + +static int +parse_eeprom(const char *input_line, struct ao_log_mega *l) { + const char *line; + int b; + + if (input_line[1] != ' ') + return 0; + if (!isupper(input_line[0])) + return 0; + + l->type = input_line[0]; + l->is_config = 0; + line = input_line + 2; + + line = parse_uint16(line, &l->tick); + for (b = 0; b < 28; b++) { + if (!line) + return 0; + line = parse_uint8(line, &l->u.bytes[b]); + } + return 1; +} + +#define YUP(t) do { \ + l->u.config_int.kind = (t); \ + l->is_config = 1; \ + return 1; \ + } while (0); + +static int +parse_config(const char *input_line, struct ao_log_mega *l) { + if (sscanf (input_line, "Config version: %d.%d", + &l->u.config_int.data[0], + &l->u.config_int.data[1])) + YUP(AO_CONFIG_CONFIG); + if (sscanf (input_line, "Main deploy: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MAIN); + if (sscanf (input_line, "Apogee delay: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_APOGEE); + if (sscanf (input_line, "Apogee lockout: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_LOCKOUT); + if (sscanf (input_line, "Frequency: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_FREQUENCY); + if (sscanf (input_line, "Radio enable: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_RADIO_ENABLE); + if (sscanf (input_line, "Accel cal +1g: %d -1g: %d", + &l->u.config_int.data[0], + &l->u.config_int.data[1])) + YUP(AO_CONFIG_ACCEL_CAL); + if (sscanf (input_line, "Radio cal: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_RADIO_CAL); + if (sscanf (input_line, "Max flight log: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MAX_LOG); + if (sscanf (input_line, "Ignite mode: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_IGNITE_MODE); + if (sscanf (input_line, "Pad orientation: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_PAD_ORIENTATION); + if (sscanf (input_line, "serial-number %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_SERIAL_NUMBER); + if (sscanf (input_line, "log-format %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_LOG_FORMAT); + if (sscanf (input_line, "ms5607 reserved: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_RESERVED); + if (sscanf (input_line, "ms5607 sens: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_SENS); + if (sscanf (input_line, "ms5607 off: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_OFF); + if (sscanf (input_line, "ms5607 tcs: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_TCS); + if (sscanf (input_line, "ms5607 tco: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_TCO); + if (sscanf (input_line, "ms5607 tref: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_TREF); + if (sscanf (input_line, "ms5607 tempsens: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_TEMPSENS); + if (sscanf (input_line, "ms5607 crc: %d", + &l->u.config_int.data[0])) + YUP(AO_CONFIG_MS5607_CRC); + return 0; +} + +int +cc_mega_parse(const char *input_line, struct ao_log_mega *l) { + return parse_eeprom(input_line, l) || parse_config(input_line, l); +} -- 2.30.2