altosui: More changes to migrate code to altoslib
[fw/altos] / altoslib / AltosLib.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.awt.*;
21 import java.util.*;
22 import java.text.*;
23 import java.nio.charset.Charset;
24
25 public class AltosLib {
26         /* EEProm command letters */
27         public static final int AO_LOG_FLIGHT = 'F';
28         public static final int AO_LOG_SENSOR = 'A';
29         public static final int AO_LOG_TEMP_VOLT = 'T';
30         public static final int AO_LOG_DEPLOY = 'D';
31         public static final int AO_LOG_STATE = 'S';
32         public static final int AO_LOG_GPS_TIME = 'G';
33         public static final int AO_LOG_GPS_LAT = 'N';
34         public static final int AO_LOG_GPS_LON = 'W';
35         public static final int AO_LOG_GPS_ALT = 'H';
36         public static final int AO_LOG_GPS_SAT = 'V';
37         public static final int AO_LOG_GPS_DATE = 'Y';
38         public static final int AO_LOG_PRESSURE = 'P';
39
40         /* Added for header fields in eeprom files */
41         public static final int AO_LOG_CONFIG_VERSION = 1000;
42         public static final int AO_LOG_MAIN_DEPLOY = 1001;
43         public static final int AO_LOG_APOGEE_DELAY = 1002;
44         public static final int AO_LOG_RADIO_CHANNEL = 1003;
45         public static final int AO_LOG_CALLSIGN = 1004;
46         public static final int AO_LOG_ACCEL_CAL = 1005;
47         public static final int AO_LOG_RADIO_CAL = 1006;
48         public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
49         public static final int AO_LOG_MANUFACTURER = 2000;
50         public static final int AO_LOG_PRODUCT = 2001;
51         public static final int AO_LOG_SERIAL_NUMBER = 2002;
52         public static final int AO_LOG_LOG_FORMAT = 2003;
53
54         /* Added for header fields in megametrum files */
55         public static final int AO_LOG_BARO_RESERVED = 3000;
56         public static final int AO_LOG_BARO_SENS = 3001;
57         public static final int AO_LOG_BARO_OFF = 3002;
58         public static final int AO_LOG_BARO_TCS = 3004;
59         public static final int AO_LOG_BARO_TCO = 3005;
60         public static final int AO_LOG_BARO_TREF = 3006;
61         public static final int AO_LOG_BARO_TEMPSENS = 3007;
62         public static final int AO_LOG_BARO_CRC = 3008;
63
64         public static final int AO_LOG_SOFTWARE_VERSION = 9999;
65
66         /* Added to flag invalid records */
67         public static final int AO_LOG_INVALID = -1;
68
69         /* Flight state numbers and names */
70         public static final int ao_flight_startup = 0;
71         public static final int ao_flight_idle = 1;
72         public static final int ao_flight_pad = 2;
73         public static final int ao_flight_boost = 3;
74         public static final int ao_flight_fast = 4;
75         public static final int ao_flight_coast = 5;
76         public static final int ao_flight_drogue = 6;
77         public static final int ao_flight_main = 7;
78         public static final int ao_flight_landed = 8;
79         public static final int ao_flight_invalid = 9;
80
81         /* Telemetry modes */
82         public static final int ao_telemetry_off = 0;
83         public static final int ao_telemetry_min = 1;
84         public static final int ao_telemetry_standard = 1;
85         public static final int ao_telemetry_0_9 = 2;
86         public static final int ao_telemetry_0_8 = 3;
87         public static final int ao_telemetry_max = 3;
88
89         public static final String[] ao_telemetry_name = {
90                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
91         };
92
93         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
94
95         public static final int ao_telemetry_standard_len = 32;
96         public static final int ao_telemetry_0_9_len = 95;
97         public static final int ao_telemetry_0_8_len = 94;
98
99         public static final int[] ao_telemetry_len = {
100                 0, 32, 95, 94
101         };
102
103         public static HashMap<String,Integer>   string_to_state = new HashMap<String,Integer>();
104
105         public static boolean map_initialized = false;
106
107         public static void initialize_map()
108         {
109                 string_to_state.put("startup", ao_flight_startup);
110                 string_to_state.put("idle", ao_flight_idle);
111                 string_to_state.put("pad", ao_flight_pad);
112                 string_to_state.put("boost", ao_flight_boost);
113                 string_to_state.put("fast", ao_flight_fast);
114                 string_to_state.put("coast", ao_flight_coast);
115                 string_to_state.put("drogue", ao_flight_drogue);
116                 string_to_state.put("apogee", ao_flight_coast);
117                 string_to_state.put("main", ao_flight_main);
118                 string_to_state.put("landed", ao_flight_landed);
119                 string_to_state.put("invalid", ao_flight_invalid);
120                 map_initialized = true;
121         }
122
123         public static int telemetry_len(int telemetry) {
124                 if (telemetry <= ao_telemetry_max)
125                         return ao_telemetry_len[telemetry];
126                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
127                                                                  telemetry));
128         }
129
130         public static String telemetry_name(int telemetry) {
131                 if (telemetry <= ao_telemetry_max)
132                         return ao_telemetry_name[telemetry];
133                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
134                                                                  telemetry));
135         }
136         
137         public static String[] state_to_string = {
138                 "startup",
139                 "idle",
140                 "pad",
141                 "boost",
142                 "fast",
143                 "coast",
144                 "drogue",
145                 "main",
146                 "landed",
147                 "invalid",
148         };
149
150         public static String[] state_to_string_capital = {
151                 "Startup",
152                 "Idle",
153                 "Pad",
154                 "Boost",
155                 "Fast",
156                 "Coast",
157                 "Drogue",
158                 "Main",
159                 "Landed",
160                 "Invalid",
161         };
162
163         public static int state(String state) {
164                 if (!map_initialized)
165                         initialize_map();
166                 if (string_to_state.containsKey(state))
167                         return string_to_state.get(state);
168                 return ao_flight_invalid;
169         }
170
171         public static String state_name(int state) {
172                 if (state < 0 || state_to_string.length <= state)
173                         return "invalid";
174                 return state_to_string[state];
175         }
176
177         public static final int AO_GPS_VALID = (1 << 4);
178         public static final int AO_GPS_RUNNING = (1 << 5);
179         public static final int AO_GPS_DATE_VALID = (1 << 6);
180         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
181         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
182
183         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
184         public static final int AO_LOG_FORMAT_FULL = 1;
185         public static final int AO_LOG_FORMAT_TINY = 2;
186         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
187         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
188         public static final int AO_LOG_FORMAT_MEGAMETRUM = 5;
189         public static final int AO_LOG_FORMAT_NONE = 127;
190
191         public static boolean isspace(int c) {
192                 switch (c) {
193                 case ' ':
194                 case '\t':
195                         return true;
196                 }
197                 return false;
198         }
199
200         public static boolean ishex(int c) {
201                 if ('0' <= c && c <= '9')
202                         return true;
203                 if ('a' <= c && c <= 'f')
204                         return true;
205                 if ('A' <= c && c <= 'F')
206                         return true;
207                 return false;
208         }
209
210         public static boolean ishex(String s) {
211                 for (int i = 0; i < s.length(); i++)
212                         if (!ishex(s.charAt(i)))
213                                 return false;
214                 return true;
215         }
216
217         public static int fromhex(int c) {
218                 if ('0' <= c && c <= '9')
219                         return c - '0';
220                 if ('a' <= c && c <= 'f')
221                         return c - 'a' + 10;
222                 if ('A' <= c && c <= 'F')
223                         return c - 'A' + 10;
224                 return -1;
225         }
226
227         public static int fromhex(String s) throws NumberFormatException {
228                 int c, v = 0;
229                 for (int i = 0; i < s.length(); i++) {
230                         c = s.charAt(i);
231                         if (!ishex(c)) {
232                                 if (i == 0)
233                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
234                                 return v;
235                         }
236                         v = v * 16 + fromhex(c);
237                 }
238                 return v;
239         }
240
241         public static boolean isdec(int c) {
242                 if ('0' <= c && c <= '9')
243                         return true;
244                 return false;
245         }
246
247         public static boolean isdec(String s) {
248                 for (int i = 0; i < s.length(); i++)
249                         if (!isdec(s.charAt(i)))
250                                 return false;
251                 return true;
252         }
253
254         public static int fromdec(int c) {
255                 if ('0' <= c && c <= '9')
256                         return c - '0';
257                 return -1;
258         }
259
260         public static int int8(int[] bytes, int i) {
261                 return (int) (byte) bytes[i];
262         }
263
264         public static int uint8(int[] bytes, int i) {
265                 return bytes[i];
266         }
267
268         public static int int16(int[] bytes, int i) {
269                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
270         }
271
272         public static int uint16(int[] bytes, int i) {
273                 return bytes[i] + (bytes[i+1] << 8);
274         }
275
276         public static int uint32(int[] bytes, int i) {
277                 return bytes[i] +
278                         (bytes[i+1] << 8) +
279                         (bytes[i+2] << 16) +
280                         (bytes[i+3] << 24);
281         }
282
283         public static final Charset     unicode_set = Charset.forName("UTF-8");
284
285         public static String string(int[] bytes, int s, int l) {
286                 if (s + l > bytes.length) {
287                         if (s > bytes.length) {
288                                 s = bytes.length;
289                                 l = 0;
290                         } else {
291                                 l = bytes.length - s;
292                         }
293                 }
294
295                 int i;
296                 for (i = l - 1; i >= 0; i--)
297                         if (bytes[s+i] != 0)
298                                 break;
299
300                 l = i + 1;
301                 byte[]  b = new byte[l];
302
303                 for (i = 0; i < l; i++)
304                         b[i] = (byte) bytes[s+i];
305                 String n = new String(b, unicode_set);
306                 return n;
307         }
308
309         public static int hexbyte(String s, int i) {
310                 int c0, c1;
311
312                 if (s.length() < i + 2)
313                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
314                 c0 = s.charAt(i);
315                 if (!ishex(c0))
316                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
317                 c1 = s.charAt(i+1);
318                 if (!ishex(c1))
319                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
320                 return fromhex(c0) * 16 + fromhex(c1);
321         }
322
323         public static int[] hexbytes(String s) {
324                 int     n;
325                 int[]   r;
326                 int     i;
327
328                 if ((s.length() & 1) != 0)
329                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
330                 n = s.length() / 2;
331                 r = new int[n];
332                 for (i = 0; i < n; i++)
333                         r[i] = hexbyte(s, i * 2);
334                 return r;
335         }
336
337         public static int fromdec(String s) throws NumberFormatException {
338                 int c, v = 0;
339                 int sign = 1;
340                 for (int i = 0; i < s.length(); i++) {
341                         c = s.charAt(i);
342                         if (i == 0 && c == '-') {
343                                 sign = -1;
344                         } else if (!isdec(c)) {
345                                 if (i == 0)
346                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
347                                 return v;
348                         } else
349                                 v = v * 10 + fromdec(c);
350                 }
351                 return v * sign;
352         }
353
354         public static String replace_extension(String input, String extension) {
355                 int dot = input.lastIndexOf(".");
356                 if (dot > 0)
357                         input = input.substring(0,dot);
358                 return input.concat(extension);
359         }
360 }