altos: Report continuity in telebt
[fw/altos] / src / core / ao_log_telem.c
1 /*
2  * Copyright © 2011 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 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMETRY;
21
22 static __data uint8_t                   ao_log_monitor_pos;
23 __pdata enum ao_flight_state            ao_flight_state;
24 __pdata int16_t                         ao_max_height;  /* max of ao_height */
25 __pdata int16_t                         sense_d, sense_m;
26
27 static void
28 ao_log_telem_track() {
29         if (ao_monitoring == sizeof (union ao_telemetry_all)) {
30                 switch (ao_log_single_write_data.telemetry.generic.type) {
31                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
32                 case AO_TELEMETRY_SENSOR_TELEMINI:
33                         sense_d = ao_log_single_write_data.telemetry.sensor.sense_d;
34                         sense_m = ao_log_single_write_data.telemetry.sensor.sense_m;
35                         /* fall through ... */
36                 case AO_TELEMETRY_SENSOR_TELENANO:
37                         if (ao_log_single_write_data.telemetry.sensor.height > ao_max_height) {
38                                 ao_max_height = ao_log_single_write_data.telemetry.sensor.height;
39                         }
40                         if (ao_log_single_write_data.telemetry.sensor.state != ao_flight_state) {
41                                 ao_flight_state = ao_log_single_write_data.telemetry.sensor.state;
42                                 if (ao_flight_state == ao_flight_pad)
43                                         ao_max_height = 0;
44                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
45                         }
46                 }
47         }
48 }
49
50 enum ao_igniter_status
51 ao_igniter_status(enum ao_igniter igniter)
52 {
53         int16_t value;
54
55         switch (igniter) {
56         case ao_igniter_drogue:
57                 value = sense_d;
58                 break;
59         case ao_igniter_main:
60                 value = sense_m;
61                 break;
62         default:
63                 value = 0;
64                 break;
65         }
66         if (value < AO_IGNITER_OPEN)
67                 return ao_igniter_open;
68         else if (value > AO_IGNITER_CLOSED)
69                 return ao_igniter_ready;
70         else
71                 return ao_igniter_unknown;
72 }
73
74 void
75 ao_log_single(void)
76 {
77         ao_storage_setup();
78
79         /* This can take a while, so let the rest
80          * of the system finish booting before we start
81          */
82         ao_delay(AO_SEC_TO_TICKS(2));
83
84         ao_log_running = 1;
85         ao_log_single_restart();
86         ao_flight_state = ao_flight_startup;
87         for (;;) {
88                 while (!ao_log_running)
89                         ao_sleep(&ao_log_running);
90
91                 ao_log_monitor_pos = ao_monitor_head;
92                 while (ao_log_running) {
93                         /* Write samples to EEPROM */
94                         while (ao_log_monitor_pos != ao_monitor_head) {
95                                 memcpy(&ao_log_single_write_data.telemetry,
96                                        &ao_monitor_ring[ao_log_monitor_pos],
97                                        AO_LOG_SINGLE_SIZE);
98                                 ao_log_single_write();
99                                 ao_log_monitor_pos = ao_monitor_ring_next(ao_log_monitor_pos);
100                                 ao_log_telem_track();
101                         }
102                         /* Wait for more telemetry data to arrive */
103                         ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
104                 }
105         }
106 }
107
108 void
109 ao_log_single_list(void)
110 {
111         if (ao_log_current_pos != 0)
112                 printf("flight 1 start %x end %x\n",
113                        0,
114                        (uint16_t) ((ao_log_current_pos + 0xff) >> 8));
115         printf ("done\n");
116 }
117
118 void
119 ao_log_single_extra_query(void)
120 {
121 }