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