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