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