Switch from GPLv2 to GPLv2+
[fw/altos] / src / kernel / ao_log_telem.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_flight.h>
21 #include <ao_sample.h>
22
23 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMETRY;
24
25 static __data uint8_t                   ao_log_monitor_pos;
26 __pdata enum ao_flight_state            ao_flight_state;
27 __xdata int16_t                         ao_max_height;  /* max of ao_height */
28 __pdata int16_t                         sense_d, sense_m;
29 __pdata uint8_t                         ao_igniter_present;
30
31 static void
32 ao_log_telem_track() {
33         if (ao_monitoring == sizeof (union ao_telemetry_all)) {
34                 switch (ao_log_single_write_data.telemetry.generic.type) {
35                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
36                 case AO_TELEMETRY_SENSOR_TELEMINI:
37                         /* fall through ... */
38                 case AO_TELEMETRY_SENSOR_TELENANO:
39                         if (ao_log_single_write_data.telemetry.generic.type == AO_TELEMETRY_SENSOR_TELENANO) {
40                                 ao_igniter_present = 0;
41                         } else {
42                                 sense_d = ao_log_single_write_data.telemetry.sensor.sense_d;
43                                 sense_m = ao_log_single_write_data.telemetry.sensor.sense_m;
44                                 ao_igniter_present = 1;
45                         }
46                         if (ao_log_single_write_data.telemetry.sensor.height > ao_max_height) {
47                                 ao_max_height = ao_log_single_write_data.telemetry.sensor.height;
48                         }
49                         if (ao_log_single_write_data.telemetry.sensor.state != ao_flight_state) {
50                                 ao_flight_state = ao_log_single_write_data.telemetry.sensor.state;
51                                 if (ao_flight_state == ao_flight_pad)
52                                         ao_max_height = 0;
53                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
54                         }
55                 }
56         }
57 }
58
59 enum ao_igniter_status
60 ao_igniter_status(enum ao_igniter igniter)
61 {
62         int16_t value;
63
64         switch (igniter) {
65         case ao_igniter_drogue:
66                 value = sense_d;
67                 break;
68         case ao_igniter_main:
69                 value = sense_m;
70                 break;
71         default:
72                 value = 0;
73                 break;
74         }
75         if (value < AO_IGNITER_OPEN)
76                 return ao_igniter_open;
77         else if (value > AO_IGNITER_CLOSED)
78                 return ao_igniter_ready;
79         else
80                 return ao_igniter_unknown;
81 }
82
83 void
84 ao_log_single(void)
85 {
86         ao_storage_setup();
87
88         /* This can take a while, so let the rest
89          * of the system finish booting before we start
90          */
91         ao_delay(AO_SEC_TO_TICKS(2));
92
93         ao_log_running = 1;
94         ao_log_single_restart();
95         ao_flight_state = ao_flight_startup;
96         ao_monitor_set(sizeof(struct ao_telemetry_generic));
97
98         for (;;) {
99                 while (!ao_log_running)
100                         ao_sleep(&ao_log_running);
101
102                 ao_log_monitor_pos = ao_monitor_head;
103                 while (ao_log_running) {
104                         /* Write samples to EEPROM */
105                         while (ao_log_monitor_pos != ao_monitor_head) {
106                                 ao_xmemcpy(&ao_log_single_write_data.telemetry,
107                                            &ao_monitor_ring[ao_log_monitor_pos],
108                                            AO_LOG_SINGLE_SIZE);
109                                 ao_log_single_write();
110                                 ao_log_monitor_pos = ao_monitor_ring_next(ao_log_monitor_pos);
111                                 ao_log_telem_track();
112                         }
113                         /* Wait for more telemetry data to arrive */
114                         ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
115                 }
116         }
117 }
118
119 void
120 ao_log_single_list(void)
121 {
122         if (ao_log_current_pos != 0)
123                 printf("flight 1 start %x end %x\n",
124                        0,
125                        (uint16_t) ((ao_log_current_pos + 0xff) >> 8));
126         printf ("done\n");
127 }
128
129 void
130 ao_log_single_extra_query(void)
131 {
132 }