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