altos: Add gyro-based orientation tracking
[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_2;
19
20 import java.util.*;
21 import java.io.*;
22 import java.nio.charset.Charset;
23
24 public class AltosLib {
25         /* EEProm command letters */
26         public static final int AO_LOG_FLIGHT = 'F';
27         public static final int AO_LOG_SENSOR = 'A';
28         public static final int AO_LOG_TEMP_VOLT = 'T';
29         public static final int AO_LOG_DEPLOY = 'D';
30         public static final int AO_LOG_STATE = 'S';
31         public static final int AO_LOG_GPS_POS = 'P';
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 telemega 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         public final static int MISSING = 0x7fffffff;
67
68         /* Added to flag invalid records */
69         public static final int AO_LOG_INVALID = -1;
70
71         /* Flight state numbers and names */
72         public static final int ao_flight_startup = 0;
73         public static final int ao_flight_idle = 1;
74         public static final int ao_flight_pad = 2;
75         public static final int ao_flight_boost = 3;
76         public static final int ao_flight_fast = 4;
77         public static final int ao_flight_coast = 5;
78         public static final int ao_flight_drogue = 6;
79         public static final int ao_flight_main = 7;
80         public static final int ao_flight_landed = 8;
81         public static final int ao_flight_invalid = 9;
82
83         /* USB product IDs */
84         public final static int vendor_altusmetrum = 0xfffe;
85
86         public final static int product_altusmetrum = 0x000a;
87         public final static int product_telemetrum = 0x000b;
88         public final static int product_teledongle = 0x000c;
89         public final static int product_teleterra = 0x000d;
90         public final static int product_telebt = 0x000e;
91         public final static int product_telelaunch = 0x000f;
92         public final static int product_telelco = 0x0010;
93         public final static int product_telescience = 0x0011;
94         public final static int product_telepyro =0x0012;
95         public final static int product_telemega = 0x0023;
96         public final static int product_megadongle = 0x0024;
97         public final static int product_telegps = 0x0025;
98         public final static int product_easymini = 0x0026;
99         public final static int product_telemini = 0x0027;
100         public final static int product_altusmetrum_min = 0x000a;
101         public final static int product_altusmetrum_max = 0x002c;
102
103         public final static int product_any = 0x10000;
104         public final static int product_basestation = 0x10000 + 1;
105         public final static int product_altimeter = 0x10000 + 2;
106
107         /* Bluetooth "identifier" (bluetooth sucks) */
108         public final static String bt_product_telebt = "TeleBT";
109
110         /* Telemetry modes */
111         public static final int ao_telemetry_off = 0;
112         public static final int ao_telemetry_min = 1;
113         public static final int ao_telemetry_standard = 1;
114         public static final int ao_telemetry_0_9 = 2;
115         public static final int ao_telemetry_0_8 = 3;
116         public static final int ao_telemetry_max = 3;
117
118         private static final String[] ao_telemetry_name = {
119                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
120         };
121
122         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
123
124         public static final int ao_telemetry_standard_len = 32;
125         public static final int ao_telemetry_0_9_len = 95;
126         public static final int ao_telemetry_0_8_len = 94;
127
128         private static final int[] ao_telemetry_len = {
129                 0, 32, 95, 94
130         };
131
132         private static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
133
134         private static boolean map_initialized = false;
135
136         public static void initialize_map()
137         {
138                 string_to_state.put("startup", ao_flight_startup);
139                 string_to_state.put("idle", ao_flight_idle);
140                 string_to_state.put("pad", ao_flight_pad);
141                 string_to_state.put("boost", ao_flight_boost);
142                 string_to_state.put("fast", ao_flight_fast);
143                 string_to_state.put("coast", ao_flight_coast);
144                 string_to_state.put("drogue", ao_flight_drogue);
145                 string_to_state.put("apogee", ao_flight_coast);
146                 string_to_state.put("main", ao_flight_main);
147                 string_to_state.put("landed", ao_flight_landed);
148                 string_to_state.put("invalid", ao_flight_invalid);
149                 map_initialized = true;
150         }
151
152         public static int telemetry_len(int telemetry) {
153                 if (telemetry <= ao_telemetry_max)
154                         return ao_telemetry_len[telemetry];
155                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
156                                                                  telemetry));
157         }
158
159         public static String telemetry_name(int telemetry) {
160                 if (telemetry <= ao_telemetry_max)
161                         return ao_telemetry_name[telemetry];
162                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
163                                                                  telemetry));
164         }
165         
166         private static String[] state_to_string = {
167                 "startup",
168                 "idle",
169                 "pad",
170                 "boost",
171                 "fast",
172                 "coast",
173                 "drogue",
174                 "main",
175                 "landed",
176                 "invalid",
177         };
178
179         private static String[] state_to_string_capital = {
180                 "Startup",
181                 "Idle",
182                 "Pad",
183                 "Boost",
184                 "Fast",
185                 "Coast",
186                 "Drogue",
187                 "Main",
188                 "Landed",
189                 "Invalid",
190         };
191
192         public static int state(String state) {
193                 if (!map_initialized)
194                         initialize_map();
195                 if (string_to_state.containsKey(state))
196                         return string_to_state.get(state);
197                 return ao_flight_invalid;
198         }
199
200         public static String state_name(int state) {
201                 if (state < 0 || state_to_string.length <= state)
202                         return "invalid";
203                 return state_to_string[state];
204         }
205
206         public static String state_name_capital(int state) {
207                 if (state < 0 || state_to_string.length <= state)
208                         return "Invalid";
209                 return state_to_string_capital[state];
210         }
211
212         public static final int AO_GPS_VALID = (1 << 4);
213         public static final int AO_GPS_RUNNING = (1 << 5);
214         public static final int AO_GPS_DATE_VALID = (1 << 6);
215         public static final int AO_GPS_NUM_SAT_SHIFT = 0;
216         public static final int AO_GPS_NUM_SAT_MASK = 0xf;
217
218         public static final int AO_LOG_FORMAT_UNKNOWN = 0;
219         public static final int AO_LOG_FORMAT_FULL = 1;
220         public static final int AO_LOG_FORMAT_TINY = 2;
221         public static final int AO_LOG_FORMAT_TELEMETRY = 3;
222         public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
223         public static final int AO_LOG_FORMAT_TELEMEGA = 5;
224         public static final int AO_LOG_FORMAT_EASYMINI = 6;
225         public static final int AO_LOG_FORMAT_TELEMETRUM = 7;
226         public static final int AO_LOG_FORMAT_TELEMINI = 8;
227         public static final int AO_LOG_FORMAT_NONE = 127;
228
229         public static boolean isspace(int c) {
230                 switch (c) {
231                 case ' ':
232                 case '\t':
233                         return true;
234                 }
235                 return false;
236         }
237
238         public static boolean ishex(int c) {
239                 if ('0' <= c && c <= '9')
240                         return true;
241                 if ('a' <= c && c <= 'f')
242                         return true;
243                 if ('A' <= c && c <= 'F')
244                         return true;
245                 return false;
246         }
247
248         public static boolean ishex(String s) {
249                 for (int i = 0; i < s.length(); i++)
250                         if (!ishex(s.charAt(i)))
251                                 return false;
252                 return true;
253         }
254
255         public static int fromhex(int c) {
256                 if ('0' <= c && c <= '9')
257                         return c - '0';
258                 if ('a' <= c && c <= 'f')
259                         return c - 'a' + 10;
260                 if ('A' <= c && c <= 'F')
261                         return c - 'A' + 10;
262                 return -1;
263         }
264
265         public static int fromhex(String s) throws NumberFormatException {
266                 int c, v = 0;
267                 for (int i = 0; i < s.length(); i++) {
268                         c = s.charAt(i);
269                         if (!ishex(c)) {
270                                 if (i == 0)
271                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
272                                 return v;
273                         }
274                         v = v * 16 + fromhex(c);
275                 }
276                 return v;
277         }
278
279         public static boolean isdec(int c) {
280                 if ('0' <= c && c <= '9')
281                         return true;
282                 return false;
283         }
284
285         public static boolean isdec(String s) {
286                 for (int i = 0; i < s.length(); i++)
287                         if (!isdec(s.charAt(i)))
288                                 return false;
289                 return true;
290         }
291
292         public static int fromdec(int c) {
293                 if ('0' <= c && c <= '9')
294                         return c - '0';
295                 return -1;
296         }
297
298         public static int int8(int[] bytes, int i) {
299                 return (int) (byte) bytes[i];
300         }
301
302         public static int uint8(int[] bytes, int i) {
303                 return bytes[i];
304         }
305
306         public static int int16(int[] bytes, int i) {
307                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
308         }
309
310         public static int uint16(int[] bytes, int i) {
311                 return bytes[i] + (bytes[i+1] << 8);
312         }
313
314         public static int uint32(int[] bytes, int i) {
315                 return bytes[i] +
316                         (bytes[i+1] << 8) +
317                         (bytes[i+2] << 16) +
318                         (bytes[i+3] << 24);
319         }
320
321         public static int int32(int[] bytes, int i) {
322                 return (int) uint32(bytes, i);
323         }
324
325         public static final Charset     unicode_set = Charset.forName("UTF-8");
326
327         public static String string(int[] bytes, int s, int l) {
328                 if (s + l > bytes.length) {
329                         if (s > bytes.length) {
330                                 s = bytes.length;
331                                 l = 0;
332                         } else {
333                                 l = bytes.length - s;
334                         }
335                 }
336
337                 int i;
338                 for (i = l - 1; i >= 0; i--)
339                         if (bytes[s+i] != 0)
340                                 break;
341
342                 l = i + 1;
343                 byte[]  b = new byte[l];
344
345                 for (i = 0; i < l; i++)
346                         b[i] = (byte) bytes[s+i];
347                 String n = new String(b, unicode_set);
348                 return n;
349         }
350
351         public static int hexbyte(String s, int i) {
352                 int c0, c1;
353
354                 if (s.length() < i + 2)
355                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
356                 c0 = s.charAt(i);
357                 if (!ishex(c0))
358                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
359                 c1 = s.charAt(i+1);
360                 if (!ishex(c1))
361                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
362                 return fromhex(c0) * 16 + fromhex(c1);
363         }
364
365         public static int[] hexbytes(String s) {
366                 int     n;
367                 int[]   r;
368                 int     i;
369
370                 if ((s.length() & 1) != 0)
371                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
372                 n = s.length() / 2;
373                 r = new int[n];
374                 for (i = 0; i < n; i++)
375                         r[i] = hexbyte(s, i * 2);
376                 return r;
377         }
378
379         public static int fromdec(String s) throws NumberFormatException {
380                 int c, v = 0;
381                 int sign = 1;
382                 for (int i = 0; i < s.length(); i++) {
383                         c = s.charAt(i);
384                         if (i == 0 && c == '-') {
385                                 sign = -1;
386                         } else if (!isdec(c)) {
387                                 if (i == 0)
388                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
389                                 return v;
390                         } else
391                                 v = v * 10 + fromdec(c);
392                 }
393                 return v * sign;
394         }
395
396         public static String gets(FileInputStream s) throws IOException {
397                 int c;
398                 String  line = "";
399
400                 while ((c = s.read()) != -1) {
401                         if (c == '\r')
402                                 continue;
403                         if (c == '\n') {
404                                 return line;
405                         }
406                         line = line + (char) c;
407                 }
408                 return null;
409         }
410
411         public static String replace_extension(String input, String extension) {
412                 int dot = input.lastIndexOf(".");
413                 if (dot > 0)
414                         input = input.substring(0,dot);
415                 return input.concat(extension);
416         }
417
418         public static File replace_extension(File input, String extension) {
419                 return new File(replace_extension(input.getPath(), extension));
420         }
421
422         public static String product_name(int product_id) {
423                 switch (product_id) {
424                 case product_altusmetrum: return "AltusMetrum";
425                 case product_telemetrum: return "TeleMetrum";
426                 case product_teledongle: return "TeleDongle";
427                 case product_teleterra: return "TeleTerra";
428                 case product_telebt: return "TeleBT";
429                 case product_telelaunch: return "TeleLaunch";
430                 case product_telelco: return "TeleLco";
431                 case product_telescience: return "Telescience";
432                 case product_telepyro: return "TelePyro";
433                 case product_telemega: return "TeleMega";
434                 case product_megadongle: return "MegaDongle";
435                 case product_telegps: return "TeleGPS";
436                 case product_easymini: return "EasyMini";
437                 case product_telemini: return "TeleMini";
438                 default: return "unknown";
439                 }
440         }
441 }