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