ea81eb3da5a9f35d27b018e21987d8a2ee7f5e64
[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 provide_data(AltosDataListener listener, AltosCalData cal_data) {
25
26                 super.provide_data(listener, cal_data);
27                 AltosGPS        gps;
28
29                 /* Flush any pending GPS changes */
30                 if (cal_data.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                                 listener.set_gps(cal_data.temp_gps());
40                                 cal_data.reset_temp_gps();
41                                 break;
42                         }
43                 }
44
45                 switch (cmd()) {
46                 case AltosLib.AO_LOG_FLIGHT:
47                         listener.set_state(AltosLib.ao_flight_pad);
48                         cal_data.set_ground_accel(data16(0));
49                         cal_data.set_flight(data16(2));
50                         if (cal_data.accel_plus_g == AltosLib.MISSING)
51                                 cal_data.set_accel_plus_minus(data16(0), data16(0) + two_g_default);
52                         break;
53                 case AltosLib.AO_LOG_SENSOR:
54                         listener.set_acceleration(cal_data.acceleration(data16(0)));
55                         listener.set_pressure(AltosConvert.barometer_to_pressure(data16(2)));
56                         break;
57                 case AltosLib.AO_LOG_PRESSURE:
58                         listener.set_pressure(AltosConvert.barometer_to_pressure(data16(2)));
59                         break;
60                 case AltosLib.AO_LOG_TEMP_VOLT:
61                         listener.set_temperature(AltosConvert.thermometer_to_temperature(data16(0)));
62                         listener.set_battery_voltage(AltosConvert.cc_battery_to_voltage(data16(2)));
63                         break;
64                 case AltosLib.AO_LOG_DEPLOY:
65                         listener.set_apogee_voltage(AltosConvert.cc_ignitor_to_voltage(data16(0)));
66                         listener.set_main_voltage(AltosConvert.cc_ignitor_to_voltage(data16(2)));
67                         break;
68                 case AltosLib.AO_LOG_STATE:
69                         listener.set_state(data16(0));
70                         break;
71                 case AltosLib.AO_LOG_GPS_TIME:
72                         gps = cal_data.make_temp_gps(tick(),false);
73
74                         gps.hour = data8(0);
75                         gps.minute = data8(1);
76                         gps.second = data8(2);
77
78                         int flags = data8(3);
79
80                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
81                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
82                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
83                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
84                         break;
85                 case AltosLib.AO_LOG_GPS_LAT:
86                         gps = cal_data.make_temp_gps(tick(),false);
87
88                         int lat32 = data32(0);
89                         gps.lat = (double) lat32 / 1e7;
90                         break;
91                 case AltosLib.AO_LOG_GPS_LON:
92                         gps = cal_data.make_temp_gps(tick(),false);
93
94                         int lon32 = data32(0);
95                         gps.lon = (double) lon32 / 1e7;
96                         break;
97                 case AltosLib.AO_LOG_GPS_ALT:
98                         gps = cal_data.make_temp_gps(tick(),false);
99                         gps.alt = data16(0);
100                         break;
101                 case AltosLib.AO_LOG_GPS_SAT:
102                         gps = cal_data.make_temp_gps(tick(),true);
103                         int svid = data16(0);
104                         int c_n0 = data16(3);
105                         gps.add_sat(svid, c_n0);
106                         break;
107                 case AltosLib.AO_LOG_GPS_DATE:
108                         gps = cal_data.make_temp_gps(tick(),false);
109                         gps.year = data8(0) + 2000;
110                         gps.month = data8(1);
111                         gps.day = data8(2);
112                         break;
113                 }
114         }
115
116         public AltosEepromRecord next() {
117                 int     s = next_start();
118                 if (s < 0)
119                         return null;
120                 return new AltosEepromRecordFull(eeprom, s);
121         }
122
123         public AltosEepromRecordFull(AltosEepromNew eeprom, int start) {
124                 super(eeprom, start, record_length);
125         }
126
127         public AltosEepromRecordFull(AltosEepromNew eeprom) {
128                 this(eeprom, 0);
129         }
130 }