8cc07c85790e5264d49bb695090cb6c66bb5af71
[fw/altos] / src / ao_gps_print.c
1 /*
2  * Copyright © 2009 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 #ifndef AO_GPS_TEST
19 #include "ao.h"
20 #endif
21
22 struct ao_gps_split {
23         uint8_t positive;
24         uint8_t degrees;
25         uint8_t minutes;
26         uint16_t minutes_fraction;
27 };
28
29 static void
30 ao_gps_split(int32_t v, __xdata struct ao_gps_split *split) __reentrant
31 {
32         uint32_t minutes_e7;
33
34         split->positive = 1;
35         if (v < 0) {
36                 v = -v;
37                 split->positive = 0;
38         }
39         split->degrees = v / 10000000;
40         minutes_e7 = (v % 10000000) * 60;
41         split->minutes = minutes_e7 / 10000000;
42         split->minutes_fraction = (minutes_e7 % 10000000) / 1000;
43 }
44
45 void
46 ao_gps_print(__xdata struct ao_gps_data *gps_data) __reentrant
47 {
48         printf("GPS %2d sat",
49                (gps_data->flags & AO_GPS_NUM_SAT_MASK) >> AO_GPS_NUM_SAT_SHIFT);
50         if (gps_data->flags & AO_GPS_VALID) {
51                 static __xdata struct ao_gps_split      lat, lon;
52                 int16_t climb, climb_int, climb_frac;
53
54                 ao_gps_split(gps_data->latitude, &lat);
55                 ao_gps_split(gps_data->longitude, &lon);
56                 printf(" %2d:%02d:%02d",
57                        gps_data->hour,
58                        gps_data->minute,
59                        gps_data->second);
60                 printf(" %2d°%02d.%04d'%c %2d°%02d.%04d'%c %5dm",
61                        lat.degrees,
62                        lat.minutes,
63                        lat.minutes_fraction,
64                        lat.positive ? 'N' : 'S',
65                        lon.degrees,
66                        lon.minutes,
67                        lon.minutes_fraction,
68                        lon.positive ? 'E' : 'W',
69                        gps_data->altitude);
70                 climb = gps_data->climb_rate;
71                 if (climb >= 0) {
72                         climb_int = climb / 100;
73                         climb_frac = climb % 100;
74                 } else {
75                         climb = -climb;
76                         climb_int = -(climb / 100);
77                         climb_frac = climb % 100;
78                 }
79                 printf(" %5u.%02dm/s(H) %d° %5d.%02dm/s(V)",
80                        gps_data->ground_speed / 100,
81                        gps_data->ground_speed % 100,
82                        gps_data->course * 2,
83                        climb / 100,
84                        climb % 100);
85                 printf(" %d.%d(hdop) %5u(herr) %5u(verr)\n",
86                        gps_data->hdop / 5,
87                        (gps_data->hdop * 2) % 10,
88                        gps_data->h_error,
89                        gps_data->v_error);
90         } else if (gps_data->flags & AO_GPS_RUNNING) {
91                 printf(" unlocked\n");
92         } else {
93                 printf (" not-connected\n");
94         }
95 }