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