2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
28 static const struct option options[] = {
29 { .name = "tty", .has_arg = 1, .val = 'T' },
30 { .name = "device", .has_arg = 1, .val = 'D' },
31 { .name = "frequency", .has_arg = 1, .val = 'F' },
32 { .name = "realtime", .has_arg = 0, .val = 'R' },
33 { .name = "verbose", .has_arg = 0, .val = 'v' },
34 { .name = "fake", .has_arg = 0, .val = 'f' },
38 static void usage(char *program)
40 fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--frequency <kHz>] [--realtime] [--verbose] [--fake] file.telem ...\n", program);
44 #define bool(b) ((b) ? "true" : "false")
46 struct ao_telem_list {
47 struct ao_telem_list *next;
48 union ao_telemetry_all telem;
51 static struct ao_telem_list *telem_list, **telem_last;
54 trim_telem(uint16_t time)
56 while (telem_list && (int16_t) (time - telem_list->telem.generic.tick) > 0) {
57 struct ao_telem_list *next = telem_list->next;
62 telem_last = &telem_list;
66 add_telem(union ao_telemetry_all *telem)
68 struct ao_telem_list *new = malloc (sizeof (struct ao_telem_list));
69 trim_telem((uint16_t) (telem->generic.tick - 20 * 100));
73 telem_last = &new->next;
76 static enum ao_flight_state cur_state = ao_flight_invalid;
77 static enum ao_flight_state last_state = ao_flight_invalid;
79 static enum ao_flight_state
80 packet_state(union ao_telemetry_all *telem)
82 switch (telem->generic.type) {
83 case AO_TELEMETRY_SENSOR_TELEMETRUM:
84 case AO_TELEMETRY_SENSOR_TELEMINI:
85 case AO_TELEMETRY_SENSOR_TELENANO:
86 cur_state = telem->sensor.state;
88 case AO_TELEMETRY_MEGA_DATA:
89 cur_state = telem->mega_data.state;
91 case AO_TELEMETRY_METRUM_SENSOR:
92 cur_state = telem->metrum_sensor.state;
94 case AO_TELEMETRY_MINI:
95 cur_state = telem->mini.state;
101 static const char *state_names[] = {
115 send_telem(struct cc_usb *cc, union ao_telemetry_all *telem)
121 if (cur_state != last_state) {
122 if (0 <= cur_state && cur_state < sizeof(state_names) / sizeof (state_names[0]))
123 printf ("%s\n", state_names[cur_state]);
124 last_state = cur_state;
126 cc_usb_printf(cc, "S 20\n");
127 b = (uint8_t *) telem;
128 for (i = 0; i < 0x20; i++)
129 cc_usb_printf(cc, "%02x", b[i]);
134 do_delay(uint16_t now, uint16_t then)
136 int16_t delay = (int16_t) (now - then);
138 if (delay > 0 && delay < 1000)
139 usleep(delay * 10 * 1000);
143 send_queued(struct cc_usb *cc, int pause)
145 struct ao_telem_list *next;
150 if (started && pause)
151 do_delay(telem_list->telem.generic.tick, tick);
152 tick = telem_list->telem.generic.tick;
154 send_telem(cc, &telem_list->telem);
156 next = telem_list->next;
164 main (int argc, char **argv)
180 while ((c = getopt_long(argc, argv, "vRfT:D:F:r:", options, NULL)) != -1) {
213 fprintf(stderr, "Rate %d isn't 38400, 9600 or 2400\n", rate);
224 tty = cc_usbdevs_find_by_arg(device, "TeleDongle");
226 tty = getenv("ALTOS_TTY");
229 cc = cc_usb_open(tty);
233 cc_usb_printf(cc, "m 0\n");
234 cc_usb_printf(cc, "c F %d\n", freq);
235 cc_usb_printf(cc, "c T %d\n", rate);
238 union ao_telemetry_all telem;
241 memset(&telem, '\0', sizeof (telem));
242 telem.generic.serial = 1;
243 telem.generic.type = 0;
244 for (i = 0; i < sizeof (telem.generic.payload); i++)
245 telem.generic.payload[i] = i & 7;
247 telem.generic.tick += 50;
248 send_telem(cc, &telem);
252 for (i = optind; i < argc; i++) {
253 file = fopen(argv[i], "r");
261 while (fgets(line, sizeof (line), file)) {
262 union ao_telemetry_all telem;
264 if (cc_telemetry_parse(line, &telem)) {
266 * Skip packets with CRC errors.
268 if ((telem.generic.status & (1 << 7)) == 0)
272 printf ("type %4d\n", telem.generic.type);
274 if (started || realtime) {
275 do_delay(telem.generic.tick, last_tick);
276 last_tick = telem.generic.tick;
277 send_telem(cc, &telem);
279 enum ao_flight_state state = packet_state(&telem);
280 printf ("\tstate %4d\n", state);
282 if (ao_flight_pad < state && state < ao_flight_landed) {
283 printf ("started\n");
285 last_tick = send_queued(cc, realtime);