altos: Shorten help strings
[fw/altos] / src / core / 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 #include "ao_telem.h"
20
21 #if !HAS_MONITOR
22 #error Must define HAS_MONITOR to 1
23 #endif
24
25 #ifndef LEGACY_MONITOR
26 #error Must define LEGACY_MONITOR
27 #endif
28
29 #ifndef HAS_MONITOR_PUT
30 #define HAS_MONIOTOR_PUT 1
31 #endif
32
33 #ifndef AO_MONITOR_LED
34 #error Must define AO_MONITOR_LED
35 #endif
36
37 __data uint8_t ao_monitoring;
38 static __data uint8_t ao_monitor_disabled;
39 static __data uint8_t ao_internal_monitoring;
40 static __data uint8_t ao_external_monitoring;
41
42 __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
43
44 __data uint8_t  ao_monitor_head;
45
46 static void
47 _ao_monitor_adjust(void)
48 {
49         if (ao_monitoring)
50                 ao_radio_recv_abort();
51         if (ao_monitor_disabled)
52                 ao_monitoring = 0;
53         else {
54                 if (ao_external_monitoring)
55                         ao_monitoring = ao_external_monitoring;
56                 else
57                         ao_monitoring = ao_internal_monitoring;
58         }
59         ao_wakeup(DATA_TO_XDATA(&ao_monitoring));
60 }
61
62 void
63 ao_monitor_get(void)
64 {
65         uint8_t size;
66
67         for (;;) {
68                 switch (ao_monitoring) {
69                 case 0:
70                         ao_sleep(DATA_TO_XDATA(&ao_monitoring));
71                         continue;
72 #if LEGACY_MONITOR
73                 case AO_MONITORING_ORIG:
74                         size = sizeof (struct ao_telemetry_orig_recv);
75                         break;
76 #endif
77                 default:
78                         if (ao_monitoring > AO_MAX_TELEMETRY)
79                                 ao_monitoring = AO_MAX_TELEMETRY;
80                         size = ao_monitoring;
81                         break;
82                 }
83                 if (!ao_radio_recv(&ao_monitor_ring[ao_monitor_head], size + 2))
84                         continue;
85                 ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
86                 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
87         }
88 }
89
90 #if AO_MONITOR_LED
91 __xdata struct ao_task ao_monitor_blink_task;
92
93 void
94 ao_monitor_blink(void)
95 {
96         for (;;) {
97                 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
98                 ao_led_for(AO_MONITOR_LED, AO_MS_TO_TICKS(100));
99         }
100 }
101 #endif
102
103 #if HAS_MONITOR_PUT
104
105 void
106 ao_monitor_put(void)
107 {
108 #if LEGACY_MONITOR
109         __xdata char callsign[AO_MAX_CALLSIGN+1];
110         int16_t rssi;
111 #endif
112         uint8_t ao_monitor_tail;
113         uint8_t state;
114         uint8_t sum, byte;
115         __xdata union ao_monitor        *m;
116
117 #define recv_raw        ((m->raw))
118 #define recv_orig       ((m->orig))
119 #define recv_tiny       ((m->tiny))
120
121         ao_monitor_tail = ao_monitor_head;
122         for (;;) {
123                 while (!ao_external_monitoring)
124                         ao_sleep(DATA_TO_XDATA(&ao_external_monitoring));
125                 while (ao_monitor_tail == ao_monitor_head && ao_external_monitoring)
126                         ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
127                 m = &ao_monitor_ring[ao_monitor_tail];
128                 ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
129                 switch (ao_monitoring) {
130                 case 0:
131                         break;
132 #if LEGACY_MONITOR
133                 case AO_MONITORING_ORIG:
134                         state = recv_orig.telemetry_orig.flight_state;
135
136                         /* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
137                         rssi = (int16_t) (recv_orig.rssi >> 1) - 74;
138                         ao_xmemcpy(callsign, recv_orig.telemetry_orig.callsign, AO_MAX_CALLSIGN);
139                         if (state > ao_flight_invalid)
140                                 state = ao_flight_invalid;
141                         if (recv_orig.status & PKT_APPEND_STATUS_1_CRC_OK) {
142
143                                 /* General header fields */
144                                 printf(AO_TELEM_VERSION " %d "
145                                        AO_TELEM_CALL " %s "
146                                        AO_TELEM_SERIAL " %d "
147                                        AO_TELEM_FLIGHT " %d "
148                                        AO_TELEM_RSSI " %d "
149                                        AO_TELEM_STATE " %s "
150                                        AO_TELEM_TICK " %d ",
151                                        AO_TELEMETRY_VERSION,
152                                        callsign,
153                                        recv_orig.telemetry_orig.serial,
154                                        recv_orig.telemetry_orig.flight,
155                                        rssi,
156                                        ao_state_names[state],
157                                        recv_orig.telemetry_orig.adc.tick);
158
159                                 /* Raw sensor values */
160                                 printf(AO_TELEM_RAW_ACCEL " %d "
161                                        AO_TELEM_RAW_BARO " %d "
162                                        AO_TELEM_RAW_THERMO " %d "
163                                        AO_TELEM_RAW_BATT " %d "
164                                        AO_TELEM_RAW_DROGUE " %d "
165                                        AO_TELEM_RAW_MAIN " %d ",
166                                        recv_orig.telemetry_orig.adc.accel,
167                                        recv_orig.telemetry_orig.adc.pres,
168                                        recv_orig.telemetry_orig.adc.temp,
169                                        recv_orig.telemetry_orig.adc.v_batt,
170                                        recv_orig.telemetry_orig.adc.sense_d,
171                                        recv_orig.telemetry_orig.adc.sense_m);
172
173                                 /* Sensor calibration values */
174                                 printf(AO_TELEM_CAL_ACCEL_GROUND " %d "
175                                        AO_TELEM_CAL_BARO_GROUND " %d "
176                                        AO_TELEM_CAL_ACCEL_PLUS " %d "
177                                        AO_TELEM_CAL_ACCEL_MINUS " %d ",
178                                        recv_orig.telemetry_orig.ground_accel,
179                                        recv_orig.telemetry_orig.ground_pres,
180                                        recv_orig.telemetry_orig.accel_plus_g,
181                                        recv_orig.telemetry_orig.accel_minus_g);
182
183                                 if (recv_orig.telemetry_orig.u.k.unused == 0x8000) {
184                                         /* Kalman state values */
185                                         printf(AO_TELEM_KALMAN_HEIGHT " %d "
186                                                AO_TELEM_KALMAN_SPEED " %d "
187                                                AO_TELEM_KALMAN_ACCEL " %d ",
188                                                recv_orig.telemetry_orig.height,
189                                                recv_orig.telemetry_orig.u.k.speed,
190                                                recv_orig.telemetry_orig.accel);
191                                 } else {
192                                         /* Ad-hoc flight values */
193                                         printf(AO_TELEM_ADHOC_ACCEL " %d "
194                                                AO_TELEM_ADHOC_SPEED " %ld "
195                                                AO_TELEM_ADHOC_BARO " %d ",
196                                                recv_orig.telemetry_orig.accel,
197                                                recv_orig.telemetry_orig.u.flight_vel,
198                                                recv_orig.telemetry_orig.height);
199                                 }
200                                 ao_gps_print(&recv_orig.telemetry_orig.gps);
201                                 ao_gps_tracking_print(&recv_orig.telemetry_orig.gps_tracking);
202                                 putchar('\n');
203 #if HAS_RSSI
204                                 ao_rssi_set(rssi);
205 #endif
206                         } else {
207                                 printf("CRC INVALID RSSI %3d\n", rssi);
208                         }
209                         break;
210 #endif /* LEGACY_MONITOR */
211                 default:
212                         printf ("TELEM %02x", ao_monitoring + 2);
213                         sum = 0x5a;
214                         for (state = 0; state < ao_monitoring + 2; state++) {
215                                 byte = recv_raw.packet[state];
216                                 sum += byte;
217                                 printf("%02x", byte);
218                         }
219                         printf("%02x\n", sum);
220 #if HAS_RSSI
221                         if (recv_raw.packet[ao_monitoring + 1] & PKT_APPEND_STATUS_1_CRC_OK) {
222                                 rssi = ((int16_t) recv_raw.packet[ao_monitoring] >> 1) - 74;
223                                 ao_rssi_set(rssi);
224                         }
225 #endif
226                         break;
227                 }
228                 ao_usb_flush();
229         }
230 }
231
232 __xdata struct ao_task ao_monitor_put_task;
233 #endif
234
235 __xdata struct ao_task ao_monitor_get_task;
236
237 void
238 ao_monitor_set(uint8_t monitoring)
239 {
240         ao_internal_monitoring = monitoring;
241         _ao_monitor_adjust();
242 }
243
244 void
245 ao_monitor_disable(void)
246 {
247         ++ao_monitor_disabled;
248         _ao_monitor_adjust();
249 }
250
251 void
252 ao_monitor_enable(void)
253 {
254         --ao_monitor_disabled;
255         _ao_monitor_adjust();
256 }
257
258 #if HAS_MONITOR_PUT
259 static void
260 set_monitor(void)
261 {
262         ao_cmd_hex();
263         ao_external_monitoring = ao_cmd_lex_i;
264         ao_wakeup(DATA_TO_XDATA(&ao_external_monitoring));
265         ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
266         _ao_monitor_adjust();
267 }
268
269 __code struct ao_cmds ao_monitor_cmds[] = {
270         { set_monitor,  "m <0 off, 1 old, 20 std>\0Set radio monitoring" },
271         { 0,    NULL },
272 };
273 #endif
274
275 void
276 ao_monitor_init(void) __reentrant
277 {
278 #if HAS_MONITOR_PUT
279         ao_cmd_register(&ao_monitor_cmds[0]);
280         ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
281 #endif
282         ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
283 #if AO_MONITOR_LED
284         ao_add_task(&ao_monitor_blink_task, ao_monitor_blink, "monitor_blink");
285 #endif
286 }