9afa13b21abd9c6bd83a4995a8eba62c552ffc80
[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
26 static void
27 ao_log_telem_track() {
28         if (ao_monitoring == sizeof (union ao_telemetry_all)) {
29                 switch (ao_log_single_write_data.telemetry.generic.type) {
30                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
31                 case AO_TELEMETRY_SENSOR_TELEMINI:
32                 case AO_TELEMETRY_SENSOR_TELENANO:
33                         if (ao_log_single_write_data.telemetry.sensor.height > ao_max_height) {
34                                 ao_max_height = ao_log_single_write_data.telemetry.sensor.height;
35                         }
36                         if (ao_log_single_write_data.telemetry.sensor.state != ao_flight_state) {
37                                 ao_flight_state = ao_log_single_write_data.telemetry.sensor.state;
38                                 if (ao_flight_state == ao_flight_pad)
39                                         ao_max_height = 0;
40                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
41                         }
42                 }
43         }
44 }
45 void
46 ao_log_single(void)
47 {
48         ao_storage_setup();
49
50         /* This can take a while, so let the rest
51          * of the system finish booting before we start
52          */
53         ao_delay(AO_SEC_TO_TICKS(2));
54
55         ao_log_running = 1;
56         ao_log_single_restart();
57         ao_flight_state = ao_flight_startup;
58         for (;;) {
59                 while (!ao_log_running)
60                         ao_sleep(&ao_log_running);
61
62                 ao_log_monitor_pos = ao_monitor_head;
63                 while (ao_log_running) {
64                         /* Write samples to EEPROM */
65                         while (ao_log_monitor_pos != ao_monitor_head) {
66                                 memcpy(&ao_log_single_write_data.telemetry,
67                                        &ao_monitor_ring[ao_log_monitor_pos],
68                                        AO_LOG_SINGLE_SIZE);
69                                 ao_log_single_write();
70                                 ao_log_monitor_pos = ao_monitor_ring_next(ao_log_monitor_pos);
71                                 ao_log_telem_track();
72                         }
73                         /* Wait for more telemetry data to arrive */
74                         ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
75                 }
76         }
77 }
78
79 void
80 ao_log_single_list(void)
81 {
82         if (ao_log_current_pos != 0)
83                 printf("flight 1 start %x end %x\n",
84                        0,
85                        (uint16_t) ((ao_log_current_pos + 0xff) >> 8));
86         printf ("done\n");
87 }
88
89 void
90 ao_log_single_extra_query(void)
91 {
92 }