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