6c2468fc3ea7e6f56b6cb5d89bc118bdb928b8f8
[fw/altos] / src / ao_log_tiny.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; 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
20 static __data uint16_t  ao_log_tiny_interval;
21
22 #define AO_LOG_TINY_INTERVAL_ASCENT     AO_MS_TO_TICKS(100)
23 #define AO_LOG_TINY_INTERVAL_DEFAULT    AO_MS_TO_TICKS(1000)
24
25 void
26 ao_log_tiny_set_interval(uint16_t ticks)
27 {
28         ao_log_tiny_interval = ticks;
29 }
30
31 static __xdata uint16_t ao_log_tiny_data_temp;
32
33 static void ao_log_tiny_data(uint16_t d)
34 {
35         if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
36                 ao_log_stop();
37         if (ao_log_running) {
38                 ao_log_tiny_data_temp = (d);
39                 ao_storage_write(ao_log_current_pos, &ao_log_tiny_data_temp, 2);
40                 ao_log_current_pos += 2;
41         }
42 }
43
44 void
45 ao_log(void)
46 {
47         uint16_t                last_time;
48         uint16_t                now;
49         enum ao_flight_state    ao_log_tiny_state;
50         int32_t                 sum;
51         int16_t                 count;
52         uint8_t                 ao_log_adc;
53
54         ao_storage_setup();
55
56         ao_log_scan();
57
58         ao_log_tiny_state = ao_flight_invalid;
59         ao_log_tiny_interval = AO_LOG_TINY_INTERVAL_DEFAULT;
60         while (!ao_log_running)
61                 ao_sleep(&ao_log_running);
62
63         ao_log_tiny_data(ao_flight_number);
64         ao_log_tiny_data(ao_ground_pres);
65         sum = 0;
66         count = 0;
67         ao_log_adc = ao_sample_adc;
68         last_time = ao_time();
69         for (;;) {
70                 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
71                 while (ao_log_adc != ao_sample_adc) {
72                         sum += ao_adc_ring[ao_log_adc].pres;
73                         count++;
74                         ao_log_adc = ao_adc_ring_next(ao_log_adc);
75                 }
76                 if (ao_flight_state != ao_log_tiny_state) {
77                         ao_log_tiny_data(ao_flight_state | 0x8000);
78                         ao_log_tiny_state = ao_flight_state;
79                         ao_log_tiny_interval = AO_LOG_TINY_INTERVAL_DEFAULT;
80                         if (ao_log_tiny_state <= ao_flight_coast)
81                                 ao_log_tiny_interval = AO_LOG_TINY_INTERVAL_ASCENT;
82                         if (ao_log_tiny_state == ao_flight_landed)
83                                 ao_log_stop();
84                 }
85                 /* Stop logging when told to */
86                 if (!ao_log_running)
87                         ao_exit();
88                 now = ao_time();
89                 if ((int16_t) (now - (last_time + ao_log_tiny_interval)) >= 0 && count) {
90                         ao_log_tiny_data(sum / count);
91                         sum = 0;
92                         count = 0;
93                         last_time = now;
94                 }
95         }
96 }
97
98 uint16_t
99 ao_log_flight(uint8_t slot)
100 {
101         static __xdata uint16_t flight;
102
103         (void) slot;
104         ao_storage_read(0, &flight, 2);
105         if (flight == 0xffff)
106                 flight = 0;
107         return flight;
108 }