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