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