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