Add GPS height to the usual plot
[fw/altos] / altoslib / AltosEepromRecord.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;
19
20 import java.text.*;
21
22 public class AltosEepromRecord {
23         public int      cmd;
24         public int      tick;
25         public int      a;
26         public int      b;
27         public String   data;
28         public boolean  tick_valid;
29
30         public static final int record_length = 8;
31
32         public AltosEepromRecord (AltosEepromChunk chunk, int start) throws ParseException {
33
34                 cmd = chunk.data(start);
35                 tick_valid = true;
36
37                 tick_valid = !chunk.erased(start, record_length);
38                 if (tick_valid) {
39                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
40                                 throw new ParseException(String.format("invalid checksum at 0x%x",
41                                                                        chunk.address + start), 0);
42                 } else {
43                         cmd = AltosLib.AO_LOG_INVALID;
44                 }
45
46                 tick = chunk.data16(start + 2);
47                 a = chunk.data16(start + 4);
48                 b = chunk.data16(start + 6);
49
50                 data = null;
51         }
52
53         public String toString() {
54                 switch (cmd) {
55                 case AltosLib.AO_LOG_FLIGHT:
56                         return String.format("tick %5d flight %5d ground_accel %6d\n",
57                                              tick, b, a);
58                 case AltosLib.AO_LOG_SENSOR:
59                         return String.format("tick %5d accel %5d pres %5d\n",
60                                              tick, a, b);
61                 case AltosLib.AO_LOG_TEMP_VOLT:
62                 case AltosLib.AO_LOG_DEPLOY:
63                 case AltosLib.AO_LOG_STATE:
64                 case AltosLib.AO_LOG_GPS_TIME:
65                 case AltosLib.AO_LOG_GPS_LAT:
66                 case AltosLib.AO_LOG_GPS_LON:
67                 case AltosLib.AO_LOG_GPS_ALT:
68                 case AltosLib.AO_LOG_GPS_SAT:
69                 case AltosLib.AO_LOG_GPS_DATE:
70                 case AltosLib.AO_LOG_PRESSURE:
71                 default:
72                         return data;
73                 }
74         }
75
76         public AltosEepromRecord (String line) {
77                 tick_valid = false;
78                 tick = 0;
79                 a = 0;
80                 b = 0;
81                 data = null;
82                 if (line == null) {
83                         cmd = AltosLib.AO_LOG_INVALID;
84                         data = "";
85                 } else {
86                         try {
87                                 String[] tokens = line.split("\\s+");
88
89                                 if (tokens[0].length() == 1) {
90                                         if (tokens.length != 4) {
91                                                 cmd = AltosLib.AO_LOG_INVALID;
92                                                 data = line;
93                                         } else {
94                                                 cmd = tokens[0].codePointAt(0);
95                                                 tick = Integer.parseInt(tokens[1],16);
96                                                 tick_valid = true;
97                                                 a = Integer.parseInt(tokens[2],16);
98                                                 b = Integer.parseInt(tokens[3],16);
99                                         }
100                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
101                                         cmd = AltosLib.AO_LOG_CONFIG_VERSION;
102                                         data = tokens[2];
103                                 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
104                                         cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
105                                         a = Integer.parseInt(tokens[2]);
106                                 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
107                                         cmd = AltosLib.AO_LOG_APOGEE_DELAY;
108                                         a = Integer.parseInt(tokens[2]);
109                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
110                                         cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
111                                         a = Integer.parseInt(tokens[2]);
112                                 } else if (tokens[0].equals("Callsign:")) {
113                                         cmd = AltosLib.AO_LOG_CALLSIGN;
114                                         data = tokens[1].replaceAll("\"","");
115                                 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
116                                         cmd = AltosLib.AO_LOG_ACCEL_CAL;
117                                         a = Integer.parseInt(tokens[3]);
118                                         b = Integer.parseInt(tokens[5]);
119                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
120                                         cmd = AltosLib.AO_LOG_RADIO_CAL;
121                                         a = Integer.parseInt(tokens[2]);
122                                 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
123                                         cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
124                                         a = Integer.parseInt(tokens[3]);
125                                 } else if (tokens[0].equals("manufacturer")) {
126                                         cmd = AltosLib.AO_LOG_MANUFACTURER;
127                                         data = tokens[1];
128                                 } else if (tokens[0].equals("product")) {
129                                         cmd = AltosLib.AO_LOG_PRODUCT;
130                                         data = tokens[1];
131                                 } else if (tokens[0].equals("serial-number")) {
132                                         cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
133                                         a = Integer.parseInt(tokens[1]);
134                                 } else if (tokens[0].equals("log-format")) {
135                                         cmd = AltosLib.AO_LOG_LOG_FORMAT;
136                                         a = Integer.parseInt(tokens[1]);
137                                 } else if (tokens[0].equals("software-version")) {
138                                         cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
139                                         data = tokens[1];
140                                 } else {
141                                         cmd = AltosLib.AO_LOG_INVALID;
142                                         data = line;
143                                 }
144                         } catch (NumberFormatException ne) {
145                                 cmd = AltosLib.AO_LOG_INVALID;
146                                 data = line;
147                         }
148                 }
149         }
150
151         public AltosEepromRecord(int in_cmd, int in_tick, int in_a, int in_b) {
152                 tick_valid = true;
153                 cmd = in_cmd;
154                 tick = in_tick;
155                 a = in_a;
156                 b = in_b;
157         }
158 }