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