altoslib: More AltosState hacking
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_1;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromMini extends AltosEeprom {
25         public static final int record_length = 16;
26
27         public int record_length() { return record_length; }
28
29         /* AO_LOG_FLIGHT elements */
30         public int flight() { return data16(0); }
31         public int ground_pres() { return data32(4); }
32
33         /* AO_LOG_STATE elements */
34         public int state() { return data16(0); }
35         public int reason() { return data16(2); }
36
37         /* AO_LOG_SENSOR elements */
38         public int pres() { return data24(0); }
39         public int temp() { return data24(3); }
40         public int sense_a() { return data16(6); }
41         public int sense_m() { return data16(8); }
42         public int v_batt() { return data16(10); }
43
44         double voltage(AltosState state, int sensor) {
45                 double  supply;
46
47                 if (state.log_format == AltosLib.AO_LOG_FORMAT_EASYMINI)
48                         supply = 3.0;
49                 else
50                         supply = 3.3;
51                 return sensor / 32767.0 * supply * 127/27;
52         }
53
54         public void update_state(AltosState state) {
55                 super.update_state(state);
56
57                 switch (cmd) {
58                 case AltosLib.AO_LOG_FLIGHT:
59                         state.set_flight(flight());
60                         state.set_ground_pressure(ground_pres());
61                         break;
62                 case AltosLib.AO_LOG_STATE:
63                         state.set_state(state());
64                         break;
65                 case AltosLib.AO_LOG_SENSOR:
66                         state.set_ms5607(pres(), temp());
67                         state.set_apogee_voltage(voltage(state, sense_a()));
68                         state.set_main_voltage(voltage(state, sense_m()));
69                         state.set_battery_voltage(voltage(state, v_batt()));
70                         break;
71                 }
72         }
73
74         public AltosEepromMini (AltosEepromChunk chunk, int start) throws ParseException {
75                 parse_chunk(chunk, start);
76         }
77
78         public AltosEepromMini (String line) {
79                 parse_string(line);
80         }
81
82         public AltosEepromMini(int in_cmd, int in_tick) {
83                 cmd = in_cmd;
84                 tick = in_tick;
85                 valid = true;
86         }
87
88         static public LinkedList<AltosEeprom> read(FileInputStream input) {
89                 LinkedList<AltosEeprom> minis = new LinkedList<AltosEeprom>();
90
91                 for (;;) {
92                         try {
93                                 String line = AltosLib.gets(input);
94                                 if (line == null)
95                                         break;
96                                 AltosEepromMini mini = new AltosEepromMini(line);
97                                 minis.add(mini);
98                         } catch (IOException ie) {
99                                 break;
100                         }
101                 }
102
103                 return minis;
104         }
105 }