2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 #include <ao_flight.h>
23 static __xdata struct ao_log_mini log;
25 __code uint8_t ao_log_format = AO_LOG_FORMAT;
28 ao_log_csum(__xdata uint8_t *b) __reentrant
33 for (i = 0; i < sizeof (struct ao_log_mini); i++)
39 ao_log_mini(__xdata struct ao_log_mini *log) __reentrant
44 log->csum = ao_log_csum((__xdata uint8_t *) log);
45 ao_mutex_get(&ao_log_mutex); {
46 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
50 ao_storage_write(ao_log_current_pos,
52 sizeof (struct ao_log_mini));
53 ao_log_current_pos += sizeof (struct ao_log_mini);
55 } ao_mutex_put(&ao_log_mutex);
60 ao_log_dump_check_data(void)
62 if (ao_log_csum((uint8_t *) &log) != 0)
67 static __data uint8_t ao_log_data_pos;
69 /* a hack to make sure that ao_log_minis fill the eeprom block in even units */
70 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mini))] ;
72 #ifndef AO_SENSOR_INTERVAL_ASCENT
73 #define AO_SENSOR_INTERVAL_ASCENT 1
74 #define AO_SENSOR_INTERVAL_DESCENT 10
80 __pdata uint16_t next_sensor;
86 while (!ao_log_running)
87 ao_sleep(&ao_log_running);
90 log.type = AO_LOG_FLIGHT;
91 log.tick = ao_sample_tick;
92 log.u.flight.flight = ao_flight_number;
93 log.u.flight.ground_pres = ao_ground_pres;
97 /* Write the whole contents of the ring to the log
100 ao_log_data_pos = ao_data_ring_next(ao_data_head);
101 next_sensor = ao_data_ring[ao_log_data_pos].tick;
102 ao_log_state = ao_flight_startup;
104 /* Write samples to EEPROM */
105 while (ao_log_data_pos != ao_data_head) {
106 log.tick = ao_data_ring[ao_log_data_pos].tick;
107 if ((int16_t) (log.tick - next_sensor) >= 0) {
108 log.type = AO_LOG_SENSOR;
109 ao_log_pack24(log.u.sensor.pres,
110 ao_data_ring[ao_log_data_pos].ms5607_raw.pres);
111 ao_log_pack24(log.u.sensor.temp,
112 ao_data_ring[ao_log_data_pos].ms5607_raw.temp);
113 log.u.sensor.sense_a = ao_data_ring[ao_log_data_pos].adc.sense_a;
114 log.u.sensor.sense_m = ao_data_ring[ao_log_data_pos].adc.sense_m;
115 log.u.sensor.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
117 if (ao_log_state <= ao_flight_coast)
118 next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
120 next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
122 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
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_time();
130 log.u.state.state = ao_log_state;
131 log.u.state.reason = 0;
134 if (ao_log_state == ao_flight_landed)
141 /* Wait for a while */
142 ao_delay(AO_MS_TO_TICKS(100));
144 /* Stop logging when told to */
145 while (!ao_log_running)
146 ao_sleep(&ao_log_running);
151 ao_log_flight(uint8_t slot)
153 if (!ao_storage_read(ao_log_pos(slot),
155 sizeof (struct ao_log_mini)))
158 if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
159 return log.u.flight.flight;