1acce9492a4c96b08bdbef5f8de6719f514be35e
[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 public int state(String state) {
146                 if (!map_initialized)
147                         initialize_map();
148                 if (string_to_state.containsKey(state))
149                         return string_to_state.get(state);
150                 return ao_flight_invalid;
151         }
152
153         static public String state_name(int state) {
154                 if (state < 0 || state_to_string.length <= state)
155                         return "invalid";
156                 return state_to_string[state];
157         }
158
159         static final int AO_GPS_VALID = (1 << 4);
160         static final int AO_GPS_RUNNING = (1 << 5);
161         static final int AO_GPS_DATE_VALID = (1 << 6);
162         static final int AO_GPS_NUM_SAT_SHIFT = 0;
163         static final int AO_GPS_NUM_SAT_MASK = 0xf;
164
165         static boolean isspace(int c) {
166                 switch (c) {
167                 case ' ':
168                 case '\t':
169                         return true;
170                 }
171                 return false;
172         }
173
174         static boolean ishex(int c) {
175                 if ('0' <= c && c <= '9')
176                         return true;
177                 if ('a' <= c && c <= 'f')
178                         return true;
179                 if ('A' <= c && c <= 'F')
180                         return true;
181                 return false;
182         }
183
184         static boolean ishex(String s) {
185                 for (int i = 0; i < s.length(); i++)
186                         if (!ishex(s.charAt(i)))
187                                 return false;
188                 return true;
189         }
190
191         static int fromhex(int c) {
192                 if ('0' <= c && c <= '9')
193                         return c - '0';
194                 if ('a' <= c && c <= 'f')
195                         return c - 'a' + 10;
196                 if ('A' <= c && c <= 'F')
197                         return c - 'A' + 10;
198                 return -1;
199         }
200
201         static int fromhex(String s) throws NumberFormatException {
202                 int c, v = 0;
203                 for (int i = 0; i < s.length(); i++) {
204                         c = s.charAt(i);
205                         if (!ishex(c)) {
206                                 if (i == 0)
207                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
208                                 return v;
209                         }
210                         v = v * 16 + fromhex(c);
211                 }
212                 return v;
213         }
214
215         static boolean isdec(int c) {
216                 if ('0' <= c && c <= '9')
217                         return true;
218                 return false;
219         }
220
221         static boolean isdec(String s) {
222                 for (int i = 0; i < s.length(); i++)
223                         if (!isdec(s.charAt(i)))
224                                 return false;
225                 return true;
226         }
227
228         static int fromdec(int c) {
229                 if ('0' <= c && c <= '9')
230                         return c - '0';
231                 return -1;
232         }
233
234         static int int8(int[] bytes, int i) {
235                 return (int) (byte) bytes[i];
236         }
237
238         static int uint8(int[] bytes, int i) {
239                 return bytes[i];
240         }
241
242         static int int16(int[] bytes, int i) {
243                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
244         }
245
246         static int uint16(int[] bytes, int i) {
247                 return bytes[i] + (bytes[i+1] << 8);
248         }
249
250         static int uint32(int[] bytes, int i) {
251                 return bytes[i] +
252                         (bytes[i+1] << 8) +
253                         (bytes[i+2] << 16) +
254                         (bytes[i+3] << 24);
255         }
256
257         static final Charset    unicode_set = Charset.forName("UTF-8");
258
259         static String string(int[] bytes, int s, int l) {
260                 if (s + l > bytes.length) {
261                         if (s > bytes.length) {
262                                 s = bytes.length;
263                                 l = 0;
264                         } else {
265                                 l = bytes.length - s;
266                         }
267                 }
268
269                 int i;
270                 for (i = l - 1; i >= 0; i--)
271                         if (bytes[s+i] != 0)
272                                 break;
273
274                 l = i + 1;
275                 byte[]  b = new byte[l];
276
277                 for (i = 0; i < l; i++)
278                         b[i] = (byte) bytes[s+i];
279                 String n = new String(b, unicode_set);
280                 return n;
281         }
282
283         static int hexbyte(String s, int i) {
284                 int c0, c1;
285
286                 if (s.length() < i + 2)
287                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
288                 c0 = s.charAt(i);
289                 if (!Altos.ishex(c0))
290                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
291                 c1 = s.charAt(i+1);
292                 if (!Altos.ishex(c1))
293                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
294                 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
295         }
296
297         static int[] hexbytes(String s) {
298                 int     n;
299                 int[]   r;
300                 int     i;
301
302                 if ((s.length() & 1) != 0)
303                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
304                 n = s.length() / 2;
305                 r = new int[n];
306                 for (i = 0; i < n; i++)
307                         r[i] = Altos.hexbyte(s, i * 2);
308                 return r;
309         }
310
311         static int fromdec(String s) throws NumberFormatException {
312                 int c, v = 0;
313                 int sign = 1;
314                 for (int i = 0; i < s.length(); i++) {
315                         c = s.charAt(i);
316                         if (i == 0 && c == '-') {
317                                 sign = -1;
318                         } else if (!isdec(c)) {
319                                 if (i == 0)
320                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
321                                 return v;
322                         } else
323                                 v = v * 10 + fromdec(c);
324                 }
325                 return v * sign;
326         }
327
328         static String replace_extension(String input, String extension) {
329                 int dot = input.lastIndexOf(".");
330                 if (dot > 0)
331                         input = input.substring(0,dot);
332                 return input.concat(extension);
333         }
334
335         static public boolean initialized = false;
336         static public boolean loaded_library = false;
337
338         public static boolean load_library() {
339                 if (!initialized) {
340                         try {
341                                 System.loadLibrary("altos");
342                                 libaltos.altos_init();
343                                 loaded_library = true;
344                         } catch (UnsatisfiedLinkError e) {
345                                 loaded_library = false;
346                         }
347                         initialized = true;
348                 }
349                 return loaded_library;
350         }
351
352         static int usb_vendor_altusmetrum() {
353                 load_library();
354                 return 0xfffe;
355         }
356
357         static int usb_product_altusmetrum() {
358                 load_library();
359                 return 0x000a;
360         }
361
362         static int usb_product_altusmetrum_min() {
363                 load_library();
364                 return 0x000a;
365         }
366
367         static int usb_product_altusmetrum_max() {
368                 load_library();
369                 return 0x0013;
370         }
371
372         static int usb_product_telemetrum() {
373                 load_library();
374                 return 0x000b;
375         }
376
377         static int usb_product_teledongle() {
378                 load_library();
379                 return 0x000c;
380         }
381
382         static int usb_product_teleterra() {
383                 load_library();
384                 return 0x000d;
385         }
386
387         static int usb_product_telebt() {
388                 load_library();
389                 return 0x000e;
390         }
391
392         public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
393         public final static int product_altusmetrum = usb_product_altusmetrum();
394         public final static int product_telemetrum = usb_product_telemetrum();
395         public final static int product_teledongle = usb_product_teledongle();
396         public final static int product_teleterra = usb_product_teleterra();
397         public final static int product_telebt = usb_product_telebt();
398         public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
399         public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
400
401         public final static int product_any = 0x10000;
402         public final static int product_basestation = 0x10000 + 1;
403
404         static String bt_product_telebt() {
405                 load_library();
406                 return "TeleBT";
407         }
408
409         public final static String bt_product_telebt = bt_product_telebt();
410
411         public static AltosBTKnown bt_known = new AltosBTKnown();
412 }