altoslib: Missed a couple of easy mini voltage API changes
[fw/altos] / src / core / 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; 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 #include <ao_log.h>
20 #include <ao_data.h>
21 #include <ao_flight.h>
22
23 static __xdata uint8_t  ao_log_mutex;
24 static __xdata struct ao_log_mini log;
25
26 __code uint8_t ao_log_format = AO_LOG_FORMAT;
27
28 static uint8_t
29 ao_log_csum(__xdata uint8_t *b) __reentrant
30 {
31         uint8_t sum = 0x5a;
32         uint8_t i;
33
34         for (i = 0; i < sizeof (struct ao_log_mini); i++)
35                 sum += *b++;
36         return -sum;
37 }
38
39 uint8_t
40 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant
41 {
42         uint8_t wrote = 0;
43         /* set checksum */
44         log->csum = 0;
45         log->csum = ao_log_csum((__xdata uint8_t *) log);
46         ao_mutex_get(&ao_log_mutex); {
47                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
48                         ao_log_stop();
49                 if (ao_log_running) {
50                         wrote = 1;
51                         ao_storage_write(ao_log_current_pos,
52                                          log,
53                                          sizeof (struct ao_log_mini));
54                         ao_log_current_pos += sizeof (struct ao_log_mini);
55                 }
56         } ao_mutex_put(&ao_log_mutex);
57         return wrote;
58 }
59
60 static uint8_t
61 ao_log_dump_check_data(void)
62 {
63         if (ao_log_csum((uint8_t *) &log) != 0)
64                 return 0;
65         return 1;
66 }
67
68 static __data uint8_t   ao_log_data_pos;
69
70 /* a hack to make sure that ao_log_minis fill the eeprom block in even units */
71 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mini))] ;
72
73 #ifndef AO_SENSOR_INTERVAL_ASCENT
74 #define AO_SENSOR_INTERVAL_ASCENT       1
75 #define AO_SENSOR_INTERVAL_DESCENT      10
76 #endif
77
78 void
79 ao_log(void)
80 {
81         __pdata uint16_t        next_sensor;
82
83         ao_storage_setup();
84
85         ao_log_scan();
86
87         while (!ao_log_running)
88                 ao_sleep(&ao_log_running);
89
90 #if HAS_FLIGHT
91         log.type = AO_LOG_FLIGHT;
92         log.tick = ao_sample_tick;
93         log.u.flight.flight = ao_flight_number;
94         log.u.flight.ground_pres = ao_ground_pres;
95         ao_log_mini(&log);
96 #endif
97
98         /* Write the whole contents of the ring to the log
99          * when starting up.
100          */
101         ao_log_data_pos = ao_data_ring_next(ao_data_head);
102         next_sensor = ao_data_ring[ao_log_data_pos].tick;
103         ao_log_state = ao_flight_startup;
104         for (;;) {
105                 /* Write samples to EEPROM */
106                 while (ao_log_data_pos != ao_data_head) {
107                         log.tick = ao_data_ring[ao_log_data_pos].tick;
108                         if ((int16_t) (log.tick - next_sensor) >= 0) {
109                                 log.type = AO_LOG_SENSOR;
110                                 ao_log_pack24(log.u.sensor.pres,
111                                               ao_data_ring[ao_log_data_pos].ms5607_raw.pres);
112                                 ao_log_pack24(log.u.sensor.temp,
113                                               ao_data_ring[ao_log_data_pos].ms5607_raw.temp);
114                                 log.u.sensor.sense_a = ao_data_ring[ao_log_data_pos].adc.sense_a;
115                                 log.u.sensor.sense_m = ao_data_ring[ao_log_data_pos].adc.sense_m;
116                                 log.u.sensor.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
117                                 ao_log_mini(&log);
118                                 if (ao_log_state <= ao_flight_coast)
119                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
120                                 else
121                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
122                         }
123                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
124                 }
125 #if HAS_FLIGHT
126                 /* Write state change to EEPROM */
127                 if (ao_flight_state != ao_log_state) {
128                         ao_log_state = ao_flight_state;
129                         log.type = AO_LOG_STATE;
130                         log.tick = ao_time();
131                         log.u.state.state = ao_log_state;
132                         log.u.state.reason = 0;
133                         ao_log_mini(&log);
134
135                         if (ao_log_state == ao_flight_landed)
136                                 ao_log_stop();
137                 }
138 #endif
139
140                 ao_log_flush();
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_mini)))
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 }