Airfest 2013: Bdale's new airframe on Loki M with TMv1 and Tmega
[fw/tmflights] / gps_parse
1 typedef struct {
2         int     type;
3         int     time;
4         int     a;
5         int     b;
6 } flight_record;
7
8 flight_record
9 read_record(file in) {
10         flight_record   r;
11         File::fscanf(in, "%c %x %x %x\n",
12                &r.type, &r.time, &r.a, &r.b);
13         return r;
14 }
15
16 typedef struct {
17         int     degrees;
18         int     minutes;
19         int     minutes_fraction;
20 } gps_pos;
21
22 typedef struct {
23         int     hour;
24         int     minute;
25         int     second;
26 } gps_time;
27
28 real combine_gps_pos(gps_pos p, bool flag)
29 {
30         real sign = flag ? -1 : 1;
31         return sign * (p.degrees + (p.minutes +
32                                     p.minutes_fraction / 10000)
33                        / 60);
34 }
35
36 void read_gps(file in)
37 {
38         gps_time        time;
39         gps_pos         lat, lon;
40         int             flags;
41         int             alt;
42
43         while (!File::end(in)) {
44                 flight_record r = read_record(in);
45                 switch (r.type) {
46                 case 'G':
47                         time.hour = r.a & 0xff;
48                         time.minute = r.a >> 8;
49                         time.second = r.b & 0xff;
50                         flags = r.b >> 8;
51                         break;
52                 case 'N':
53                         lat.degrees = r.a & 0xff;
54                         lat.minutes = r.a >> 8;
55                         lat.minutes_fraction = r.b;
56                         break;
57                 case 'W':
58                         lon.degrees = r.a & 0xff;
59                         lon.minutes = r.a >> 8;
60                         lon.minutes_fraction = r.b;
61                         break;
62                 case 'H':
63                         alt = r.a;
64                         printf ("%5d %d sat ", r.time, flags & 0xf);
65                         if ((flags & (1 << 4)) != 0) {
66                                 printf ("%02d:%02d:%02d ",
67                                         time.hour, time.minute, time.second);
68                                 printf ("%4d°%02d.%04d'%c %2d°%02d.%04d'%c %5dm ",
69                                         lat.degrees, lat.minutes,
70                                         lat.minutes_fraction,
71                                         (flags & (1 << 6)) != 0 ? 'S' : 'N',
72                                         lon.degrees, lon.minutes,
73                                         lon.minutes_fraction,
74                                         (flags & (1 << 5)) != 0 ? 'W' : 'E',
75                                         alt);
76                                 printf ("%11.6f %11.6f %5d\n",
77                                         combine_gps_pos(lat,
78                                                         (flags & (1 << 6)) !=
79                                                         0),
80                                         combine_gps_pos(lon,
81                                                         (flags & (1 << 5)) !=
82                                                         0),
83                                         alt);
84                         } else {
85                                 printf ("unlocked\n");
86                         }
87                         break;
88                 }
89         }
90 }
91
92 read_gps(stdin);