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