0fdfa5e73c2473c7a6184762e85934d3296273f3
[fw/altos] / altoslib / AltosEepromRecordFull.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
15 package org.altusmetrum.altoslib_11;
16
17 public class AltosEepromRecordFull extends AltosEepromRecord {
18         public static final int record_length = 8;
19
20         public static final int max_sat = 12;
21
22         public static final int two_g_default = 16294 - 15758;
23
24         public void update_state(AltosFlightListener state) {
25
26                 super.update_state(state);
27                 AltosGPS        gps;
28
29                 /* Flush any pending GPS changes */
30                 if (state.gps_pending()) {
31                         switch (cmd()) {
32                         case AltosLib.AO_LOG_GPS_LAT:
33                         case AltosLib.AO_LOG_GPS_LON:
34                         case AltosLib.AO_LOG_GPS_ALT:
35                         case AltosLib.AO_LOG_GPS_SAT:
36                         case AltosLib.AO_LOG_GPS_DATE:
37                                 break;
38                         default:
39                                 state.set_temp_gps();
40                                 break;
41                         }
42                 }
43
44                 switch (cmd()) {
45                 case AltosLib.AO_LOG_FLIGHT:
46                         state.set_state(AltosLib.ao_flight_pad);
47                         state.set_ground_accel(data16(0));
48                         state.set_flight(data16(2));
49                         if (state.accel_plus_g == AltosLib.MISSING)
50                                 state.set_accel_g(data16(0), data16(0) + two_g_default);
51                         break;
52                 case AltosLib.AO_LOG_SENSOR:
53                         state.set_accel(data16(0));
54                         state.set_pressure(AltosConvert.barometer_to_pressure(data16(2)));
55                         break;
56                 case AltosLib.AO_LOG_PRESSURE:
57                         state.set_pressure(AltosConvert.barometer_to_pressure(data16(2)));
58                         break;
59                 case AltosLib.AO_LOG_TEMP_VOLT:
60                         state.set_temperature(AltosConvert.thermometer_to_temperature(data16(0)));
61                         state.set_battery_voltage(AltosConvert.cc_battery_to_voltage(data16(2)));
62                         break;
63                 case AltosLib.AO_LOG_DEPLOY:
64                         state.set_apogee_voltage(AltosConvert.cc_ignitor_to_voltage(data16(0)));
65                         state.set_main_voltage(AltosConvert.cc_ignitor_to_voltage(data16(2)));
66                         break;
67                 case AltosLib.AO_LOG_STATE:
68                         state.set_state(data16(0));
69                         break;
70                 case AltosLib.AO_LOG_GPS_TIME:
71                         gps = state.make_temp_gps(false);
72
73                         gps.hour = data8(0);
74                         gps.minute = data8(1);
75                         gps.second = data8(2);
76
77                         int flags = data8(3);
78
79                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
80                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
81                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
82                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
83                         break;
84                 case AltosLib.AO_LOG_GPS_LAT:
85                         gps = state.make_temp_gps(false);
86
87                         int lat32 = data32(0);
88                         gps.lat = (double) lat32 / 1e7;
89                         break;
90                 case AltosLib.AO_LOG_GPS_LON:
91                         gps = state.make_temp_gps(false);
92
93                         int lon32 = data32(0);
94                         gps.lon = (double) lon32 / 1e7;
95                         break;
96                 case AltosLib.AO_LOG_GPS_ALT:
97                         gps = state.make_temp_gps(false);
98                         gps.alt = data16(0);
99                         break;
100                 case AltosLib.AO_LOG_GPS_SAT:
101                         gps = state.make_temp_gps(true);
102                         int svid = data16(0);
103                         int c_n0 = data16(3);
104                         gps.add_sat(svid, c_n0);
105                         break;
106                 case AltosLib.AO_LOG_GPS_DATE:
107                         gps = state.make_temp_gps(false);
108                         gps.year = data8(0) + 2000;
109                         gps.month = data8(1);
110                         gps.day = data8(2);
111                         break;
112                 }
113         }
114
115         public AltosEepromRecord next() {
116                 int     s = next_start();
117                 if (s < 0)
118                         return null;
119                 return new AltosEepromRecordFull(eeprom, s);
120         }
121
122         public AltosEepromRecordFull(AltosEepromNew eeprom, int start) {
123                 super(eeprom, start, record_length);
124         }
125
126         public AltosEepromRecordFull(AltosEepromNew eeprom) {
127                 this(eeprom, 0);
128         }
129 }