altos: struct ao_log_mega doesn't have a ground temp value
[fw/altos] / src / core / ao_log_big.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 static __xdata uint8_t  ao_log_mutex;
21 static __xdata struct ao_log_record log;
22
23 __code uint8_t ao_log_format = AO_LOG_FORMAT_FULL;
24
25 static uint8_t
26 ao_log_csum(__xdata uint8_t *b) __reentrant
27 {
28         uint8_t sum = 0x5a;
29         uint8_t i;
30
31         for (i = 0; i < sizeof (struct ao_log_record); i++)
32                 sum += *b++;
33         return -sum;
34 }
35
36 uint8_t
37 ao_log_data(__xdata struct ao_log_record *log) __reentrant
38 {
39         uint8_t wrote = 0;
40         /* set checksum */
41         log->csum = 0;
42         log->csum = ao_log_csum((__xdata uint8_t *) log);
43         ao_mutex_get(&ao_log_mutex); {
44                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
45                         ao_log_stop();
46                 if (ao_log_running) {
47                         wrote = 1;
48                         ao_storage_write(ao_log_current_pos,
49                                          log,
50                                          sizeof (struct ao_log_record));
51                         ao_log_current_pos += sizeof (struct ao_log_record);
52                 }
53         } ao_mutex_put(&ao_log_mutex);
54         return wrote;
55 }
56
57 static uint8_t
58 ao_log_dump_check_data(void)
59 {
60         if (ao_log_csum((uint8_t *) &log) != 0)
61                 return 0;
62         return 1;
63 }
64
65 static __data uint8_t   ao_log_data_pos;
66
67 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
68 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
69
70 #ifndef AO_SENSOR_INTERVAL_ASCENT
71 #define AO_SENSOR_INTERVAL_ASCENT       1
72 #define AO_SENSOR_INTERVAL_DESCENT      10
73 #define AO_OTHER_INTERVAL               32
74 #endif
75
76 void
77 ao_log(void)
78 {
79         __pdata uint16_t        next_sensor, next_other;
80
81         ao_storage_setup();
82
83         ao_log_scan();
84
85         while (!ao_log_running)
86                 ao_sleep(&ao_log_running);
87
88         log.type = AO_LOG_FLIGHT;
89         log.tick = ao_sample_tick;
90 #if HAS_ACCEL
91         log.u.flight.ground_accel = ao_ground_accel;
92 #endif
93         log.u.flight.flight = ao_flight_number;
94         ao_log_data(&log);
95
96         /* Write the whole contents of the ring to the log
97          * when starting up.
98          */
99         ao_log_data_pos = ao_data_ring_next(ao_sample_data);
100         next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
101         ao_log_state = ao_flight_startup;
102         for (;;) {
103                 /* Write samples to EEPROM */
104                 while (ao_log_data_pos != ao_sample_data) {
105                         log.tick = ao_data_ring[ao_log_data_pos].tick;
106                         if ((int16_t) (log.tick - next_sensor) >= 0) {
107                                 log.type = AO_LOG_SENSOR;
108                                 log.u.sensor.accel = ao_data_ring[ao_log_data_pos].adc.accel;
109                                 log.u.sensor.pres = ao_data_ring[ao_log_data_pos].adc.pres;
110                                 ao_log_data(&log);
111                                 if (ao_log_state <= ao_flight_coast)
112                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
113                                 else
114                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
115                         }
116                         if ((int16_t) (log.tick - next_other) >= 0) {
117                                 log.type = AO_LOG_TEMP_VOLT;
118                                 log.u.temp_volt.temp = ao_data_ring[ao_log_data_pos].adc.temp;
119                                 log.u.temp_volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
120                                 ao_log_data(&log);
121                                 log.type = AO_LOG_DEPLOY;
122                                 log.u.deploy.drogue = ao_data_ring[ao_log_data_pos].adc.sense_d;
123                                 log.u.deploy.main = ao_data_ring[ao_log_data_pos].adc.sense_m;
124                                 ao_log_data(&log);
125                                 next_other = log.tick + AO_OTHER_INTERVAL;
126                         }
127                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
128                 }
129                 /* Write state change to EEPROM */
130                 if (ao_flight_state != ao_log_state) {
131                         ao_log_state = ao_flight_state;
132                         log.type = AO_LOG_STATE;
133                         log.tick = ao_sample_tick;
134                         log.u.state.state = ao_log_state;
135                         log.u.state.reason = 0;
136                         ao_log_data(&log);
137
138                         if (ao_log_state == ao_flight_landed)
139                                 ao_log_stop();
140                 }
141
142                 /* Wait for a while */
143                 ao_delay(AO_MS_TO_TICKS(100));
144
145                 /* Stop logging when told to */
146                 while (!ao_log_running)
147                         ao_sleep(&ao_log_running);
148         }
149 }
150
151 uint16_t
152 ao_log_flight(uint8_t slot)
153 {
154         if (!ao_storage_read(ao_log_pos(slot),
155                              &log,
156                              sizeof (struct ao_log_record)))
157                 return 0;
158
159         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
160                 return log.u.flight.flight;
161         return 0;
162 }