altos: Switch all tick variables to AO_TICK_TYPE/AO_TICK_SIGNED
[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; 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_log.h>
21 #include <ao_log_gps.h>
22 #include <ao_data.h>
23 #include <ao_flight.h>
24 #include <ao_distance.h>
25 #include <ao_tracker.h>
26
27 void
28 ao_log_gps_flight(void)
29 {
30         ao_log_data.type = AO_LOG_FLIGHT;
31         ao_log_data.tick = ao_time();
32         ao_log_data.u.flight.flight = ao_flight_number;
33         ao_log_write(&ao_log_data);
34 }
35
36 void
37 ao_log_gps_data(AO_TICK_TYPE tick, struct ao_telemetry_location *gps_data)
38 {
39         ao_log_data.tick = tick;
40         ao_log_data.type = AO_LOG_GPS_TIME;
41         ao_log_data.u.gps.latitude = gps_data->latitude;
42         ao_log_data.u.gps.longitude = gps_data->longitude;
43         ao_log_data.u.gps.altitude_low = gps_data->altitude_low;
44         ao_log_data.u.gps.altitude_high = gps_data->altitude_high;
45
46         ao_log_data.u.gps.hour = gps_data->hour;
47         ao_log_data.u.gps.minute = gps_data->minute;
48         ao_log_data.u.gps.second = gps_data->second;
49         ao_log_data.u.gps.flags = gps_data->flags;
50         ao_log_data.u.gps.year = gps_data->year;
51         ao_log_data.u.gps.month = gps_data->month;
52         ao_log_data.u.gps.day = gps_data->day;
53         ao_log_data.u.gps.course = gps_data->course;
54         ao_log_data.u.gps.ground_speed = gps_data->ground_speed;
55         ao_log_data.u.gps.climb_rate = gps_data->climb_rate;
56         ao_log_data.u.gps.pdop = gps_data->pdop;
57         ao_log_data.u.gps.hdop = gps_data->hdop;
58         ao_log_data.u.gps.vdop = gps_data->vdop;
59         ao_log_data.u.gps.mode = gps_data->mode;
60         ao_log_write(&ao_log_data);
61 }
62
63 void
64 ao_log_gps_tracking(AO_TICK_TYPE tick, struct ao_telemetry_satellite *gps_tracking_data)
65 {
66         uint8_t c, n, i;
67
68         ao_log_data.tick = tick;
69         ao_log_data.type = AO_LOG_GPS_SAT;
70         i = 0;
71         n = gps_tracking_data->channels;
72         for (c = 0; c < n; c++)
73                 if ((ao_log_data.u.gps_sat.sats[i].svid = gps_tracking_data->sats[c].svid))
74                 {
75                         ao_log_data.u.gps_sat.sats[i].c_n = gps_tracking_data->sats[c].c_n_1;
76                         i++;
77                         if (i >= 12)
78                                 break;
79                 }
80         ao_log_data.u.gps_sat.channels = i;
81         ao_log_write(&ao_log_data);
82 }
83
84 static uint8_t
85 ao_log_check_empty(void)
86 {
87         uint8_t *b = (void *) &ao_log_data;
88         unsigned i;
89
90         for (i = 0; i < sizeof (ao_log_type); i++)
91                 if (*b++ != AO_STORAGE_ERASED_BYTE)
92                         return 0;
93         return 1;
94 }
95
96 int8_t
97 ao_log_check(uint32_t pos)
98 {
99         if (!ao_storage_read(pos,
100                              &ao_log_data,
101                              sizeof (struct ao_log_gps)))
102                 return AO_LOG_INVALID;
103
104         if (ao_log_check_empty())
105                 return AO_LOG_EMPTY;
106
107         if (!ao_log_check_data())
108                 return AO_LOG_INVALID;
109
110         return AO_LOG_VALID;
111 }