04155071fbd0767f143788cf91a5ef94754e8167
[fw/altos] / altoslib / AltosEepromMini.java
1 /*
2  * Copyright © 2011 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 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24
25 public class AltosEepromMini extends AltosEeprom {
26         public static final int record_length = 16;
27
28         public int record_length() { return record_length; }
29
30         /* AO_LOG_FLIGHT elements */
31         public int flight() { return data16(0); }
32         public int ground_pres() { return data32(4); }
33
34         /* AO_LOG_STATE elements */
35         public int state() { return data16(0); }
36         public int reason() { return data16(2); }
37
38         /* AO_LOG_SENSOR elements */
39         public int pres() { return data24(0); }
40         public int temp() { return data24(3); }
41         public int sense_a() { return data16(6); }
42         public int sense_m() { return data16(8); }
43         public int v_batt() { return data16(10); }
44
45         private double battery_voltage(AltosState state, int sensor) {
46                 if (state.log_format == AltosLib.AO_LOG_FORMAT_EASYMINI)
47                         return AltosConvert.easy_mini_voltage(sensor, state.serial);
48                 if (state.log_format == AltosLib.AO_LOG_FORMAT_TELEMINI2)
49                         return AltosConvert.tele_mini_2_voltage(sensor);
50                 if (state.log_format == AltosLib.AO_LOG_FORMAT_TELEMINI3)
51                         return AltosConvert.tele_mini_3_battery_voltage(sensor);
52                 return -1;
53         }
54
55         private double pyro_voltage(AltosState state, int sensor) {
56                 if (state.log_format == AltosLib.AO_LOG_FORMAT_EASYMINI)
57                         return AltosConvert.easy_mini_voltage(sensor, state.serial);
58                 if (state.log_format == AltosLib.AO_LOG_FORMAT_TELEMINI2)
59                         return AltosConvert.tele_mini_2_voltage(sensor);
60                 if (state.log_format == AltosLib.AO_LOG_FORMAT_TELEMINI3)
61                         return AltosConvert.tele_mini_3_pyro_voltage(sensor);
62                 return -1;
63         }
64
65         public void update_state(AltosState state) {
66                 super.update_state(state);
67
68                 switch (cmd) {
69                 case AltosLib.AO_LOG_FLIGHT:
70                         state.set_flight(flight());
71                         state.set_ground_pressure(ground_pres());
72                         break;
73                 case AltosLib.AO_LOG_STATE:
74                         state.set_state(state());
75                         break;
76                 case AltosLib.AO_LOG_SENSOR:
77                         state.set_ms5607(pres(), temp());
78                         state.set_apogee_voltage(pyro_voltage(state, sense_a()));
79                         state.set_main_voltage(pyro_voltage(state, sense_m()));
80                         state.set_battery_voltage(battery_voltage(state, v_batt()));
81                         break;
82                 }
83         }
84
85         public AltosEepromMini (AltosEepromChunk chunk, int start) throws ParseException {
86                 parse_chunk(chunk, start);
87         }
88
89         public AltosEepromMini (String line) {
90                 parse_string(line);
91         }
92
93         public AltosEepromMini(int in_cmd, int in_tick) {
94                 cmd = in_cmd;
95                 tick = in_tick;
96                 valid = true;
97         }
98
99         static public LinkedList<AltosEeprom> read(FileInputStream input) {
100                 LinkedList<AltosEeprom> minis = new LinkedList<AltosEeprom>();
101
102                 for (;;) {
103                         try {
104                                 String line = AltosLib.gets(input);
105                                 if (line == null)
106                                         break;
107                                 AltosEepromMini mini = new AltosEepromMini(line);
108                                 minis.add(mini);
109                         } catch (IOException ie) {
110                                 break;
111                         }
112                 }
113
114                 return minis;
115         }
116 }