altos/lisp: Fix uninitialized values in ao_lisp_make_const
[fw/altos] / altoslib / AltosEepromGPS.java
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 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24
25 public class AltosEepromGPS extends AltosEeprom {
26         public static final int record_length = 32;
27
28         public static final int max_sat = 12;
29
30         public int record_length() { return record_length; }
31
32         /* AO_LOG_FLIGHT elements */
33         public int flight() { return data16(0); }
34         public int start_altitude() { return data16(2); }
35         public int start_latitude() { return data32(4); }
36         public int start_longitude() { return data32(8); }
37
38         /* AO_LOG_GPS_TIME elements */
39         public int latitude() { return data32(0); }
40         public int longitude() { return data32(4); }
41         public int altitude_low() { return data16(8); }
42         public int hour() { return data8(10); }
43         public int minute() { return data8(11); }
44         public int second() { return data8(12); }
45         public int flags() { return data8(13); }
46         public int year() { return data8(14); }
47         public int month() { return data8(15); }
48         public int day() { return data8(16); }
49         public int course() { return data8(17); }
50         public int ground_speed() { return data16(18); }
51         public int climb_rate() { return data16(20); }
52         public int pdop() { return data8(22); }
53         public int hdop() { return data8(23); }
54         public int vdop() { return data8(24); }
55         public int mode() { return data8(25); }
56         public int altitude_high() { return data16(26); }
57
58         public boolean has_seconds() { return cmd == AltosLib.AO_LOG_GPS_TIME; }
59
60         public int seconds() {
61                 switch (cmd) {
62                 case AltosLib.AO_LOG_GPS_TIME:
63                         return second() + 60 * (minute() + 60 * (hour() + 24 * (day() + 31 * month())));
64                 default:
65                         return 0;
66                 }
67         }
68
69         public AltosEepromGPS (AltosEepromChunk chunk, int start) throws ParseException {
70                 parse_chunk(chunk, start);
71         }
72
73         public void update_state(AltosState state) {
74                 super.update_state(state);
75
76                 AltosGPS        gps;
77
78                 /* Flush any pending GPS changes */
79                 if (state.gps_pending) {
80                         switch (cmd) {
81                         case AltosLib.AO_LOG_GPS_LAT:
82                         case AltosLib.AO_LOG_GPS_LON:
83                         case AltosLib.AO_LOG_GPS_ALT:
84                         case AltosLib.AO_LOG_GPS_SAT:
85                         case AltosLib.AO_LOG_GPS_DATE:
86                                 break;
87                         default:
88                                 state.set_temp_gps();
89                                 break;
90                         }
91                 }
92
93                 switch (cmd) {
94                 case AltosLib.AO_LOG_FLIGHT:
95                         if (state.flight == AltosLib.MISSING) {
96                                 state.set_boost_tick(tick);
97                                 state.set_flight(flight());
98                         }
99                         /* no place to log start lat/lon yet */
100                         break;
101                 case AltosLib.AO_LOG_GPS_TIME:
102                         state.set_tick(tick);
103                         gps = state.make_temp_gps(false);
104                         gps.lat = latitude() / 1e7;
105                         gps.lon = longitude() / 1e7;
106                         if (state.altitude_32())
107                                 gps.alt = (altitude_low() & 0xffff) | (altitude_high() << 16);
108                         else
109                                 gps.alt = altitude_low();
110
111                         gps.hour = hour();
112                         gps.minute = minute();
113                         gps.second = second();
114
115                         int flags = flags();
116
117                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
118                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
119                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
120                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
121
122                         gps.year = 2000 + year();
123                         gps.month = month();
124                         gps.day = day();
125                         gps.ground_speed = ground_speed() * 1.0e-2;
126                         gps.course = course() * 2;
127                         gps.climb_rate = climb_rate() * 1.0e-2;
128                         if (state.compare_version("1.4.9") >= 0) {
129                                 gps.pdop = pdop() / 10.0;
130                                 gps.hdop = hdop() / 10.0;
131                                 gps.vdop = vdop() / 10.0;
132                         } else {
133                                 gps.pdop = pdop() / 100.0;
134                                 if (gps.pdop < 0.8)
135                                         gps.pdop += 2.56;
136                                 gps.hdop = hdop() / 100.0;
137                                 if (gps.hdop < 0.8)
138                                         gps.hdop += 2.56;
139                                 gps.vdop = vdop() / 100.0;
140                                 if (gps.vdop < 0.8)
141                                         gps.vdop += 2.56;
142                         }
143                         break;
144                 }
145         }
146
147         public AltosEepromGPS (String line) {
148                 parse_string(line);
149         }
150
151         static public LinkedList<AltosEeprom> read(FileInputStream input) {
152                 LinkedList<AltosEeprom> tgpss = new LinkedList<AltosEeprom>();
153
154                 for (;;) {
155                         try {
156                                 String line = AltosLib.gets(input);
157                                 if (line == null)
158                                         break;
159                                 try {
160                                         AltosEepromGPS tgps = new AltosEepromGPS(line);
161                                         if (tgps.cmd != AltosLib.AO_LOG_INVALID)
162                                                 tgpss.add(tgps);
163                                 } catch (Exception e) {
164                                         System.out.printf ("exception\n");
165                                 }
166                         } catch (IOException ie) {
167                                 break;
168                         }
169                 }
170
171                 return tgpss;
172         }
173 }