2 * Copyright © 2017 Bdale Garbee <bdale@gag.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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
22 #include <ao_flight.h>
24 static __xdata struct ao_log_firetwo log;
26 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEFIRETWO;
29 ao_log_csum(__xdata uint8_t *b) __reentrant
34 for (i = 0; i < sizeof (struct ao_log_firetwo); i++)
40 ao_log_firetwo(__xdata struct ao_log_firetwo *log) __reentrant
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)
51 ao_storage_write(ao_log_current_pos,
53 sizeof (struct ao_log_firetwo));
54 ao_log_current_pos += sizeof (struct ao_log_firetwo);
56 } ao_mutex_put(&ao_log_mutex);
61 ao_log_dump_check_data(void)
63 if (ao_log_csum((uint8_t *) &log) != 0)
69 static __data uint8_t ao_log_data_pos;
71 /* a hack to make sure that ao_log_metrums fill the eeprom block in even units */
72 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_firetwo))] ;
78 uint16_t ao_flight_state = ao_flight_startup;
85 while (!ao_log_running)
86 ao_sleep(&ao_log_running);
88 log.type = AO_LOG_FLIGHT;
90 log.u.flight.flight = ao_flight_number;
93 /* Write the whole contents of the ring to the log
96 ao_log_data_pos = ao_data_ring_next(ao_data_head);
97 ao_log_state = ao_flight_startup;
99 /* Write samples to EEPROM */
100 while (ao_log_data_pos != ao_data_head) {
101 log.tick = ao_data_ring[ao_log_data_pos].tick;
102 log.type = AO_LOG_SENSOR;
103 log.u.sensor.pressure = ao_data_ring[ao_log_data_pos].adc.pressure;
104 log.u.sensor.thrust = ao_data_ring[ao_log_data_pos].adc.thrust;
105 // for (i = 0; i < 4; i++) {
106 // log.u.sensor.thermistor[i] = ao_data_ring[ao_log_data_pos].sensor.thermistor[i];
108 ao_log_firetwo(&log);
109 ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
111 /* Write state change to EEPROM */
112 if (ao_flight_state != ao_log_state) {
113 ao_log_state = ao_flight_state;
114 log.type = AO_LOG_STATE;
115 log.tick = ao_time();
116 log.u.state.state = ao_log_state;
117 log.u.state.reason = 0;
118 ao_log_firetwo(&log);
120 if (ao_log_state == ao_flight_landed)
126 if (!ao_log_running) break;
128 /* Wait for a while */
129 ao_delay(AO_MS_TO_TICKS(100));
131 } while (ao_log_running);
135 ao_log_flight(uint8_t slot)
137 if (!ao_storage_read(ao_log_pos(slot),
139 sizeof (struct ao_log_firetwo)))
142 if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
143 return log.u.flight.flight;