altoslib: Create data file open helper in AltosLib
[fw/altos] / altoslib / AltosEepromRecordMini.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 AltosEepromRecordMini extends AltosEepromRecord {
22         public static final int record_length = 16;
23
24         /* AO_LOG_FLIGHT elements */
25         public int flight() { return data16(0); }
26         public int ground_pres() { return data32(4); }
27
28         /* AO_LOG_STATE elements */
29         public int state() { return data16(0); }
30         public int reason() { return data16(2); }
31
32         /* AO_LOG_SENSOR elements */
33         public int pres() { return data24(0); }
34         public int temp() { return data24(3); }
35         public int sense_a() { return data16(6); }
36         public int sense_m() { return data16(8); }
37         public int v_batt() { return data16(10); }
38
39         private int log_format() {
40                 return eeprom.config_data().log_format;
41         }
42
43         private double battery_voltage(int sensor) {
44                 int log_format = log_format();
45                 if (log_format == AltosLib.AO_LOG_FORMAT_EASYMINI)
46                         return AltosConvert.easy_mini_voltage(sensor, eeprom.config_data().serial);
47                 if (log_format == AltosLib.AO_LOG_FORMAT_TELEMINI2)
48                         return AltosConvert.tele_mini_2_voltage(sensor);
49                 if (log_format == AltosLib.AO_LOG_FORMAT_TELEMINI3)
50                         return AltosConvert.tele_mini_3_battery_voltage(sensor);
51                 return -1;
52         }
53
54         private double pyro_voltage(int sensor) {
55                 int log_format = log_format();
56                 if (log_format == AltosLib.AO_LOG_FORMAT_EASYMINI)
57                         return AltosConvert.easy_mini_voltage(sensor, eeprom.config_data().serial);
58                 if (log_format == AltosLib.AO_LOG_FORMAT_TELEMINI2)
59                         return AltosConvert.tele_mini_2_voltage(sensor);
60                 if (log_format == AltosLib.AO_LOG_FORMAT_TELEMINI3)
61                         return AltosConvert.tele_mini_3_pyro_voltage(sensor);
62                 return -1;
63         }
64
65         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
66                 super.provide_data(listener, cal_data);
67
68                 switch (cmd()) {
69                 case AltosLib.AO_LOG_FLIGHT:
70                         cal_data.set_flight(flight());
71                         cal_data.set_ground_pressure(ground_pres());
72                         break;
73                 case AltosLib.AO_LOG_STATE:
74                         listener.set_state(state());
75                         break;
76                 case AltosLib.AO_LOG_SENSOR:
77                         AltosPresTemp pt = eeprom.config_data().ms5607().pres_temp(pres(), temp());
78                         listener.set_pressure(pt.pres);
79                         listener.set_temperature(pt.temp);
80                         listener.set_apogee_voltage(pyro_voltage(sense_a()));
81                         listener.set_main_voltage(pyro_voltage(sense_m()));
82                         listener.set_battery_voltage(battery_voltage(v_batt()));
83                         break;
84                 }
85         }
86
87         public AltosEepromRecord next() {
88                 int     s = next_start();
89                 if (s < 0)
90                         return null;
91                 return new AltosEepromRecordMini(eeprom, s);
92         }
93
94         public AltosEepromRecordMini(AltosEepromNew eeprom, int start) {
95                 super(eeprom, start, record_length);
96         }
97
98         public AltosEepromRecordMini(AltosEepromNew eeprom) {
99                 this(eeprom, 0);
100         }
101 }