altosui: Quick hacks to download megametrum data and convert to CSV
[fw/altos] / altosui / Altos.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 altosui;
19
20 import java.awt.*;
21 import java.util.*;
22 import java.text.*;
23 import java.nio.charset.Charset;
24
25 import libaltosJNI.*;
26
27 public class Altos {
28         /* EEProm command letters */
29         static final int AO_LOG_FLIGHT = 'F';
30         static final int AO_LOG_SENSOR = 'A';
31         static final int AO_LOG_TEMP_VOLT = 'T';
32         static final int AO_LOG_DEPLOY = 'D';
33         static final int AO_LOG_STATE = 'S';
34         static final int AO_LOG_GPS_TIME = 'G';
35         static final int AO_LOG_GPS_LAT = 'N';
36         static final int AO_LOG_GPS_LON = 'W';
37         static final int AO_LOG_GPS_ALT = 'H';
38         static final int AO_LOG_GPS_SAT = 'V';
39         static final int AO_LOG_GPS_DATE = 'Y';
40         static final int AO_LOG_PRESSURE = 'P';
41
42         /* Added for header fields in eeprom files */
43         static final int AO_LOG_CONFIG_VERSION = 1000;
44         static final int AO_LOG_MAIN_DEPLOY = 1001;
45         static final int AO_LOG_APOGEE_DELAY = 1002;
46         static final int AO_LOG_RADIO_CHANNEL = 1003;
47         static final int AO_LOG_CALLSIGN = 1004;
48         static final int AO_LOG_ACCEL_CAL = 1005;
49         static final int AO_LOG_RADIO_CAL = 1006;
50         static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
51         static final int AO_LOG_MANUFACTURER = 2000;
52         static final int AO_LOG_PRODUCT = 2001;
53         static final int AO_LOG_SERIAL_NUMBER = 2002;
54         static final int AO_LOG_LOG_FORMAT = 2003;
55
56         /* Added for header fields in megametrum files */
57         static final int AO_LOG_BARO_RESERVED = 3000;
58         static final int AO_LOG_BARO_SENS = 3001;
59         static final int AO_LOG_BARO_OFF = 3002;
60         static final int AO_LOG_BARO_TCS = 3004;
61         static final int AO_LOG_BARO_TCO = 3005;
62         static final int AO_LOG_BARO_TREF = 3006;
63         static final int AO_LOG_BARO_TEMPSENS = 3007;
64         static final int AO_LOG_BARO_CRC = 3008;
65
66         static final int AO_LOG_SOFTWARE_VERSION = 9999;
67
68         /* Added to flag invalid records */
69         static final int AO_LOG_INVALID = -1;
70
71         /* Flight state numbers and names */
72         static final int ao_flight_startup = 0;
73         static final int ao_flight_idle = 1;
74         static final int ao_flight_pad = 2;
75         static final int ao_flight_boost = 3;
76         static final int ao_flight_fast = 4;
77         static final int ao_flight_coast = 5;
78         static final int ao_flight_drogue = 6;
79         static final int ao_flight_main = 7;
80         static final int ao_flight_landed = 8;
81         static final int ao_flight_invalid = 9;
82
83         /* Telemetry modes */
84         static final int ao_telemetry_off = 0;
85         static final int ao_telemetry_min = 1;
86         static final int ao_telemetry_standard = 1;
87         static final int ao_telemetry_0_9 = 2;
88         static final int ao_telemetry_0_8 = 3;
89         static final int ao_telemetry_max = 3;
90
91         static final String[] ao_telemetry_name = {
92                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
93         };
94
95         static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
96
97         static final int ao_telemetry_standard_len = 32;
98         static final int ao_telemetry_0_9_len = 95;
99         static final int ao_telemetry_0_8_len = 94;
100
101         static final int[] ao_telemetry_len = {
102                 0, 32, 95, 94
103         };
104
105         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
106
107         static boolean map_initialized = false;
108
109         static final int tab_elt_pad = 5;
110
111         static Font label_font;
112         static Font value_font;
113         static Font status_font;
114         static Font table_label_font;
115         static Font table_value_font;
116
117         final static int font_size_small = 1;
118         final static int font_size_medium = 2;
119         final static int font_size_large = 3;
120
121         static void set_fonts(int size) {
122                 int     brief_size;
123                 int     table_size;
124                 int     status_size;
125
126                 switch (size) {
127                 case font_size_small:
128                         brief_size = 16;
129                         status_size = 18;
130                         table_size = 11;
131                         break;
132                 default:
133                 case font_size_medium:
134                         brief_size = 22;
135                         status_size = 24;
136                         table_size = 14;
137                         break;
138                 case font_size_large:
139                         brief_size = 26;
140                         status_size = 30;
141                         table_size = 17;
142                         break;
143                 }
144                 label_font = new Font("Dialog", Font.PLAIN, brief_size);
145                 value_font = new Font("Monospaced", Font.PLAIN, brief_size);
146                 status_font = new Font("SansSerif", Font.BOLD, status_size);
147                 table_label_font = new Font("SansSerif", Font.PLAIN, table_size);
148                 table_value_font = new Font("Monospaced", Font.PLAIN, table_size);
149         }
150
151         static final int text_width = 20;
152
153         static void initialize_map()
154         {
155                 string_to_state.put("startup", ao_flight_startup);
156                 string_to_state.put("idle", ao_flight_idle);
157                 string_to_state.put("pad", ao_flight_pad);
158                 string_to_state.put("boost", ao_flight_boost);
159                 string_to_state.put("fast", ao_flight_fast);
160                 string_to_state.put("coast", ao_flight_coast);
161                 string_to_state.put("drogue", ao_flight_drogue);
162                 string_to_state.put("apogee", ao_flight_coast);
163                 string_to_state.put("main", ao_flight_main);
164                 string_to_state.put("landed", ao_flight_landed);
165                 string_to_state.put("invalid", ao_flight_invalid);
166                 map_initialized = true;
167         }
168
169         static int telemetry_len(int telemetry) {
170                 if (telemetry <= ao_telemetry_max)
171                         return ao_telemetry_len[telemetry];
172                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
173                                                                  telemetry));
174         }
175
176         static String telemetry_name(int telemetry) {
177                 if (telemetry <= ao_telemetry_max)
178                         return ao_telemetry_name[telemetry];
179                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
180                                                                  telemetry));
181         }
182         
183         static String[] state_to_string = {
184                 "startup",
185                 "idle",
186                 "pad",
187                 "boost",
188                 "fast",
189                 "coast",
190                 "drogue",
191                 "main",
192                 "landed",
193                 "invalid",
194         };
195
196         static String[] state_to_string_capital = {
197                 "Startup",
198                 "Idle",
199                 "Pad",
200                 "Boost",
201                 "Fast",
202                 "Coast",
203                 "Drogue",
204                 "Main",
205                 "Landed",
206                 "Invalid",
207         };
208
209         static public int state(String state) {
210                 if (!map_initialized)
211                         initialize_map();
212                 if (string_to_state.containsKey(state))
213                         return string_to_state.get(state);
214                 return ao_flight_invalid;
215         }
216
217         static public String state_name(int state) {
218                 if (state < 0 || state_to_string.length <= state)
219                         return "invalid";
220                 return state_to_string[state];
221         }
222
223         static final int AO_GPS_VALID = (1 << 4);
224         static final int AO_GPS_RUNNING = (1 << 5);
225         static final int AO_GPS_DATE_VALID = (1 << 6);
226         static final int AO_GPS_NUM_SAT_SHIFT = 0;
227         static final int AO_GPS_NUM_SAT_MASK = 0xf;
228
229         static final int AO_LOG_FORMAT_UNKNOWN = 0;
230         static final int AO_LOG_FORMAT_FULL = 1;
231         static final int AO_LOG_FORMAT_TINY = 2;
232         static final int AO_LOG_FORMAT_TELEMETRY = 3;
233         static final int AO_LOG_FORMAT_TELESCIENCE = 4;
234         static final int AO_LOG_FORMAT_MEGAMETRUM = 5;
235         static final int AO_LOG_FORMAT_NONE = 127;
236
237         static boolean isspace(int c) {
238                 switch (c) {
239                 case ' ':
240                 case '\t':
241                         return true;
242                 }
243                 return false;
244         }
245
246         static boolean ishex(int c) {
247                 if ('0' <= c && c <= '9')
248                         return true;
249                 if ('a' <= c && c <= 'f')
250                         return true;
251                 if ('A' <= c && c <= 'F')
252                         return true;
253                 return false;
254         }
255
256         static boolean ishex(String s) {
257                 for (int i = 0; i < s.length(); i++)
258                         if (!ishex(s.charAt(i)))
259                                 return false;
260                 return true;
261         }
262
263         static int fromhex(int c) {
264                 if ('0' <= c && c <= '9')
265                         return c - '0';
266                 if ('a' <= c && c <= 'f')
267                         return c - 'a' + 10;
268                 if ('A' <= c && c <= 'F')
269                         return c - 'A' + 10;
270                 return -1;
271         }
272
273         static int fromhex(String s) throws NumberFormatException {
274                 int c, v = 0;
275                 for (int i = 0; i < s.length(); i++) {
276                         c = s.charAt(i);
277                         if (!ishex(c)) {
278                                 if (i == 0)
279                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
280                                 return v;
281                         }
282                         v = v * 16 + fromhex(c);
283                 }
284                 return v;
285         }
286
287         static boolean isdec(int c) {
288                 if ('0' <= c && c <= '9')
289                         return true;
290                 return false;
291         }
292
293         static boolean isdec(String s) {
294                 for (int i = 0; i < s.length(); i++)
295                         if (!isdec(s.charAt(i)))
296                                 return false;
297                 return true;
298         }
299
300         static int fromdec(int c) {
301                 if ('0' <= c && c <= '9')
302                         return c - '0';
303                 return -1;
304         }
305
306         static int int8(int[] bytes, int i) {
307                 return (int) (byte) bytes[i];
308         }
309
310         static int uint8(int[] bytes, int i) {
311                 return bytes[i];
312         }
313
314         static int int16(int[] bytes, int i) {
315                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
316         }
317
318         static int uint16(int[] bytes, int i) {
319                 return bytes[i] + (bytes[i+1] << 8);
320         }
321
322         static int uint32(int[] bytes, int i) {
323                 return bytes[i] +
324                         (bytes[i+1] << 8) +
325                         (bytes[i+2] << 16) +
326                         (bytes[i+3] << 24);
327         }
328
329         static final Charset    unicode_set = Charset.forName("UTF-8");
330
331         static String string(int[] bytes, int s, int l) {
332                 if (s + l > bytes.length) {
333                         if (s > bytes.length) {
334                                 s = bytes.length;
335                                 l = 0;
336                         } else {
337                                 l = bytes.length - s;
338                         }
339                 }
340
341                 int i;
342                 for (i = l - 1; i >= 0; i--)
343                         if (bytes[s+i] != 0)
344                                 break;
345
346                 l = i + 1;
347                 byte[]  b = new byte[l];
348
349                 for (i = 0; i < l; i++)
350                         b[i] = (byte) bytes[s+i];
351                 String n = new String(b, unicode_set);
352                 return n;
353         }
354
355         static int hexbyte(String s, int i) {
356                 int c0, c1;
357
358                 if (s.length() < i + 2)
359                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
360                 c0 = s.charAt(i);
361                 if (!Altos.ishex(c0))
362                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
363                 c1 = s.charAt(i+1);
364                 if (!Altos.ishex(c1))
365                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
366                 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
367         }
368
369         static int[] hexbytes(String s) {
370                 int     n;
371                 int[]   r;
372                 int     i;
373
374                 if ((s.length() & 1) != 0)
375                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
376                 n = s.length() / 2;
377                 r = new int[n];
378                 for (i = 0; i < n; i++)
379                         r[i] = Altos.hexbyte(s, i * 2);
380                 return r;
381         }
382
383         static int fromdec(String s) throws NumberFormatException {
384                 int c, v = 0;
385                 int sign = 1;
386                 for (int i = 0; i < s.length(); i++) {
387                         c = s.charAt(i);
388                         if (i == 0 && c == '-') {
389                                 sign = -1;
390                         } else if (!isdec(c)) {
391                                 if (i == 0)
392                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
393                                 return v;
394                         } else
395                                 v = v * 10 + fromdec(c);
396                 }
397                 return v * sign;
398         }
399
400         static String replace_extension(String input, String extension) {
401                 int dot = input.lastIndexOf(".");
402                 if (dot > 0)
403                         input = input.substring(0,dot);
404                 return input.concat(extension);
405         }
406
407         static public boolean initialized = false;
408         static public boolean loaded_library = false;
409
410         public static boolean load_library() {
411                 if (!initialized) {
412                         try {
413                                 System.loadLibrary("altos");
414                                 libaltos.altos_init();
415                                 loaded_library = true;
416                         } catch (UnsatisfiedLinkError e) {
417                                 try {
418                                         System.loadLibrary("altos64");
419                                         libaltos.altos_init();
420                                         loaded_library = true;
421                                 } catch (UnsatisfiedLinkError e2) {
422                                         loaded_library = false;
423                                 }
424                         }
425                         initialized = true;
426                 }
427                 return loaded_library;
428         }
429
430         static int usb_vendor_altusmetrum() {
431                 load_library();
432                 return 0xfffe;
433         }
434
435         static int usb_product_altusmetrum() {
436                 load_library();
437                 return 0x000a;
438         }
439
440         static int usb_product_altusmetrum_min() {
441                 load_library();
442                 return 0x000a;
443         }
444
445         static int usb_product_altusmetrum_max() {
446                 load_library();
447                 return 0x0013;
448         }
449
450         static int usb_product_telemetrum() {
451                 load_library();
452                 return 0x000b;
453         }
454
455         static int usb_product_teledongle() {
456                 load_library();
457                 return 0x000c;
458         }
459
460         static int usb_product_teleterra() {
461                 load_library();
462                 return 0x000d;
463         }
464
465         static int usb_product_telebt() {
466                 load_library();
467                 return 0x000e;
468         }
469
470         static int usb_product_telelaunch() {
471                 load_library();
472                 return 0x000f;
473         }
474
475         static int usb_product_telelco() {
476                 load_library();
477                 return 0x0010;
478         }
479
480         static int usb_product_telescience() {
481                 load_library();
482                 return 0x0011;
483         }
484
485         static int usb_product_telepyro() {
486                 load_library();
487                 return 0x0012;
488         }
489
490         public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
491         public final static int product_altusmetrum = usb_product_altusmetrum();
492         public final static int product_telemetrum = usb_product_telemetrum();
493         public final static int product_teledongle = usb_product_teledongle();
494         public final static int product_teleterra = usb_product_teleterra();
495         public final static int product_telebt = usb_product_telebt();
496         public final static int product_telelaunch = usb_product_telelaunch();
497         public final static int product_tele10 = usb_product_telelco();
498         public final static int product_telescience = usb_product_telescience();
499         public final static int product_telepyro = usb_product_telepyro();
500         public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
501         public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
502
503         public final static int product_any = 0x10000;
504         public final static int product_basestation = 0x10000 + 1;
505
506         static String bt_product_telebt() {
507                 load_library();
508                 return "TeleBT";
509         }
510
511         public final static String bt_product_telebt = bt_product_telebt();
512
513         public static AltosBTKnown bt_known = new AltosBTKnown();
514 }