ao-tools: Add TMv2 and Tgps log formats to cc.h
[fw/altos] / altoslib / AltosEepromTm.java
1 /*
2  * Copyright © 2010 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_5;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromTm extends AltosEeprom {
25         public int      i;
26         public int      a;
27         public int      b;
28
29         public static final int record_length = 2;
30
31         public void write(PrintStream out) {
32                 out.printf("%c %4x %4x %4x\n", cmd, tick, a, b);
33         }
34
35         public int record_length() { return record_length; }
36
37         public String string() {
38                 return String.format("%c %4x %4x %4x\n", cmd, tick, a, b);
39         }
40
41         public void update_state(AltosState state) {
42                 super.update_state(state);
43
44                 switch (cmd) {
45                 case AltosLib.AO_LOG_FLIGHT:
46                         state.set_state(AltosLib.ao_flight_boost);
47                         state.set_flight(b);
48                         break;
49                 case AltosLib.AO_LOG_PRESSURE:
50                         if (tick == 0)
51                                 state.set_ground_pressure(AltosConvert.barometer_to_pressure(b));
52                         else
53                                 state.set_pressure(AltosConvert.barometer_to_pressure(b));
54                         break;
55                 case AltosLib.AO_LOG_STATE:
56                         state.set_state(a);
57                         break;
58                 }
59         }
60
61         public AltosEepromTm (AltosEepromChunk chunk, int start, AltosState state) throws ParseException {
62                 int     value = chunk.data16(start);
63
64                 int     i = (chunk.address + start) / record_length;
65
66                 cmd = chunk.data(start);
67                 valid = true;
68
69                 valid = !chunk.erased(start, record_length);
70
71                 switch (i) {
72                 case 0:
73                         cmd = AltosLib.AO_LOG_FLIGHT;
74                         tick = 0;
75                         a = 0;
76                         b = value;
77                         break;
78                 case 1:
79                         cmd = AltosLib.AO_LOG_PRESSURE;
80                         tick = 0;
81                         a = 0;
82                         b = value;
83                         break;
84                 default:
85                         if ((value & 0x8000) != 0) {
86                                 cmd = AltosLib.AO_LOG_STATE;
87                                 tick = state.tick;
88                                 a = value & 0x7fff;
89                                 b = 0;
90                         } else {
91                                 if (state.ascent)
92                                         tick = state.tick + 10;
93                                 else
94                                         tick = state.tick + 100;
95                                 cmd = AltosLib.AO_LOG_PRESSURE;
96                                 a = 0;
97                                 b = value;
98                         }
99                         break;
100                 }
101         }
102
103         public AltosEepromTm (String line) {
104                 valid = false;
105                 tick = 0;
106                 a = 0;
107                 b = 0;
108                 if (line == null) {
109                         cmd = AltosLib.AO_LOG_INVALID;
110                 } else {
111                         try {
112                                 String[] tokens = line.split("\\s+");
113
114                                 if (tokens[0].length() == 1) {
115                                         if (tokens.length != 4) {
116                                                 cmd = AltosLib.AO_LOG_INVALID;
117                                         } else {
118                                                 cmd = tokens[0].codePointAt(0);
119                                                 tick = Integer.parseInt(tokens[1],16);
120                                                 valid = true;
121                                                 a = Integer.parseInt(tokens[2],16);
122                                                 b = Integer.parseInt(tokens[3],16);
123                                         }
124                                 } else {
125                                         cmd = AltosLib.AO_LOG_INVALID;
126                                 }
127                         } catch (NumberFormatException ne) {
128                                 cmd = AltosLib.AO_LOG_INVALID;
129                         }
130                 }
131         }
132
133         public AltosEepromTm(int in_cmd, int in_tick, int in_a, int in_b) {
134                 valid = true;
135                 cmd = in_cmd;
136                 tick = in_tick;
137                 a = in_a;
138                 b = in_b;
139         }
140
141         static public LinkedList<AltosEeprom> read(FileInputStream input) {
142                 LinkedList<AltosEeprom> tms = new LinkedList<AltosEeprom>();
143
144                 for (;;) {
145                         try {
146                                 String line = AltosLib.gets(input);
147                                 if (line == null)
148                                         break;
149                                 AltosEepromTm tm = new AltosEepromTm(line);
150                                 tms.add(tm);
151                         } catch (IOException ie) {
152                                 break;
153                         }
154                 }
155
156                 return tms;
157         }
158
159 }