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