altos: Add bit-bang i2c driver
[fw/altos] / src / kernel / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 static uint8_t  ao_log_data_pos;
22
23 /* a hack to make sure that ao_log_records fill the eeprom block in even units */
24 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
25
26 #ifndef AO_SENSOR_INTERVAL_ASCENT
27 #define AO_SENSOR_INTERVAL_ASCENT       1
28 #define AO_SENSOR_INTERVAL_DESCENT      10
29 #define AO_OTHER_INTERVAL               32
30 #endif
31
32 void
33 ao_log(void)
34 {
35         uint16_t        next_sensor, next_other;
36
37         ao_storage_setup();
38
39         ao_log_scan();
40
41         while (!ao_log_running)
42                 ao_sleep(&ao_log_running);
43
44         ao_log_data.type = AO_LOG_FLIGHT;
45         ao_log_data.tick = ao_sample_tick;
46 #if HAS_ACCEL
47         ao_log_data.u.flight.ground_accel = ao_ground_accel;
48 #endif
49         ao_log_data.u.flight.flight = ao_flight_number;
50         ao_log_write(&ao_log_data);
51
52         /* Write the whole contents of the ring to the log
53          * when starting up.
54          */
55         ao_log_data_pos = ao_data_ring_next(ao_sample_data);
56         next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
57         ao_log_state = ao_flight_startup;
58         for (;;) {
59                 /* Write samples to EEPROM */
60                 while (ao_log_data_pos != ao_sample_data) {
61                         ao_log_data.tick = ao_data_ring[ao_log_data_pos].tick;
62                         if ((int16_t) (ao_log_data.tick - next_sensor) >= 0) {
63                                 ao_log_data.type = AO_LOG_SENSOR;
64                                 ao_log_data.u.sensor.accel = ao_data_ring[ao_log_data_pos].adc.accel;
65                                 ao_log_data.u.sensor.pres = ao_data_ring[ao_log_data_pos].adc.pres;
66                                 ao_log_write(&ao_log_data);
67                                 if (ao_log_state <= ao_flight_coast)
68                                         next_sensor = ao_log_data.tick + AO_SENSOR_INTERVAL_ASCENT;
69                                 else
70                                         next_sensor = ao_log_data.tick + AO_SENSOR_INTERVAL_DESCENT;
71                         }
72                         if ((int16_t) (ao_log_data.tick - next_other) >= 0) {
73                                 ao_log_data.type = AO_LOG_TEMP_VOLT;
74                                 ao_log_data.u.temp_volt.temp = ao_data_ring[ao_log_data_pos].adc.temp;
75                                 ao_log_data.u.temp_volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
76                                 ao_log_write(&ao_log_data);
77                                 ao_log_data.type = AO_LOG_DEPLOY;
78                                 ao_log_data.u.deploy.drogue = ao_data_ring[ao_log_data_pos].adc.sense_d;
79                                 ao_log_data.u.deploy.main = ao_data_ring[ao_log_data_pos].adc.sense_m;
80                                 ao_log_write(&ao_log_data);
81                                 next_other = ao_log_data.tick + AO_OTHER_INTERVAL;
82                         }
83                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
84                 }
85                 /* Write state change to EEPROM */
86                 if (ao_flight_state != ao_log_state) {
87                         ao_log_state = ao_flight_state;
88                         ao_log_data.type = AO_LOG_STATE;
89                         ao_log_data.tick = ao_sample_tick;
90                         ao_log_data.u.state.state = ao_log_state;
91                         ao_log_data.u.state.reason = 0;
92                         ao_log_write(&ao_log_data);
93
94                         if (ao_log_state == ao_flight_landed)
95                                 ao_log_stop();
96                 }
97
98                 /* Wait for a while */
99                 ao_delay(AO_MS_TO_TICKS(100));
100
101                 /* Stop logging when told to */
102                 while (!ao_log_running)
103                         ao_sleep(&ao_log_running);
104         }
105 }