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