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