altoslib: Fix gyro headings in CSV files
[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_13;
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 provide_data(AltosDataListener listener, AltosCalData cal_data) {
69                 super.provide_data(listener, cal_data);
70
71                 AltosGPS        gps;
72
73                 switch (cmd()) {
74                 case AltosLib.AO_LOG_FLIGHT:
75                         cal_data.set_flight(flight());
76                         cal_data.set_ground_accel(ground_accel());
77                         cal_data.set_ground_pressure(ground_pres());
78                         break;
79                 case AltosLib.AO_LOG_STATE:
80                         listener.set_state(state());
81                         break;
82                 case AltosLib.AO_LOG_SENSOR:
83                         AltosPresTemp pt = eeprom.config_data().ms5607().pres_temp(pres(), temp());
84                         listener.set_pressure(pt.pres);
85                         listener.set_temperature(pt.temp);
86                         listener.set_acceleration(cal_data.acceleration(accel()));
87                         break;
88                 case AltosLib.AO_LOG_TEMP_VOLT:
89                         listener.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
90                         listener.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense_a()));
91                         listener.set_main_voltage(AltosConvert.mega_pyro_voltage(sense_m()));
92                         break;
93                 case AltosLib.AO_LOG_GPS_POS:
94                         gps = listener.make_temp_gps(false);
95                         gps.lat = latitude() / 1e7;
96                         gps.lon = longitude() / 1e7;
97                         if (config_data().altitude_32())
98                                 gps.alt = (altitude_low() & 0xffff) | (altitude_high() << 16);
99                         else
100                                 gps.alt = altitude_low();
101                         break;
102                 case AltosLib.AO_LOG_GPS_TIME:
103                         gps = listener.make_temp_gps(false);
104
105                         gps.hour = hour();
106                         gps.minute = minute();
107                         gps.second = second();
108
109                         int flags = flags();
110
111                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
112                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
113                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
114                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
115
116                         gps.year = 2000 + year();
117                         gps.month = month();
118                         gps.day = day();
119                         gps.pdop = pdop() / 10.0;
120                         break;
121                 case AltosLib.AO_LOG_GPS_SAT:
122                         gps = listener.make_temp_gps(true);
123
124                         int n = nsat();
125                         for (int i = 0; i < n; i++)
126                                 gps.add_sat(svid(i), c_n(i));
127                         break;
128                 }
129         }
130
131         public AltosEepromRecord next() {
132                 int     s = next_start();
133                 if (s < 0)
134                         return null;
135                 return new AltosEepromRecordMetrum(eeprom, s);
136         }
137
138         public AltosEepromRecordMetrum(AltosEeprom eeprom, int start) {
139                 super(eeprom, start, record_length);
140         }
141
142         public AltosEepromRecordMetrum(AltosEeprom eeprom) {
143                 this(eeprom, 0);
144         }
145 }