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