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