altos: Add tiny logging for TeleMini/TeleNano
[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 static __data uint32_t  ao_log_tiny_pos;
22
23 #define AO_LOG_TINY_INTERVAL_ASCENT     AO_MS_TO_TICKS(100)
24 #define AO_LOG_TINY_INTERVAL_DEFAULT    AO_MS_TO_TICKS(1000)
25
26 void
27 ao_log_tiny_set_interval(uint16_t ticks)
28 {
29         ao_log_tiny_interval = ticks;
30 }
31
32 static __xdata uint16_t ao_log_tiny_data_temp;
33
34 #define ao_log_tiny_data(d) do { \
35                 ao_log_tiny_data_temp = (d);                                    \
36                 ao_storage_write(ao_log_tiny_pos, &ao_log_tiny_data_temp, 2);   \
37                 ao_log_tiny_pos += 2;                                           \
38         } while (0)
39
40 void
41 ao_log(void)
42 {
43         uint16_t                time;
44         int16_t                 delay;
45         enum ao_flight_state    ao_log_tiny_state;
46
47         ao_storage_setup();
48
49         ao_log_tiny_state = ao_flight_invalid;
50         ao_log_tiny_interval = AO_LOG_TINY_INTERVAL_DEFAULT;
51         while (!ao_log_running)
52                 ao_sleep(&ao_log_running);
53
54         time = ao_time();
55         ao_log_tiny_data(ao_flight_number);
56         for (;;) {
57                 if (ao_flight_state != ao_log_tiny_state) {
58                         ao_log_tiny_data(ao_flight_state | 0x8000);
59                         ao_log_tiny_state = ao_flight_state;
60                         ao_log_tiny_interval = AO_LOG_TINY_INTERVAL_DEFAULT;
61                         if (ao_log_tiny_state <= ao_flight_coast)
62                                 ao_log_tiny_interval = AO_LOG_TINY_INTERVAL_ASCENT;
63                 }
64                 ao_log_tiny_data(ao_flight_pres);       // XXX change to alt
65                 time += ao_log_tiny_interval;
66                 delay = time - ao_time();
67                 if (delay > 0)
68                         ao_delay(delay);
69         }
70 }
71
72 uint16_t
73 ao_log_flight(uint8_t slot)
74 {
75         static __xdata uint16_t flight;
76
77         (void) slot;
78         ao_storage_read(0, &flight, 2);
79         if (flight == 0xffff)
80                 flight = 0;
81         return flight;
82 }