Altosui: Add flight statistics tab to graph window
[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_SOFTWARE_VERSION = 9999;
55
56         /* Added to flag invalid records */
57         static final int AO_LOG_INVALID = -1;
58
59         /* Flight state numbers and names */
60         static final int ao_flight_startup = 0;
61         static final int ao_flight_idle = 1;
62         static final int ao_flight_pad = 2;
63         static final int ao_flight_boost = 3;
64         static final int ao_flight_fast = 4;
65         static final int ao_flight_coast = 5;
66         static final int ao_flight_drogue = 6;
67         static final int ao_flight_main = 7;
68         static final int ao_flight_landed = 8;
69         static final int ao_flight_invalid = 9;
70
71         /* Telemetry modes */
72         static final int ao_telemetry_off = 0;
73         static final int ao_telemetry_min = 1;
74         static final int ao_telemetry_standard = 1;
75         static final int ao_telemetry_0_9 = 2;
76         static final int ao_telemetry_0_8 = 3;
77         static final int ao_telemetry_max = 3;
78
79         static final String[] ao_telemetry_name = {
80                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
81         };
82
83         static final int ao_telemetry_standard_len = 32;
84         static final int ao_telemetry_0_9_len = 95;
85         static final int ao_telemetry_0_8_len = 94;
86
87         static final int[] ao_telemetry_len = {
88                 0, 32, 95, 94
89         };
90
91         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
92
93         static boolean map_initialized = false;
94
95         static final int tab_elt_pad = 5;
96
97         static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
98         static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
99         static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
100
101         static final int text_width = 20;
102
103         static void initialize_map()
104         {
105                 string_to_state.put("startup", ao_flight_startup);
106                 string_to_state.put("idle", ao_flight_idle);
107                 string_to_state.put("pad", ao_flight_pad);
108                 string_to_state.put("boost", ao_flight_boost);
109                 string_to_state.put("fast", ao_flight_fast);
110                 string_to_state.put("coast", ao_flight_coast);
111                 string_to_state.put("drogue", ao_flight_drogue);
112                 string_to_state.put("main", ao_flight_main);
113                 string_to_state.put("landed", ao_flight_landed);
114                 string_to_state.put("invalid", ao_flight_invalid);
115                 map_initialized = true;
116         }
117
118         static int telemetry_len(int telemetry) {
119                 if (telemetry <= ao_telemetry_max)
120                         return ao_telemetry_len[telemetry];
121                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
122                                                                  telemetry));
123         }
124
125         static String telemetry_name(int telemetry) {
126                 if (telemetry <= ao_telemetry_max)
127                         return ao_telemetry_name[telemetry];
128                 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
129                                                                  telemetry));
130         }
131         
132         static String[] state_to_string = {
133                 "startup",
134                 "idle",
135                 "pad",
136                 "boost",
137                 "fast",
138                 "coast",
139                 "drogue",
140                 "main",
141                 "landed",
142                 "invalid",
143         };
144
145         static String[] state_to_string_capital = {
146                 "Startup",
147                 "Idle",
148                 "Pad",
149                 "Boost",
150                 "Fast",
151                 "Coast",
152                 "Drogue",
153                 "Main",
154                 "Landed",
155                 "Invalid",
156         };
157
158         static public int state(String state) {
159                 if (!map_initialized)
160                         initialize_map();
161                 if (string_to_state.containsKey(state))
162                         return string_to_state.get(state);
163                 return ao_flight_invalid;
164         }
165
166         static public String state_name(int state) {
167                 if (state < 0 || state_to_string.length <= state)
168                         return "invalid";
169                 return state_to_string[state];
170         }
171
172         static final int AO_GPS_VALID = (1 << 4);
173         static final int AO_GPS_RUNNING = (1 << 5);
174         static final int AO_GPS_DATE_VALID = (1 << 6);
175         static final int AO_GPS_NUM_SAT_SHIFT = 0;
176         static final int AO_GPS_NUM_SAT_MASK = 0xf;
177
178         static boolean isspace(int c) {
179                 switch (c) {
180                 case ' ':
181                 case '\t':
182                         return true;
183                 }
184                 return false;
185         }
186
187         static boolean ishex(int c) {
188                 if ('0' <= c && c <= '9')
189                         return true;
190                 if ('a' <= c && c <= 'f')
191                         return true;
192                 if ('A' <= c && c <= 'F')
193                         return true;
194                 return false;
195         }
196
197         static boolean ishex(String s) {
198                 for (int i = 0; i < s.length(); i++)
199                         if (!ishex(s.charAt(i)))
200                                 return false;
201                 return true;
202         }
203
204         static int fromhex(int c) {
205                 if ('0' <= c && c <= '9')
206                         return c - '0';
207                 if ('a' <= c && c <= 'f')
208                         return c - 'a' + 10;
209                 if ('A' <= c && c <= 'F')
210                         return c - 'A' + 10;
211                 return -1;
212         }
213
214         static int fromhex(String s) throws NumberFormatException {
215                 int c, v = 0;
216                 for (int i = 0; i < s.length(); i++) {
217                         c = s.charAt(i);
218                         if (!ishex(c)) {
219                                 if (i == 0)
220                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
221                                 return v;
222                         }
223                         v = v * 16 + fromhex(c);
224                 }
225                 return v;
226         }
227
228         static boolean isdec(int c) {
229                 if ('0' <= c && c <= '9')
230                         return true;
231                 return false;
232         }
233
234         static boolean isdec(String s) {
235                 for (int i = 0; i < s.length(); i++)
236                         if (!isdec(s.charAt(i)))
237                                 return false;
238                 return true;
239         }
240
241         static int fromdec(int c) {
242                 if ('0' <= c && c <= '9')
243                         return c - '0';
244                 return -1;
245         }
246
247         static int int8(int[] bytes, int i) {
248                 return (int) (byte) bytes[i];
249         }
250
251         static int uint8(int[] bytes, int i) {
252                 return bytes[i];
253         }
254
255         static int int16(int[] bytes, int i) {
256                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
257         }
258
259         static int uint16(int[] bytes, int i) {
260                 return bytes[i] + (bytes[i+1] << 8);
261         }
262
263         static int uint32(int[] bytes, int i) {
264                 return bytes[i] +
265                         (bytes[i+1] << 8) +
266                         (bytes[i+2] << 16) +
267                         (bytes[i+3] << 24);
268         }
269
270         static final Charset    unicode_set = Charset.forName("UTF-8");
271
272         static String string(int[] bytes, int s, int l) {
273                 if (s + l > bytes.length) {
274                         if (s > bytes.length) {
275                                 s = bytes.length;
276                                 l = 0;
277                         } else {
278                                 l = bytes.length - s;
279                         }
280                 }
281
282                 int i;
283                 for (i = l - 1; i >= 0; i--)
284                         if (bytes[s+i] != 0)
285                                 break;
286
287                 l = i + 1;
288                 byte[]  b = new byte[l];
289
290                 for (i = 0; i < l; i++)
291                         b[i] = (byte) bytes[s+i];
292                 String n = new String(b, unicode_set);
293                 return n;
294         }
295
296         static int hexbyte(String s, int i) {
297                 int c0, c1;
298
299                 if (s.length() < i + 2)
300                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
301                 c0 = s.charAt(i);
302                 if (!Altos.ishex(c0))
303                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
304                 c1 = s.charAt(i+1);
305                 if (!Altos.ishex(c1))
306                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
307                 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
308         }
309
310         static int[] hexbytes(String s) {
311                 int     n;
312                 int[]   r;
313                 int     i;
314
315                 if ((s.length() & 1) != 0)
316                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
317                 n = s.length() / 2;
318                 r = new int[n];
319                 for (i = 0; i < n; i++)
320                         r[i] = Altos.hexbyte(s, i * 2);
321                 return r;
322         }
323
324         static int fromdec(String s) throws NumberFormatException {
325                 int c, v = 0;
326                 int sign = 1;
327                 for (int i = 0; i < s.length(); i++) {
328                         c = s.charAt(i);
329                         if (i == 0 && c == '-') {
330                                 sign = -1;
331                         } else if (!isdec(c)) {
332                                 if (i == 0)
333                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
334                                 return v;
335                         } else
336                                 v = v * 10 + fromdec(c);
337                 }
338                 return v * sign;
339         }
340
341         static String replace_extension(String input, String extension) {
342                 int dot = input.lastIndexOf(".");
343                 if (dot > 0)
344                         input = input.substring(0,dot);
345                 return input.concat(extension);
346         }
347
348         static public boolean initialized = false;
349         static public boolean loaded_library = false;
350
351         public static boolean load_library() {
352                 if (!initialized) {
353                         try {
354                                 System.loadLibrary("altos");
355                                 libaltos.altos_init();
356                                 loaded_library = true;
357                         } catch (UnsatisfiedLinkError e) {
358                                 loaded_library = false;
359                         }
360                         initialized = true;
361                 }
362                 return loaded_library;
363         }
364
365         static int usb_vendor_altusmetrum() {
366                 load_library();
367                 return 0xfffe;
368         }
369
370         static int usb_product_altusmetrum() {
371                 load_library();
372                 return 0x000a;
373         }
374
375         static int usb_product_altusmetrum_min() {
376                 load_library();
377                 return 0x000a;
378         }
379
380         static int usb_product_altusmetrum_max() {
381                 load_library();
382                 return 0x0013;
383         }
384
385         static int usb_product_telemetrum() {
386                 load_library();
387                 return 0x000b;
388         }
389
390         static int usb_product_teledongle() {
391                 load_library();
392                 return 0x000c;
393         }
394
395         static int usb_product_teleterra() {
396                 load_library();
397                 return 0x000d;
398         }
399
400         static int usb_product_telebt() {
401                 load_library();
402                 return 0x000e;
403         }
404
405         public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
406         public final static int product_altusmetrum = usb_product_altusmetrum();
407         public final static int product_telemetrum = usb_product_telemetrum();
408         public final static int product_teledongle = usb_product_teledongle();
409         public final static int product_teleterra = usb_product_teleterra();
410         public final static int product_telebt = usb_product_telebt();
411         public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
412         public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
413
414         public final static int product_any = 0x10000;
415         public final static int product_basestation = 0x10000 + 1;
416
417         static String bt_product_telebt() {
418                 load_library();
419                 return "TeleBT";
420         }
421
422         public final static String bt_product_telebt = bt_product_telebt();
423
424 //      public static AltosBTKnown bt_known = new AltosBTKnown();
425 }