d13aac42863d5b2e8f8bdc718682d65c0ed95aa5
[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_3;
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
63         /* AO_LOG_GPS_SAT elements */
64         public int nsat() { return data8(0); }
65         public int more() { return data8(1); }
66         public int svid(int n) { return data8(2 + n * 2); }
67         public int c_n(int n) { return data8(2 + n * 2 + 1); }
68
69         public AltosEepromMetrum2 (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_POS:
82                         case AltosLib.AO_LOG_GPS_LAT:
83                         case AltosLib.AO_LOG_GPS_LON:
84                         case AltosLib.AO_LOG_GPS_ALT:
85                         case AltosLib.AO_LOG_GPS_SAT:
86                         case AltosLib.AO_LOG_GPS_DATE:
87                                 break;
88                         default:
89                                 state.set_temp_gps();
90                                 break;
91                         }
92                 }
93
94                 switch (cmd) {
95                 case AltosLib.AO_LOG_FLIGHT:
96                         state.set_flight(flight());
97                         state.set_ground_accel(ground_accel());
98                         state.set_ground_pressure(ground_pres());
99 //                      state.set_temperature(ground_temp() / 100.0);
100                         break;
101                 case AltosLib.AO_LOG_STATE:
102                         state.set_state(state());
103                         break;
104                 case AltosLib.AO_LOG_SENSOR:
105                         state.set_ms5607(pres(), temp());
106                         state.set_accel(accel());
107
108                         break;
109                 case AltosLib.AO_LOG_TEMP_VOLT:
110                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
111
112                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense_a()));
113                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense_m()));
114
115                         break;
116                 case AltosLib.AO_LOG_GPS_POS:
117                         gps = state.make_temp_gps(false);
118                         gps.lat = latitude() / 1e7;
119                         gps.lon = longitude() / 1e7;
120                         gps.alt = altitude();
121                         break;
122                 case AltosLib.AO_LOG_GPS_TIME:
123                         gps = state.make_temp_gps(false);
124
125                         gps.hour = hour();
126                         gps.minute = minute();
127                         gps.second = second();
128
129                         int flags = flags();
130
131                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
132                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
133                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
134                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
135
136                         gps.year = 2000 + year();
137                         gps.month = month();
138                         gps.day = day();
139                         break;
140                 case AltosLib.AO_LOG_GPS_SAT:
141                         gps = state.make_temp_gps(true);
142
143                         int n = nsat();
144                         for (int i = 0; i < n; i++)
145                                 gps.add_sat(svid(i), c_n(i));
146                         break;
147                 }
148         }
149
150         public AltosEepromMetrum2 (String line) {
151                 parse_string(line);
152         }
153
154         static public LinkedList<AltosEeprom> read(FileInputStream input) {
155                 LinkedList<AltosEeprom> metrums = new LinkedList<AltosEeprom>();
156
157                 for (;;) {
158                         try {
159                                 String line = AltosLib.gets(input);
160                                 if (line == null)
161                                         break;
162                                 try {
163                                         AltosEepromMetrum2 metrum = new AltosEepromMetrum2(line);
164
165                                         if (metrum.cmd != AltosLib.AO_LOG_INVALID)
166                                                 metrums.add(metrum);
167                                 } catch (Exception e) {
168                                         System.out.printf ("exception\n");
169                                 }
170                         } catch (IOException ie) {
171                                 break;
172                         }
173                 }
174
175                 return metrums;
176         }
177 }