Split out ao_state_names to separate file
[fw/altos] / ao_monitor.c
1 /*
2  * Copyright © 2009 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 "ao.h"
19
20 __xdata uint8_t ao_monitoring;
21 __pdata uint8_t ao_monitor_led;
22
23 void
24 ao_monitor(void)
25 {
26         __xdata struct ao_radio_recv recv;
27         __xdata char callsign[AO_MAX_CALLSIGN+1];
28         uint8_t state;
29
30         for (;;) {
31                 __critical while (!ao_monitoring)
32                         ao_sleep(&ao_monitoring);
33                 ao_radio_recv(&recv);
34                 state = recv.telemetry.flight_state;
35                 memcpy(callsign, recv.telemetry.callsign, AO_MAX_CALLSIGN);
36                 if (state > ao_flight_invalid)
37                         state = ao_flight_invalid;
38                 if (recv.status & PKT_APPEND_STATUS_1_CRC_OK) {
39                         printf ("CALL %s SERIAL %3d RSSI %3d STATUS %02x STATE %7s ",
40                                 callsign,
41                                 recv.telemetry.addr,
42                                 (int) recv.rssi - 74, recv.status,
43                                 ao_state_names[state]);
44                         printf("%5u a: %5d p: %5d t: %5d v: %5d d: %5d m: %5d ",
45                                recv.telemetry.adc.tick,
46                                recv.telemetry.adc.accel,
47                                recv.telemetry.adc.pres,
48                                recv.telemetry.adc.temp,
49                                recv.telemetry.adc.v_batt,
50                                recv.telemetry.adc.sense_d,
51                                recv.telemetry.adc.sense_m);
52                         ao_gps_print(&recv.telemetry.gps);
53                         ao_rssi_set((int) recv.rssi - 74);
54                 } else {
55                         printf("CRC INVALID RSSI %3d\n", (int) recv.rssi - 74);
56                 }
57                 ao_usb_flush();
58                 ao_led_toggle(ao_monitor_led);
59         }
60 }
61
62 __xdata struct ao_task ao_monitor_task;
63
64 static void
65 ao_set_monitor(void)
66 {
67         ao_cmd_hex();
68         ao_monitoring = ao_cmd_lex_i != 0;
69         ao_wakeup(&ao_monitoring);
70 }
71
72 __code struct ao_cmds ao_monitor_cmds[] = {
73         { 'm',  ao_set_monitor,         "m <0 off, 1 on>                    Enable/disable radio monitoring" },
74         { 0,    ao_set_monitor, NULL },
75 };
76
77 void
78 ao_monitor_init(uint8_t monitor_led)
79 {
80         ao_monitor_led = monitor_led;
81         ao_monitoring = 0;
82         ao_cmd_register(&ao_monitor_cmds[0]);
83         ao_add_task(&ao_monitor_task, ao_monitor, "monitor");
84 }