altos: Rename telemetry to telemetry_orig
[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
24 import libaltosJNI.*;
25
26 public class Altos {
27         /* EEProm command letters */
28         static final int AO_LOG_FLIGHT = 'F';
29         static final int AO_LOG_SENSOR = 'A';
30         static final int AO_LOG_TEMP_VOLT = 'T';
31         static final int AO_LOG_DEPLOY = 'D';
32         static final int AO_LOG_STATE = 'S';
33         static final int AO_LOG_GPS_TIME = 'G';
34         static final int AO_LOG_GPS_LAT = 'N';
35         static final int AO_LOG_GPS_LON = 'W';
36         static final int AO_LOG_GPS_ALT = 'H';
37         static final int AO_LOG_GPS_SAT = 'V';
38         static final int AO_LOG_GPS_DATE = 'Y';
39         static final int AO_LOG_PRESSURE = 'P';
40
41         /* Added for header fields in eeprom files */
42         static final int AO_LOG_CONFIG_VERSION = 1000;
43         static final int AO_LOG_MAIN_DEPLOY = 1001;
44         static final int AO_LOG_APOGEE_DELAY = 1002;
45         static final int AO_LOG_RADIO_CHANNEL = 1003;
46         static final int AO_LOG_CALLSIGN = 1004;
47         static final int AO_LOG_ACCEL_CAL = 1005;
48         static final int AO_LOG_RADIO_CAL = 1006;
49         static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
50         static final int AO_LOG_MANUFACTURER = 2000;
51         static final int AO_LOG_PRODUCT = 2001;
52         static final int AO_LOG_SERIAL_NUMBER = 2002;
53         static final int AO_LOG_SOFTWARE_VERSION = 9999;
54
55         /* Added to flag invalid records */
56         static final int AO_LOG_INVALID = -1;
57
58         /* Flight state numbers and names */
59         static final int ao_flight_startup = 0;
60         static final int ao_flight_idle = 1;
61         static final int ao_flight_pad = 2;
62         static final int ao_flight_boost = 3;
63         static final int ao_flight_fast = 4;
64         static final int ao_flight_coast = 5;
65         static final int ao_flight_drogue = 6;
66         static final int ao_flight_main = 7;
67         static final int ao_flight_landed = 8;
68         static final int ao_flight_invalid = 9;
69
70         /* Telemetry modes */
71         static final int ao_telemetry_off = 0;
72         static final int ao_telemetry_full = 1;
73         static final int ao_telemetry_tiny = 2;
74
75         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
76
77         static boolean map_initialized = false;
78
79         static final int tab_elt_pad = 5;
80
81         static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
82         static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
83         static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
84
85         static final int text_width = 16;
86
87         static void initialize_map()
88         {
89                 string_to_state.put("startup", ao_flight_startup);
90                 string_to_state.put("idle", ao_flight_idle);
91                 string_to_state.put("pad", ao_flight_pad);
92                 string_to_state.put("boost", ao_flight_boost);
93                 string_to_state.put("fast", ao_flight_fast);
94                 string_to_state.put("coast", ao_flight_coast);
95                 string_to_state.put("drogue", ao_flight_drogue);
96                 string_to_state.put("main", ao_flight_main);
97                 string_to_state.put("landed", ao_flight_landed);
98                 string_to_state.put("invalid", ao_flight_invalid);
99                 map_initialized = true;
100         }
101
102         static String[] state_to_string = {
103                 "startup",
104                 "idle",
105                 "pad",
106                 "boost",
107                 "fast",
108                 "coast",
109                 "drogue",
110                 "main",
111                 "landed",
112                 "invalid",
113         };
114
115         static public int state(String state) {
116                 if (!map_initialized)
117                         initialize_map();
118                 if (string_to_state.containsKey(state))
119                         return string_to_state.get(state);
120                 return ao_flight_invalid;
121         }
122
123         static public String state_name(int state) {
124                 if (state < 0 || state_to_string.length <= state)
125                         return "invalid";
126                 return state_to_string[state];
127         }
128
129         static final int AO_GPS_VALID = (1 << 4);
130         static final int AO_GPS_RUNNING = (1 << 5);
131         static final int AO_GPS_DATE_VALID = (1 << 6);
132         static final int AO_GPS_NUM_SAT_SHIFT = 0;
133         static final int AO_GPS_NUM_SAT_MASK = 0xf;
134
135         static boolean isspace(int c) {
136                 switch (c) {
137                 case ' ':
138                 case '\t':
139                         return true;
140                 }
141                 return false;
142         }
143
144         static boolean ishex(int c) {
145                 if ('0' <= c && c <= '9')
146                         return true;
147                 if ('a' <= c && c <= 'f')
148                         return true;
149                 if ('A' <= c && c <= 'F')
150                         return true;
151                 return false;
152         }
153
154         static boolean ishex(String s) {
155                 for (int i = 0; i < s.length(); i++)
156                         if (!ishex(s.charAt(i)))
157                                 return false;
158                 return true;
159         }
160
161         static int fromhex(int c) {
162                 if ('0' <= c && c <= '9')
163                         return c - '0';
164                 if ('a' <= c && c <= 'f')
165                         return c - 'a' + 10;
166                 if ('A' <= c && c <= 'F')
167                         return c - 'A' + 10;
168                 return -1;
169         }
170
171         static int fromhex(String s) throws NumberFormatException {
172                 int c, v = 0;
173                 for (int i = 0; i < s.length(); i++) {
174                         c = s.charAt(i);
175                         if (!ishex(c)) {
176                                 if (i == 0)
177                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
178                                 return v;
179                         }
180                         v = v * 16 + fromhex(c);
181                 }
182                 return v;
183         }
184
185         static boolean isdec(int c) {
186                 if ('0' <= c && c <= '9')
187                         return true;
188                 return false;
189         }
190
191         static boolean isdec(String s) {
192                 for (int i = 0; i < s.length(); i++)
193                         if (!isdec(s.charAt(i)))
194                                 return false;
195                 return true;
196         }
197
198         static int fromdec(int c) {
199                 if ('0' <= c && c <= '9')
200                         return c - '0';
201                 return -1;
202         }
203
204         static int fromdec(String s) throws NumberFormatException {
205                 int c, v = 0;
206                 int sign = 1;
207                 for (int i = 0; i < s.length(); i++) {
208                         c = s.charAt(i);
209                         if (i == 0 && c == '-') {
210                                 sign = -1;
211                         } else if (!isdec(c)) {
212                                 if (i == 0)
213                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
214                                 return v;
215                         } else
216                                 v = v * 10 + fromdec(c);
217                 }
218                 return v * sign;
219         }
220
221         static String replace_extension(String input, String extension) {
222                 int dot = input.lastIndexOf(".");
223                 if (dot > 0)
224                         input = input.substring(0,dot);
225                 return input.concat(extension);
226         }
227
228         static public boolean initialized = false;
229         static public boolean loaded_library = false;
230
231         public static boolean load_library() {
232                 if (!initialized) {
233                         try {
234                                 System.loadLibrary("altos");
235                                 libaltos.altos_init();
236                                 loaded_library = true;
237                         } catch (UnsatisfiedLinkError e) {
238                                 loaded_library = false;
239                         }
240                         initialized = true;
241                 }
242                 return loaded_library;
243         }
244
245         static int usb_vendor_altusmetrum() {
246                 if (load_library())
247                         return libaltosConstants.USB_VENDOR_ALTUSMETRUM;
248                 return 0x000a;
249         }
250
251         static int usb_product_altusmetrum() {
252                 if (load_library())
253                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM;
254                 return 0x000a;
255         }
256
257         static int usb_product_altusmetrum_min() {
258                 if (load_library())
259                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MIN;
260                 return 0x000a;
261         }
262
263         static int usb_product_altusmetrum_max() {
264                 if (load_library())
265                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MAX;
266                 return 0x000d;
267         }
268
269         static int usb_product_telemetrum() {
270                 if (load_library())
271                         return libaltosConstants.USB_PRODUCT_TELEMETRUM;
272                 return 0x000b;
273         }
274
275         static int usb_product_teledongle() {
276                 if (load_library())
277                         return libaltosConstants.USB_PRODUCT_TELEDONGLE;
278                 return 0x000c;
279         }
280
281         static int usb_product_teleterra() {
282                 if (load_library())
283                         return libaltosConstants.USB_PRODUCT_TELETERRA;
284                 return 0x000d;
285         }
286
287         static int usb_product_telebt() {
288                 if (load_library())
289                         return libaltosConstants.USB_PRODUCT_TELEBT;
290                 return 0x000e;
291         }
292
293         public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
294         public final static int product_altusmetrum = usb_product_altusmetrum();
295         public final static int product_telemetrum = usb_product_telemetrum();
296         public final static int product_teledongle = usb_product_teledongle();
297         public final static int product_teleterra = usb_product_teleterra();
298         public final static int product_telebt = usb_product_telebt();
299         public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
300         public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
301
302         public final static int product_any = 0x10000;
303         public final static int product_basestation = 0x10000 + 1;
304
305         static String bt_product_telebt() {
306                 if (load_library())
307                         return libaltosConstants.BLUETOOTH_PRODUCT_TELEBT;
308                 return "TeleBT";
309         }
310
311         public final static String bt_product_telebt = bt_product_telebt();
312
313         public static AltosBTKnown bt_known = new AltosBTKnown();
314 }