2f43879ec978a902a6adaf82f4ce42210a38c32f
[fw/altos] / altoslib / AltosEepromMetrum2.java
1 /*
2  * Copyright © 2011 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_5;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromMetrum2 extends AltosEeprom {
25         public static final int record_length = 16;
26
27         public int record_length() { return record_length; }
28
29         /* AO_LOG_FLIGHT elements */
30         public int flight() { return data16(0); }
31         public int ground_accel() { return data16(2); }
32         public int ground_pres() { return data32(4); }
33         public int ground_temp() { return data32(8); }
34
35         /* AO_LOG_STATE elements */
36         public int state() { return data16(0); }
37         public int reason() { return data16(2); }
38
39         /* AO_LOG_SENSOR elements */
40         public int pres() { return data32(0); }
41         public int temp() { return data32(4); }
42         public int accel() { return data16(8); }
43
44         /* AO_LOG_TEMP_VOLT elements */
45         public int v_batt() { return data16(0); }
46         public int sense_a() { return data16(2); }
47         public int sense_m() { return data16(4); }
48
49         /* AO_LOG_GPS_POS elements */
50         public int latitude() { return data32(0); }
51         public int longitude() { return data32(4); }
52         public int altitude() { return data16(8); }
53
54         /* AO_LOG_GPS_TIME elements */
55         public int hour() { return data8(0); }
56         public int minute() { return data8(1); }
57         public int second() { return data8(2); }
58         public int flags() { return data8(3); }
59         public int year() { return data8(4); }
60         public int month() { return data8(5); }
61         public int day() { return data8(6); }
62         public int pdop() { return data8(7); }
63
64         /* AO_LOG_GPS_SAT elements */
65         public int nsat() { return data8(0); }
66         public int more() { return data8(1); }
67         public int svid(int n) { return data8(2 + n * 2); }
68         public int c_n(int n) { return data8(2 + n * 2 + 1); }
69
70         public AltosEepromMetrum2 (AltosEepromChunk chunk, int start) throws ParseException {
71                 parse_chunk(chunk, start);
72         }
73
74         public void update_state(AltosState state) {
75                 super.update_state(state);
76
77                 AltosGPS        gps;
78
79                 /* Flush any pending GPS changes */
80                 if (state.gps_pending) {
81                         switch (cmd) {
82                         case AltosLib.AO_LOG_GPS_POS:
83                         case AltosLib.AO_LOG_GPS_LAT:
84                         case AltosLib.AO_LOG_GPS_LON:
85                         case AltosLib.AO_LOG_GPS_ALT:
86                         case AltosLib.AO_LOG_GPS_SAT:
87                         case AltosLib.AO_LOG_GPS_DATE:
88                                 break;
89                         default:
90                                 state.set_temp_gps();
91                                 break;
92                         }
93                 }
94
95                 switch (cmd) {
96                 case AltosLib.AO_LOG_FLIGHT:
97                         state.set_flight(flight());
98                         state.set_ground_accel(ground_accel());
99                         state.set_ground_pressure(ground_pres());
100 //                      state.set_temperature(ground_temp() / 100.0);
101                         break;
102                 case AltosLib.AO_LOG_STATE:
103                         state.set_state(state());
104                         break;
105                 case AltosLib.AO_LOG_SENSOR:
106                         state.set_ms5607(pres(), temp());
107                         state.set_accel(accel());
108
109                         break;
110                 case AltosLib.AO_LOG_TEMP_VOLT:
111                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
112
113                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense_a()));
114                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense_m()));
115
116                         break;
117                 case AltosLib.AO_LOG_GPS_POS:
118                         gps = state.make_temp_gps(false);
119                         gps.lat = latitude() / 1e7;
120                         gps.lon = longitude() / 1e7;
121                         gps.alt = altitude();
122                         break;
123                 case AltosLib.AO_LOG_GPS_TIME:
124                         gps = state.make_temp_gps(false);
125
126                         gps.hour = hour();
127                         gps.minute = minute();
128                         gps.second = second();
129
130                         int flags = flags();
131
132                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
133                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
134                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
135                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
136
137                         gps.year = 2000 + year();
138                         gps.month = month();
139                         gps.day = day();
140                         gps.pdop = pdop() / 10.0;
141                         break;
142                 case AltosLib.AO_LOG_GPS_SAT:
143                         gps = state.make_temp_gps(true);
144
145                         int n = nsat();
146                         for (int i = 0; i < n; i++)
147                                 gps.add_sat(svid(i), c_n(i));
148                         break;
149                 }
150         }
151
152         public AltosEepromMetrum2 (String line) {
153                 parse_string(line);
154         }
155
156         static public LinkedList<AltosEeprom> read(FileInputStream input) {
157                 LinkedList<AltosEeprom> metrums = new LinkedList<AltosEeprom>();
158
159                 for (;;) {
160                         try {
161                                 String line = AltosLib.gets(input);
162                                 if (line == null)
163                                         break;
164                                 try {
165                                         AltosEepromMetrum2 metrum = new AltosEepromMetrum2(line);
166
167                                         if (metrum.cmd != AltosLib.AO_LOG_INVALID)
168                                                 metrums.add(metrum);
169                                 } catch (Exception e) {
170                                         System.out.printf ("exception\n");
171                                 }
172                         } catch (IOException ie) {
173                                 break;
174                         }
175                 }
176
177                 return metrums;
178         }
179 }