2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
23 import java.nio.charset.Charset;
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';
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_LOG_FORMAT = 2003;
56 /* Added for header fields in megametrum files */
57 static final int AO_LOG_BARO_RESERVED = 3000;
58 static final int AO_LOG_BARO_SENS = 3001;
59 static final int AO_LOG_BARO_OFF = 3002;
60 static final int AO_LOG_BARO_TCS = 3004;
61 static final int AO_LOG_BARO_TCO = 3005;
62 static final int AO_LOG_BARO_TREF = 3006;
63 static final int AO_LOG_BARO_TEMPSENS = 3007;
64 static final int AO_LOG_BARO_CRC = 3008;
66 static final int AO_LOG_SOFTWARE_VERSION = 9999;
68 /* Added to flag invalid records */
69 static final int AO_LOG_INVALID = -1;
71 /* Flight state numbers and names */
72 static final int ao_flight_startup = 0;
73 static final int ao_flight_idle = 1;
74 static final int ao_flight_pad = 2;
75 static final int ao_flight_boost = 3;
76 static final int ao_flight_fast = 4;
77 static final int ao_flight_coast = 5;
78 static final int ao_flight_drogue = 6;
79 static final int ao_flight_main = 7;
80 static final int ao_flight_landed = 8;
81 static final int ao_flight_invalid = 9;
84 static final int ao_telemetry_off = 0;
85 static final int ao_telemetry_min = 1;
86 static final int ao_telemetry_standard = 1;
87 static final int ao_telemetry_0_9 = 2;
88 static final int ao_telemetry_0_8 = 3;
89 static final int ao_telemetry_max = 3;
91 static final String[] ao_telemetry_name = {
92 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
95 static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
97 static final int ao_telemetry_standard_len = 32;
98 static final int ao_telemetry_0_9_len = 95;
99 static final int ao_telemetry_0_8_len = 94;
101 static final int[] ao_telemetry_len = {
105 static HashMap<String,Integer> string_to_state = new HashMap<String,Integer>();
107 static boolean map_initialized = false;
109 static final int tab_elt_pad = 5;
111 static Font label_font;
112 static Font value_font;
113 static Font status_font;
114 static Font table_label_font;
115 static Font table_value_font;
117 final static int font_size_small = 1;
118 final static int font_size_medium = 2;
119 final static int font_size_large = 3;
121 static void set_fonts(int size) {
127 case font_size_small:
133 case font_size_medium:
138 case font_size_large:
144 label_font = new Font("Dialog", Font.PLAIN, brief_size);
145 value_font = new Font("Monospaced", Font.PLAIN, brief_size);
146 status_font = new Font("SansSerif", Font.BOLD, status_size);
147 table_label_font = new Font("SansSerif", Font.PLAIN, table_size);
148 table_value_font = new Font("Monospaced", Font.PLAIN, table_size);
151 static final int text_width = 20;
153 static void initialize_map()
155 string_to_state.put("startup", ao_flight_startup);
156 string_to_state.put("idle", ao_flight_idle);
157 string_to_state.put("pad", ao_flight_pad);
158 string_to_state.put("boost", ao_flight_boost);
159 string_to_state.put("fast", ao_flight_fast);
160 string_to_state.put("coast", ao_flight_coast);
161 string_to_state.put("drogue", ao_flight_drogue);
162 string_to_state.put("apogee", ao_flight_coast);
163 string_to_state.put("main", ao_flight_main);
164 string_to_state.put("landed", ao_flight_landed);
165 string_to_state.put("invalid", ao_flight_invalid);
166 map_initialized = true;
169 static int telemetry_len(int telemetry) {
170 if (telemetry <= ao_telemetry_max)
171 return ao_telemetry_len[telemetry];
172 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
176 static String telemetry_name(int telemetry) {
177 if (telemetry <= ao_telemetry_max)
178 return ao_telemetry_name[telemetry];
179 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
183 static String[] state_to_string = {
196 static String[] state_to_string_capital = {
209 static public int state(String state) {
210 if (!map_initialized)
212 if (string_to_state.containsKey(state))
213 return string_to_state.get(state);
214 return ao_flight_invalid;
217 static public String state_name(int state) {
218 if (state < 0 || state_to_string.length <= state)
220 return state_to_string[state];
223 static final int AO_GPS_VALID = (1 << 4);
224 static final int AO_GPS_RUNNING = (1 << 5);
225 static final int AO_GPS_DATE_VALID = (1 << 6);
226 static final int AO_GPS_NUM_SAT_SHIFT = 0;
227 static final int AO_GPS_NUM_SAT_MASK = 0xf;
229 static final int AO_LOG_FORMAT_UNKNOWN = 0;
230 static final int AO_LOG_FORMAT_FULL = 1;
231 static final int AO_LOG_FORMAT_TINY = 2;
232 static final int AO_LOG_FORMAT_TELEMETRY = 3;
233 static final int AO_LOG_FORMAT_TELESCIENCE = 4;
234 static final int AO_LOG_FORMAT_MEGAMETRUM = 5;
235 static final int AO_LOG_FORMAT_NONE = 127;
237 static boolean isspace(int c) {
246 static boolean ishex(int c) {
247 if ('0' <= c && c <= '9')
249 if ('a' <= c && c <= 'f')
251 if ('A' <= c && c <= 'F')
256 static boolean ishex(String s) {
257 for (int i = 0; i < s.length(); i++)
258 if (!ishex(s.charAt(i)))
263 static int fromhex(int c) {
264 if ('0' <= c && c <= '9')
266 if ('a' <= c && c <= 'f')
268 if ('A' <= c && c <= 'F')
273 static int fromhex(String s) throws NumberFormatException {
275 for (int i = 0; i < s.length(); i++) {
279 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
282 v = v * 16 + fromhex(c);
287 static boolean isdec(int c) {
288 if ('0' <= c && c <= '9')
293 static boolean isdec(String s) {
294 for (int i = 0; i < s.length(); i++)
295 if (!isdec(s.charAt(i)))
300 static int fromdec(int c) {
301 if ('0' <= c && c <= '9')
306 static int int8(int[] bytes, int i) {
307 return (int) (byte) bytes[i];
310 static int uint8(int[] bytes, int i) {
314 static int int16(int[] bytes, int i) {
315 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
318 static int uint16(int[] bytes, int i) {
319 return bytes[i] + (bytes[i+1] << 8);
322 static int uint32(int[] bytes, int i) {
329 static final Charset unicode_set = Charset.forName("UTF-8");
331 static String string(int[] bytes, int s, int l) {
332 if (s + l > bytes.length) {
333 if (s > bytes.length) {
337 l = bytes.length - s;
342 for (i = l - 1; i >= 0; i--)
347 byte[] b = new byte[l];
349 for (i = 0; i < l; i++)
350 b[i] = (byte) bytes[s+i];
351 String n = new String(b, unicode_set);
355 static int hexbyte(String s, int i) {
358 if (s.length() < i + 2)
359 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
361 if (!Altos.ishex(c0))
362 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
364 if (!Altos.ishex(c1))
365 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
366 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
369 static int[] hexbytes(String s) {
374 if ((s.length() & 1) != 0)
375 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
378 for (i = 0; i < n; i++)
379 r[i] = Altos.hexbyte(s, i * 2);
383 static int fromdec(String s) throws NumberFormatException {
386 for (int i = 0; i < s.length(); i++) {
388 if (i == 0 && c == '-') {
390 } else if (!isdec(c)) {
392 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
395 v = v * 10 + fromdec(c);
400 static String replace_extension(String input, String extension) {
401 int dot = input.lastIndexOf(".");
403 input = input.substring(0,dot);
404 return input.concat(extension);
407 static public boolean initialized = false;
408 static public boolean loaded_library = false;
410 public static boolean load_library() {
413 System.loadLibrary("altos");
414 libaltos.altos_init();
415 loaded_library = true;
416 } catch (UnsatisfiedLinkError e) {
418 System.loadLibrary("altos64");
419 libaltos.altos_init();
420 loaded_library = true;
421 } catch (UnsatisfiedLinkError e2) {
422 loaded_library = false;
427 return loaded_library;
430 static int usb_vendor_altusmetrum() {
435 static int usb_product_altusmetrum() {
440 static int usb_product_altusmetrum_min() {
445 static int usb_product_altusmetrum_max() {
450 static int usb_product_telemetrum() {
455 static int usb_product_teledongle() {
460 static int usb_product_teleterra() {
465 static int usb_product_telebt() {
470 static int usb_product_telelaunch() {
475 static int usb_product_telelco() {
480 static int usb_product_telescience() {
485 static int usb_product_telepyro() {
490 public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
491 public final static int product_altusmetrum = usb_product_altusmetrum();
492 public final static int product_telemetrum = usb_product_telemetrum();
493 public final static int product_teledongle = usb_product_teledongle();
494 public final static int product_teleterra = usb_product_teleterra();
495 public final static int product_telebt = usb_product_telebt();
496 public final static int product_telelaunch = usb_product_telelaunch();
497 public final static int product_tele10 = usb_product_telelco();
498 public final static int product_telescience = usb_product_telescience();
499 public final static int product_telepyro = usb_product_telepyro();
500 public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
501 public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
503 public final static int product_any = 0x10000;
504 public final static int product_basestation = 0x10000 + 1;
506 static String bt_product_telebt() {
511 public final static String bt_product_telebt = bt_product_telebt();
513 public static AltosBTKnown bt_known = new AltosBTKnown();