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 = 20;
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 String[] state_to_string_capital = {
158 static public int state(String state) {
159 if (!map_initialized)
161 if (string_to_state.containsKey(state))
162 return string_to_state.get(state);
163 return ao_flight_invalid;
166 static public String state_name(int state) {
167 if (state < 0 || state_to_string.length <= state)
169 return state_to_string[state];
172 static final int AO_GPS_VALID = (1 << 4);
173 static final int AO_GPS_RUNNING = (1 << 5);
174 static final int AO_GPS_DATE_VALID = (1 << 6);
175 static final int AO_GPS_NUM_SAT_SHIFT = 0;
176 static final int AO_GPS_NUM_SAT_MASK = 0xf;
178 static boolean isspace(int c) {
187 static boolean ishex(int c) {
188 if ('0' <= c && c <= '9')
190 if ('a' <= c && c <= 'f')
192 if ('A' <= c && c <= 'F')
197 static boolean ishex(String s) {
198 for (int i = 0; i < s.length(); i++)
199 if (!ishex(s.charAt(i)))
204 static int fromhex(int c) {
205 if ('0' <= c && c <= '9')
207 if ('a' <= c && c <= 'f')
209 if ('A' <= c && c <= 'F')
214 static int fromhex(String s) throws NumberFormatException {
216 for (int i = 0; i < s.length(); i++) {
220 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
223 v = v * 16 + fromhex(c);
228 static boolean isdec(int c) {
229 if ('0' <= c && c <= '9')
234 static boolean isdec(String s) {
235 for (int i = 0; i < s.length(); i++)
236 if (!isdec(s.charAt(i)))
241 static int fromdec(int c) {
242 if ('0' <= c && c <= '9')
247 static int int8(int[] bytes, int i) {
248 return (int) (byte) bytes[i];
251 static int uint8(int[] bytes, int i) {
255 static int int16(int[] bytes, int i) {
256 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
259 static int uint16(int[] bytes, int i) {
260 return bytes[i] + (bytes[i+1] << 8);
263 static int uint32(int[] bytes, int i) {
270 static final Charset unicode_set = Charset.forName("UTF-8");
272 static String string(int[] bytes, int s, int l) {
273 if (s + l > bytes.length) {
274 if (s > bytes.length) {
278 l = bytes.length - s;
283 for (i = l - 1; i >= 0; i--)
288 byte[] b = new byte[l];
290 for (i = 0; i < l; i++)
291 b[i] = (byte) bytes[s+i];
292 String n = new String(b, unicode_set);
296 static int hexbyte(String s, int i) {
299 if (s.length() < i + 2)
300 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
302 if (!Altos.ishex(c0))
303 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
305 if (!Altos.ishex(c1))
306 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
307 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
310 static int[] hexbytes(String s) {
315 if ((s.length() & 1) != 0)
316 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
319 for (i = 0; i < n; i++)
320 r[i] = Altos.hexbyte(s, i * 2);
324 static int fromdec(String s) throws NumberFormatException {
327 for (int i = 0; i < s.length(); i++) {
329 if (i == 0 && c == '-') {
331 } else if (!isdec(c)) {
333 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
336 v = v * 10 + fromdec(c);
341 static String replace_extension(String input, String extension) {
342 int dot = input.lastIndexOf(".");
344 input = input.substring(0,dot);
345 return input.concat(extension);
348 static public boolean initialized = false;
349 static public boolean loaded_library = false;
351 public static boolean load_library() {
354 System.loadLibrary("altos");
355 libaltos.altos_init();
356 loaded_library = true;
357 } catch (UnsatisfiedLinkError e) {
358 loaded_library = false;
362 return loaded_library;
365 static int usb_vendor_altusmetrum() {
370 static int usb_product_altusmetrum() {
375 static int usb_product_altusmetrum_min() {
380 static int usb_product_altusmetrum_max() {
385 static int usb_product_telemetrum() {
390 static int usb_product_teledongle() {
395 static int usb_product_teleterra() {
400 static int usb_product_telebt() {
405 public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
406 public final static int product_altusmetrum = usb_product_altusmetrum();
407 public final static int product_telemetrum = usb_product_telemetrum();
408 public final static int product_teledongle = usb_product_teledongle();
409 public final static int product_teleterra = usb_product_teleterra();
410 public final static int product_telebt = usb_product_telebt();
411 public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
412 public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
414 public final static int product_any = 0x10000;
415 public final static int product_basestation = 0x10000 + 1;
417 static String bt_product_telebt() {
422 public final static String bt_product_telebt = bt_product_telebt();
424 // public static AltosBTKnown bt_known = new AltosBTKnown();