altoslib: Save eeprom data in new .eeprom format
[fw/altos] / altoslib / AltosEepromRecordMetrum.java
1 /*
2  * Copyright © 2017 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 public class AltosEepromRecordMetrum extends AltosEepromRecord {
22         public static final int record_length = 16;
23
24         public int record_length() { return record_length; }
25
26         /* AO_LOG_FLIGHT elements */
27         public int flight() { return data16(0); }
28         public int ground_accel() { return data16(2); }
29         public int ground_pres() { return data32(4); }
30         public int ground_temp() { return data32(8); }
31
32         /* AO_LOG_STATE elements */
33         public int state() { return data16(0); }
34         public int reason() { return data16(2); }
35
36         /* AO_LOG_SENSOR elements */
37         public int pres() { return data32(0); }
38         public int temp() { return data32(4); }
39         public int accel() { return data16(8); }
40
41         /* AO_LOG_TEMP_VOLT elements */
42         public int v_batt() { return data16(0); }
43         public int sense_a() { return data16(2); }
44         public int sense_m() { return data16(4); }
45
46         /* AO_LOG_GPS_POS elements */
47         public int latitude() { return data32(0); }
48         public int longitude() { return data32(4); }
49         public int altitude_low() { return data16(8); }
50         public int altitude_high() { return data16(10); }
51
52         /* AO_LOG_GPS_TIME elements */
53         public int hour() { return data8(0); }
54         public int minute() { return data8(1); }
55         public int second() { return data8(2); }
56         public int flags() { return data8(3); }
57         public int year() { return data8(4); }
58         public int month() { return data8(5); }
59         public int day() { return data8(6); }
60         public int pdop() { return data8(7); }
61
62         /* AO_LOG_GPS_SAT elements */
63         public int nsat() { return data8(0); }
64         public int more() { return data8(1); }
65         public int svid(int n) { return data8(2 + n * 2); }
66         public int c_n(int n) { return data8(2 + n * 2 + 1); }
67
68         public void update_state(AltosState state) {
69                 super.update_state(state);
70
71                 AltosGPS        gps;
72
73                 /* Flush any pending GPS changes */
74                 if (state.gps_pending) {
75                         switch (cmd()) {
76                         case AltosLib.AO_LOG_GPS_POS:
77                         case AltosLib.AO_LOG_GPS_LAT:
78                         case AltosLib.AO_LOG_GPS_LON:
79                         case AltosLib.AO_LOG_GPS_ALT:
80                         case AltosLib.AO_LOG_GPS_SAT:
81                         case AltosLib.AO_LOG_GPS_DATE:
82                                 break;
83                         default:
84                                 state.set_temp_gps();
85                                 break;
86                         }
87                 }
88
89                 switch (cmd()) {
90                 case AltosLib.AO_LOG_FLIGHT:
91                         state.set_flight(flight());
92                         state.set_ground_accel(ground_accel());
93                         state.set_ground_pressure(ground_pres());
94                         break;
95                 case AltosLib.AO_LOG_STATE:
96                         state.set_state(state());
97                         break;
98                 case AltosLib.AO_LOG_SENSOR:
99                         state.set_ms5607(pres(), temp());
100                         state.set_accel(accel());
101
102                         break;
103                 case AltosLib.AO_LOG_TEMP_VOLT:
104                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
105
106                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense_a()));
107                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense_m()));
108
109                         break;
110                 case AltosLib.AO_LOG_GPS_POS:
111                         gps = state.make_temp_gps(false);
112                         gps.lat = latitude() / 1e7;
113                         gps.lon = longitude() / 1e7;
114                         if (state.altitude_32())
115                                 gps.alt = (altitude_low() & 0xffff) | (altitude_high() << 16);
116                         else
117                                 gps.alt = altitude_low();
118                         break;
119                 case AltosLib.AO_LOG_GPS_TIME:
120                         gps = state.make_temp_gps(false);
121
122                         gps.hour = hour();
123                         gps.minute = minute();
124                         gps.second = second();
125
126                         int flags = flags();
127
128                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
129                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
130                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
131                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
132
133                         gps.year = 2000 + year();
134                         gps.month = month();
135                         gps.day = day();
136                         gps.pdop = pdop() / 10.0;
137                         break;
138                 case AltosLib.AO_LOG_GPS_SAT:
139                         gps = state.make_temp_gps(true);
140
141                         int n = nsat();
142                         for (int i = 0; i < n; i++)
143                                 gps.add_sat(svid(i), c_n(i));
144                         break;
145                 }
146         }
147
148         public AltosEepromRecord next() {
149                 int     s = next_start();
150                 if (s < 0)
151                         return null;
152                 return new AltosEepromRecordMetrum(eeprom, s);
153         }
154
155         public AltosEepromRecordMetrum(AltosEepromNew eeprom, int start) {
156                 super(eeprom, start, record_length);
157         }
158
159         public AltosEepromRecordMetrum(AltosEepromNew eeprom) {
160                 this(eeprom, 0);
161         }
162 }