altoslib: Make sure AltosFlightSeries is filled in before use
[fw/altos] / altoslib / AltosEeprom.java
1 /*
2  * Copyright © 2013 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 abstract class AltosEeprom implements AltosDataProvider {
26         public int      cmd;
27         public int      tick;
28         public int      data8[];
29         public boolean  valid;
30
31         public int data8(int i) {
32                 return data8[i];
33         }
34
35         public int data16(int i) {
36                 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
37         }
38
39         public int data24(int i) {
40                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16);
41         }
42
43         public int data32(int i) {
44                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
45         }
46
47         public boolean has_seconds() { return false; }
48
49         public int seconds() { return 0; }
50
51         public final static int header_length = 4;
52
53         public abstract int record_length();
54
55         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
56                 cal_data.set_tick(tick);
57                 if (cmd == AltosLib.AO_LOG_FLIGHT)
58                         cal_data.set_boost_tick();
59                 listener.set_time(cal_data.time());
60         }
61
62         public void write(PrintStream out) {
63                 out.printf("%c %04x", cmd, tick);
64                 if (data8 != null) {
65                         for (int i = 0; i < data8.length; i++)
66                                 out.printf (" %02x", data8[i]);
67                 }
68                 out.printf ("\n");
69         }
70
71         public String string() {
72                 String  s;
73
74                 s = String.format("%c %04x", cmd, tick);
75                 if (data8 != null) {
76                         for (int i = 0; i < data8.length; i++) {
77                                 String  d = String.format(" %02x", data8[i]);
78                                 s = s.concat(d);
79                         }
80                 }
81                 s = s.concat("\n");
82                 return s;
83         }
84
85         void parse_chunk(AltosEepromChunk chunk, int start) throws ParseException {
86                 cmd = chunk.data(start);
87
88                 int data_length = record_length() - header_length;
89
90                 valid = !chunk.erased(start, record_length());
91                 if (valid) {
92                         if (AltosConvert.checksum(chunk.data, start, record_length()) != 0)
93                                 throw new ParseException(String.format("invalid checksum at 0x%x",
94                                                                        chunk.address + start), 0);
95                 } else {
96                         cmd = AltosLib.AO_LOG_INVALID;
97                 }
98
99                 tick = chunk.data16(start+2);
100
101                 data8 = new int[data_length];
102                 for (int i = 0; i < data_length; i++)
103                         data8[i] = chunk.data(start + header_length + i);
104         }
105
106         void parse_string(String line) {
107                 valid = false;
108                 tick = 0;
109                 cmd = AltosLib.AO_LOG_INVALID;
110
111                 int data_length = record_length() - header_length;
112
113                 if (line == null)
114                         return;
115                 try {
116                         String[] tokens = line.split("\\s+");
117
118                         if (tokens[0].length() == 1) {
119                                 if (tokens.length == 2 + data_length) {
120                                         cmd = tokens[0].codePointAt(0);
121                                         tick = Integer.parseInt(tokens[1],16);
122                                         valid = true;
123                                         data8 = new int[data_length];
124
125                                         for (int i = 0; i < data_length; i++)
126                                                 data8[i] = Integer.parseInt(tokens[2 + i],16);
127                                 }
128                         }
129                 } catch (NumberFormatException ne) {
130                 }
131         }
132 }