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_SOFTWARE_VERSION = 9999;
56 /* Added to flag invalid records */
57 static final int AO_LOG_INVALID = -1;
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;
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;
79 static final String[] ao_telemetry_name = {
80 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
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;
87 static final int[] ao_telemetry_len = {
91 static HashMap<String,Integer> string_to_state = new HashMap<String,Integer>();
93 static boolean map_initialized = false;
95 static final int tab_elt_pad = 5;
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);
101 static final int text_width = 16;
103 static void initialize_map()
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;
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",
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",
132 static String[] state_to_string = {
145 static public int state(String state) {
146 if (!map_initialized)
148 if (string_to_state.containsKey(state))
149 return string_to_state.get(state);
150 return ao_flight_invalid;
153 static public String state_name(int state) {
154 if (state < 0 || state_to_string.length <= state)
156 return state_to_string[state];
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;
165 static boolean isspace(int c) {
174 static boolean ishex(int c) {
175 if ('0' <= c && c <= '9')
177 if ('a' <= c && c <= 'f')
179 if ('A' <= c && c <= 'F')
184 static boolean ishex(String s) {
185 for (int i = 0; i < s.length(); i++)
186 if (!ishex(s.charAt(i)))
191 static int fromhex(int c) {
192 if ('0' <= c && c <= '9')
194 if ('a' <= c && c <= 'f')
196 if ('A' <= c && c <= 'F')
201 static int fromhex(String s) throws NumberFormatException {
203 for (int i = 0; i < s.length(); i++) {
207 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
210 v = v * 16 + fromhex(c);
215 static boolean isdec(int c) {
216 if ('0' <= c && c <= '9')
221 static boolean isdec(String s) {
222 for (int i = 0; i < s.length(); i++)
223 if (!isdec(s.charAt(i)))
228 static int fromdec(int c) {
229 if ('0' <= c && c <= '9')
234 static int int8(int[] bytes, int i) {
235 return (int) (byte) bytes[i];
238 static int uint8(int[] bytes, int i) {
242 static int int16(int[] bytes, int i) {
243 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
246 static int uint16(int[] bytes, int i) {
247 return bytes[i] + (bytes[i+1] << 8);
250 static int uint32(int[] bytes, int i) {
257 static final Charset unicode_set = Charset.forName("UTF-8");
259 static String string(int[] bytes, int s, int l) {
260 if (s + l > bytes.length) {
261 if (s > bytes.length) {
265 l = bytes.length - s;
270 for (i = l - 1; i >= 0; i--)
275 byte[] b = new byte[l];
277 for (i = 0; i < l; i++)
278 b[i] = (byte) bytes[s+i];
279 String n = new String(b, unicode_set);
283 static int hexbyte(String s, int i) {
286 if (s.length() < i + 2)
287 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
289 if (!Altos.ishex(c0))
290 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
292 if (!Altos.ishex(c1))
293 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
294 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
297 static int[] hexbytes(String s) {
302 if ((s.length() & 1) != 0)
303 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
306 for (i = 0; i < n; i++)
307 r[i] = Altos.hexbyte(s, i * 2);
311 static int fromdec(String s) throws NumberFormatException {
314 for (int i = 0; i < s.length(); i++) {
316 if (i == 0 && c == '-') {
318 } else if (!isdec(c)) {
320 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
323 v = v * 10 + fromdec(c);
328 static String replace_extension(String input, String extension) {
329 int dot = input.lastIndexOf(".");
331 input = input.substring(0,dot);
332 return input.concat(extension);
335 static public boolean initialized = false;
336 static public boolean loaded_library = false;
338 public static boolean load_library() {
341 System.loadLibrary("altos");
342 libaltos.altos_init();
343 loaded_library = true;
344 } catch (UnsatisfiedLinkError e) {
345 loaded_library = false;
349 return loaded_library;
352 static int usb_vendor_altusmetrum() {
357 static int usb_product_altusmetrum() {
362 static int usb_product_altusmetrum_min() {
367 static int usb_product_altusmetrum_max() {
372 static int usb_product_telemetrum() {
377 static int usb_product_teledongle() {
382 static int usb_product_teleterra() {
387 static int usb_product_telebt() {
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();
401 public final static int product_any = 0x10000;
402 public final static int product_basestation = 0x10000 + 1;
404 static String bt_product_telebt() {
409 public final static String bt_product_telebt = bt_product_telebt();
411 public static AltosBTKnown bt_known = new AltosBTKnown();