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