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 String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
85 static final int ao_telemetry_standard_len = 32;
86 static final int ao_telemetry_0_9_len = 95;
87 static final int ao_telemetry_0_8_len = 94;
89 static final int[] ao_telemetry_len = {
93 static HashMap<String,Integer> string_to_state = new HashMap<String,Integer>();
95 static boolean map_initialized = false;
97 static final int tab_elt_pad = 5;
99 static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
100 static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
101 static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
103 static final int text_width = 20;
105 static void initialize_map()
107 string_to_state.put("startup", ao_flight_startup);
108 string_to_state.put("idle", ao_flight_idle);
109 string_to_state.put("pad", ao_flight_pad);
110 string_to_state.put("boost", ao_flight_boost);
111 string_to_state.put("fast", ao_flight_fast);
112 string_to_state.put("coast", ao_flight_coast);
113 string_to_state.put("drogue", ao_flight_drogue);
114 string_to_state.put("main", ao_flight_main);
115 string_to_state.put("landed", ao_flight_landed);
116 string_to_state.put("invalid", ao_flight_invalid);
117 map_initialized = true;
120 static int telemetry_len(int telemetry) {
121 if (telemetry <= ao_telemetry_max)
122 return ao_telemetry_len[telemetry];
123 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
127 static String telemetry_name(int telemetry) {
128 if (telemetry <= ao_telemetry_max)
129 return ao_telemetry_name[telemetry];
130 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
134 static String[] state_to_string = {
147 static String[] state_to_string_capital = {
160 static public int state(String state) {
161 if (!map_initialized)
163 if (string_to_state.containsKey(state))
164 return string_to_state.get(state);
165 return ao_flight_invalid;
168 static public String state_name(int state) {
169 if (state < 0 || state_to_string.length <= state)
171 return state_to_string[state];
174 static final int AO_GPS_VALID = (1 << 4);
175 static final int AO_GPS_RUNNING = (1 << 5);
176 static final int AO_GPS_DATE_VALID = (1 << 6);
177 static final int AO_GPS_NUM_SAT_SHIFT = 0;
178 static final int AO_GPS_NUM_SAT_MASK = 0xf;
180 static boolean isspace(int c) {
189 static boolean ishex(int c) {
190 if ('0' <= c && c <= '9')
192 if ('a' <= c && c <= 'f')
194 if ('A' <= c && c <= 'F')
199 static boolean ishex(String s) {
200 for (int i = 0; i < s.length(); i++)
201 if (!ishex(s.charAt(i)))
206 static int fromhex(int c) {
207 if ('0' <= c && c <= '9')
209 if ('a' <= c && c <= 'f')
211 if ('A' <= c && c <= 'F')
216 static int fromhex(String s) throws NumberFormatException {
218 for (int i = 0; i < s.length(); i++) {
222 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
225 v = v * 16 + fromhex(c);
230 static boolean isdec(int c) {
231 if ('0' <= c && c <= '9')
236 static boolean isdec(String s) {
237 for (int i = 0; i < s.length(); i++)
238 if (!isdec(s.charAt(i)))
243 static int fromdec(int c) {
244 if ('0' <= c && c <= '9')
249 static int int8(int[] bytes, int i) {
250 return (int) (byte) bytes[i];
253 static int uint8(int[] bytes, int i) {
257 static int int16(int[] bytes, int i) {
258 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
261 static int uint16(int[] bytes, int i) {
262 return bytes[i] + (bytes[i+1] << 8);
265 static int uint32(int[] bytes, int i) {
272 static final Charset unicode_set = Charset.forName("UTF-8");
274 static String string(int[] bytes, int s, int l) {
275 if (s + l > bytes.length) {
276 if (s > bytes.length) {
280 l = bytes.length - s;
285 for (i = l - 1; i >= 0; i--)
290 byte[] b = new byte[l];
292 for (i = 0; i < l; i++)
293 b[i] = (byte) bytes[s+i];
294 String n = new String(b, unicode_set);
298 static int hexbyte(String s, int i) {
301 if (s.length() < i + 2)
302 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
304 if (!Altos.ishex(c0))
305 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
307 if (!Altos.ishex(c1))
308 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
309 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
312 static int[] hexbytes(String s) {
317 if ((s.length() & 1) != 0)
318 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
321 for (i = 0; i < n; i++)
322 r[i] = Altos.hexbyte(s, i * 2);
326 static int fromdec(String s) throws NumberFormatException {
329 for (int i = 0; i < s.length(); i++) {
331 if (i == 0 && c == '-') {
333 } else if (!isdec(c)) {
335 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
338 v = v * 10 + fromdec(c);
343 static String replace_extension(String input, String extension) {
344 int dot = input.lastIndexOf(".");
346 input = input.substring(0,dot);
347 return input.concat(extension);
350 static public boolean initialized = false;
351 static public boolean loaded_library = false;
353 public static boolean load_library() {
356 System.loadLibrary("altos");
357 libaltos.altos_init();
358 loaded_library = true;
359 } catch (UnsatisfiedLinkError e) {
360 loaded_library = false;
364 return loaded_library;
367 static int usb_vendor_altusmetrum() {
372 static int usb_product_altusmetrum() {
377 static int usb_product_altusmetrum_min() {
382 static int usb_product_altusmetrum_max() {
387 static int usb_product_telemetrum() {
392 static int usb_product_teledongle() {
397 static int usb_product_teleterra() {
402 static int usb_product_telebt() {
407 public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
408 public final static int product_altusmetrum = usb_product_altusmetrum();
409 public final static int product_telemetrum = usb_product_telemetrum();
410 public final static int product_teledongle = usb_product_teledongle();
411 public final static int product_teleterra = usb_product_teleterra();
412 public final static int product_telebt = usb_product_telebt();
413 public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
414 public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
416 public final static int product_any = 0x10000;
417 public final static int product_basestation = 0x10000 + 1;
419 static String bt_product_telebt() {
424 public final static String bt_product_telebt = bt_product_telebt();
426 // public static AltosBTKnown bt_known = new AltosBTKnown();