altos: Simplify tracker logic, removing boost detect
[fw/altos] / src / kernel / ao_log_gps.c
1 /*
2  * Copyright © 2014 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 #include <ao_log.h>
20 #include <ao_log_gps.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23 #include <ao_distance.h>
24 #include <ao_tracker.h>
25
26 static __xdata uint8_t  ao_log_mutex;
27 static __xdata struct ao_log_gps log;
28
29 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEGPS;
30
31 static uint8_t
32 ao_log_csum(__xdata uint8_t *b) __reentrant
33 {
34         uint8_t sum = 0x5a;
35         uint8_t i;
36
37         for (i = 0; i < sizeof (struct ao_log_gps); i++)
38                 sum += *b++;
39         return -sum;
40 }
41
42 uint8_t
43 ao_log_gps(__xdata struct ao_log_gps *log) __reentrant
44 {
45         uint8_t wrote = 0;
46         /* set checksum */
47         log->csum = 0;
48         log->csum = ao_log_csum((__xdata uint8_t *) log);
49         ao_mutex_get(&ao_log_mutex); {
50                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
51                         ao_log_stop();
52                 if (ao_log_running) {
53                         wrote = 1;
54                         ao_storage_write(ao_log_current_pos,
55                                          log,
56                                          sizeof (struct ao_log_gps));
57                         ao_log_current_pos += sizeof (struct ao_log_gps);
58                 }
59         } ao_mutex_put(&ao_log_mutex);
60         return wrote;
61 }
62
63 void
64 ao_log_gps_flight(void)
65 {
66         log.type = AO_LOG_FLIGHT;
67         log.tick = ao_time();
68         log.u.flight.flight = ao_flight_number;
69         ao_log_gps(&log);
70 }
71
72 void
73 ao_log_gps_data(uint16_t tick, struct ao_telemetry_location *gps_data)
74 {
75         log.tick = tick;
76         log.type = AO_LOG_GPS_TIME;
77         log.u.gps.latitude = gps_data->latitude;
78         log.u.gps.longitude = gps_data->longitude;
79         log.u.gps.altitude = gps_data->altitude;
80
81         log.u.gps.hour = gps_data->hour;
82         log.u.gps.minute = gps_data->minute;
83         log.u.gps.second = gps_data->second;
84         log.u.gps.flags = gps_data->flags;
85         log.u.gps.year = gps_data->year;
86         log.u.gps.month = gps_data->month;
87         log.u.gps.day = gps_data->day;
88         log.u.gps.course = gps_data->course;
89         log.u.gps.ground_speed = gps_data->ground_speed;
90         log.u.gps.climb_rate = gps_data->climb_rate;
91         log.u.gps.pdop = gps_data->pdop;
92         log.u.gps.hdop = gps_data->hdop;
93         log.u.gps.vdop = gps_data->vdop;
94         log.u.gps.mode = gps_data->mode;
95         ao_log_gps(&log);
96 }
97
98 void
99 ao_log_gps_tracking(uint16_t tick, struct ao_telemetry_satellite *gps_tracking_data)
100 {
101         uint8_t c, n, i;
102
103         log.tick = tick;
104         log.type = AO_LOG_GPS_SAT;
105         i = 0;
106         n = gps_tracking_data->channels;
107         for (c = 0; c < n; c++)
108                 if ((log.u.gps_sat.sats[i].svid = gps_tracking_data->sats[c].svid))
109                 {
110                         log.u.gps_sat.sats[i].c_n = gps_tracking_data->sats[c].c_n_1;
111                         i++;
112                         if (i >= 12)
113                                 break;
114                 }
115         log.u.gps_sat.channels = i;
116         ao_log_gps(&log);
117 }
118
119 static uint8_t
120 ao_log_dump_check_data(void)
121 {
122         if (ao_log_csum((uint8_t *) &log) != 0)
123                 return 0;
124         return 1;
125 }
126
127 uint16_t
128 ao_log_flight(uint8_t slot)
129 {
130         if (!ao_storage_read(ao_log_pos(slot),
131                              &log,
132                              sizeof (struct ao_log_gps)))
133                 return 0;
134
135         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
136                 return log.u.flight.flight;
137         return 0;
138 }