12c5d6bb61cb3523dccbb286a3f029de639ef559
[fw/altos] / src / kernel / ao_log_mini.c
1 /*
2  * Copyright © 2012 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 #include <ao_log.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23
24 static __data uint8_t   ao_log_data_pos;
25
26 /* a hack to make sure that ao_log_minis fill the eeprom block in even units */
27 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mini))] ;
28
29 #ifndef AO_SENSOR_INTERVAL_ASCENT
30 #define AO_SENSOR_INTERVAL_ASCENT       1
31 #define AO_SENSOR_INTERVAL_DESCENT      10
32 #endif
33
34 void
35 ao_log(void)
36 {
37         __pdata uint16_t        next_sensor;
38
39         ao_storage_setup();
40
41         ao_log_scan();
42
43         while (!ao_log_running)
44                 ao_sleep(&ao_log_running);
45
46 #if HAS_FLIGHT
47         ao_log_data.type = AO_LOG_FLIGHT;
48         ao_log_data.tick = ao_sample_tick;
49         ao_log_data.u.flight.flight = ao_flight_number;
50         ao_log_data.u.flight.ground_pres = ao_ground_pres;
51         ao_log_write(&ao_log_data);
52 #endif
53
54         /* Write the whole contents of the ring to the log
55          * when starting up.
56          */
57         ao_log_data_pos = ao_data_ring_next(ao_data_head);
58         next_sensor = ao_data_ring[ao_log_data_pos].tick;
59         ao_log_state = ao_flight_startup;
60         for (;;) {
61                 /* Write samples to EEPROM */
62                 while (ao_log_data_pos != ao_data_head) {
63                         ao_log_data.tick = ao_data_ring[ao_log_data_pos].tick;
64                         if ((int16_t) (ao_log_data.tick - next_sensor) >= 0) {
65                                 ao_log_data.type = AO_LOG_SENSOR;
66                                 ao_log_pack24(ao_log_data.u.sensor.pres,
67                                               ao_data_ring[ao_log_data_pos].ms5607_raw.pres);
68                                 ao_log_pack24(ao_log_data.u.sensor.temp,
69                                               ao_data_ring[ao_log_data_pos].ms5607_raw.temp);
70 #if AO_LOG_FORMAT != AO_LOG_FORMAT_DETHERM
71                                 ao_log_data.u.sensor.sense_a = ao_data_ring[ao_log_data_pos].adc.sense_a;
72                                 ao_log_data.u.sensor.sense_m = ao_data_ring[ao_log_data_pos].adc.sense_m;
73                                 ao_log_data.u.sensor.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
74 #endif
75                                 ao_log_write(&ao_log_data);
76                                 if (ao_log_state <= ao_flight_coast)
77                                         next_sensor = ao_log_data.tick + AO_SENSOR_INTERVAL_ASCENT;
78                                 else
79                                         next_sensor = ao_log_data.tick + AO_SENSOR_INTERVAL_DESCENT;
80                         }
81                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
82                 }
83 #if HAS_FLIGHT
84                 /* Write state change to EEPROM */
85                 if (ao_flight_state != ao_log_state) {
86                         ao_log_state = ao_flight_state;
87                         ao_log_data.type = AO_LOG_STATE;
88                         ao_log_data.tick = ao_time();
89                         ao_log_data.u.state.state = ao_log_state;
90                         ao_log_data.u.state.reason = 0;
91                         ao_log_write(&ao_log_data);
92
93                         if (ao_log_state == ao_flight_landed)
94                                 ao_log_stop();
95                 }
96 #endif
97
98                 ao_log_flush();
99
100                 /* Wait for a while */
101                 ao_delay(AO_MS_TO_TICKS(100));
102
103                 /* Stop logging when told to */
104                 while (!ao_log_running)
105                         ao_sleep(&ao_log_running);
106         }
107 }