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.
18 package org.altusmetrum.altoslib_1;
22 import java.nio.charset.Charset;
24 public class AltosLib {
25 /* EEProm command letters */
26 public static final int AO_LOG_FLIGHT = 'F';
27 public static final int AO_LOG_SENSOR = 'A';
28 public static final int AO_LOG_TEMP_VOLT = 'T';
29 public static final int AO_LOG_DEPLOY = 'D';
30 public static final int AO_LOG_STATE = 'S';
31 public static final int AO_LOG_GPS_TIME = 'G';
32 public static final int AO_LOG_GPS_LAT = 'N';
33 public static final int AO_LOG_GPS_LON = 'W';
34 public static final int AO_LOG_GPS_ALT = 'H';
35 public static final int AO_LOG_GPS_SAT = 'V';
36 public static final int AO_LOG_GPS_DATE = 'Y';
37 public static final int AO_LOG_PRESSURE = 'P';
39 /* Added for header fields in eeprom files */
40 public static final int AO_LOG_CONFIG_VERSION = 1000;
41 public static final int AO_LOG_MAIN_DEPLOY = 1001;
42 public static final int AO_LOG_APOGEE_DELAY = 1002;
43 public static final int AO_LOG_RADIO_CHANNEL = 1003;
44 public static final int AO_LOG_CALLSIGN = 1004;
45 public static final int AO_LOG_ACCEL_CAL = 1005;
46 public static final int AO_LOG_RADIO_CAL = 1006;
47 public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
48 public static final int AO_LOG_MANUFACTURER = 2000;
49 public static final int AO_LOG_PRODUCT = 2001;
50 public static final int AO_LOG_SERIAL_NUMBER = 2002;
51 public static final int AO_LOG_LOG_FORMAT = 2003;
53 /* Added for header fields in telemega files */
54 public static final int AO_LOG_BARO_RESERVED = 3000;
55 public static final int AO_LOG_BARO_SENS = 3001;
56 public static final int AO_LOG_BARO_OFF = 3002;
57 public static final int AO_LOG_BARO_TCS = 3004;
58 public static final int AO_LOG_BARO_TCO = 3005;
59 public static final int AO_LOG_BARO_TREF = 3006;
60 public static final int AO_LOG_BARO_TEMPSENS = 3007;
61 public static final int AO_LOG_BARO_CRC = 3008;
63 public static final int AO_LOG_SOFTWARE_VERSION = 9999;
65 /* Added to flag invalid records */
66 public static final int AO_LOG_INVALID = -1;
68 /* Flight state numbers and names */
69 public static final int ao_flight_startup = 0;
70 public static final int ao_flight_idle = 1;
71 public static final int ao_flight_pad = 2;
72 public static final int ao_flight_boost = 3;
73 public static final int ao_flight_fast = 4;
74 public static final int ao_flight_coast = 5;
75 public static final int ao_flight_drogue = 6;
76 public static final int ao_flight_main = 7;
77 public static final int ao_flight_landed = 8;
78 public static final int ao_flight_invalid = 9;
81 public final static int vendor_altusmetrum = 0xfffe;
83 public final static int product_altusmetrum = 0x000a;
84 public final static int product_telemetrum = 0x000b;
85 public final static int product_teledongle = 0x000c;
86 public final static int product_teleterra = 0x000d;
87 public final static int product_telebt = 0x000e;
88 public final static int product_telelaunch = 0x000f;
89 public final static int product_telelco = 0x0010;
90 public final static int product_telescience = 0x0011;
91 public final static int product_telepyro =0x0012;
92 public final static int product_telemega = 0x0023;
93 public final static int product_megadongle = 0x0024;
94 public final static int product_telegps = 0x0025;
95 public final static int product_altusmetrum_min = 0x000a;
96 public final static int product_altusmetrum_max = 0x0025;
98 public final static int product_any = 0x10000;
99 public final static int product_basestation = 0x10000 + 1;
100 public final static int product_altimeter = 0x10000 + 2;
102 /* Bluetooth "identifier" (bluetooth sucks) */
103 public final static String bt_product_telebt = "TeleBT";
105 /* Telemetry modes */
106 public static final int ao_telemetry_off = 0;
107 public static final int ao_telemetry_min = 1;
108 public static final int ao_telemetry_standard = 1;
109 public static final int ao_telemetry_0_9 = 2;
110 public static final int ao_telemetry_0_8 = 3;
111 public static final int ao_telemetry_max = 3;
113 private static final String[] ao_telemetry_name = {
114 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
117 public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
119 public static final int ao_telemetry_standard_len = 32;
120 public static final int ao_telemetry_0_9_len = 95;
121 public static final int ao_telemetry_0_8_len = 94;
123 private static final int[] ao_telemetry_len = {
127 private static HashMap<String,Integer> string_to_state = new HashMap<String,Integer>();
129 private static boolean map_initialized = false;
131 public static void initialize_map()
133 string_to_state.put("startup", ao_flight_startup);
134 string_to_state.put("idle", ao_flight_idle);
135 string_to_state.put("pad", ao_flight_pad);
136 string_to_state.put("boost", ao_flight_boost);
137 string_to_state.put("fast", ao_flight_fast);
138 string_to_state.put("coast", ao_flight_coast);
139 string_to_state.put("drogue", ao_flight_drogue);
140 string_to_state.put("apogee", ao_flight_coast);
141 string_to_state.put("main", ao_flight_main);
142 string_to_state.put("landed", ao_flight_landed);
143 string_to_state.put("invalid", ao_flight_invalid);
144 map_initialized = true;
147 public static int telemetry_len(int telemetry) {
148 if (telemetry <= ao_telemetry_max)
149 return ao_telemetry_len[telemetry];
150 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
154 public static String telemetry_name(int telemetry) {
155 if (telemetry <= ao_telemetry_max)
156 return ao_telemetry_name[telemetry];
157 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
161 private static String[] state_to_string = {
174 private static String[] state_to_string_capital = {
187 public static int state(String state) {
188 if (!map_initialized)
190 if (string_to_state.containsKey(state))
191 return string_to_state.get(state);
192 return ao_flight_invalid;
195 public static String state_name(int state) {
196 if (state < 0 || state_to_string.length <= state)
198 return state_to_string[state];
201 public static String state_name_capital(int state) {
202 if (state < 0 || state_to_string.length <= state)
204 return state_to_string_capital[state];
207 public static final int AO_GPS_VALID = (1 << 4);
208 public static final int AO_GPS_RUNNING = (1 << 5);
209 public static final int AO_GPS_DATE_VALID = (1 << 6);
210 public static final int AO_GPS_NUM_SAT_SHIFT = 0;
211 public static final int AO_GPS_NUM_SAT_MASK = 0xf;
213 public static final int AO_LOG_FORMAT_UNKNOWN = 0;
214 public static final int AO_LOG_FORMAT_FULL = 1;
215 public static final int AO_LOG_FORMAT_TINY = 2;
216 public static final int AO_LOG_FORMAT_TELEMETRY = 3;
217 public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
218 public static final int AO_LOG_FORMAT_TELEMEGA = 5;
219 public static final int AO_LOG_FORMAT_NONE = 127;
221 public static boolean isspace(int c) {
230 public static boolean ishex(int c) {
231 if ('0' <= c && c <= '9')
233 if ('a' <= c && c <= 'f')
235 if ('A' <= c && c <= 'F')
240 public static boolean ishex(String s) {
241 for (int i = 0; i < s.length(); i++)
242 if (!ishex(s.charAt(i)))
247 public static int fromhex(int c) {
248 if ('0' <= c && c <= '9')
250 if ('a' <= c && c <= 'f')
252 if ('A' <= c && c <= 'F')
257 public static int fromhex(String s) throws NumberFormatException {
259 for (int i = 0; i < s.length(); i++) {
263 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
266 v = v * 16 + fromhex(c);
271 public static boolean isdec(int c) {
272 if ('0' <= c && c <= '9')
277 public static boolean isdec(String s) {
278 for (int i = 0; i < s.length(); i++)
279 if (!isdec(s.charAt(i)))
284 public static int fromdec(int c) {
285 if ('0' <= c && c <= '9')
290 public static int int8(int[] bytes, int i) {
291 return (int) (byte) bytes[i];
294 public static int uint8(int[] bytes, int i) {
298 public static int int16(int[] bytes, int i) {
299 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
302 public static int uint16(int[] bytes, int i) {
303 return bytes[i] + (bytes[i+1] << 8);
306 public static int uint32(int[] bytes, int i) {
313 public static int int32(int[] bytes, int i) {
314 return (int) uint32(bytes, i);
317 public static final Charset unicode_set = Charset.forName("UTF-8");
319 public static String string(int[] bytes, int s, int l) {
320 if (s + l > bytes.length) {
321 if (s > bytes.length) {
325 l = bytes.length - s;
330 for (i = l - 1; i >= 0; i--)
335 byte[] b = new byte[l];
337 for (i = 0; i < l; i++)
338 b[i] = (byte) bytes[s+i];
339 String n = new String(b, unicode_set);
343 public static int hexbyte(String s, int i) {
346 if (s.length() < i + 2)
347 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
350 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
353 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
354 return fromhex(c0) * 16 + fromhex(c1);
357 public static int[] hexbytes(String s) {
362 if ((s.length() & 1) != 0)
363 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
366 for (i = 0; i < n; i++)
367 r[i] = hexbyte(s, i * 2);
371 public static int fromdec(String s) throws NumberFormatException {
374 for (int i = 0; i < s.length(); i++) {
376 if (i == 0 && c == '-') {
378 } else if (!isdec(c)) {
380 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
383 v = v * 10 + fromdec(c);
388 public static String gets(FileInputStream s) throws IOException {
392 while ((c = s.read()) != -1) {
398 line = line + (char) c;
403 public static String replace_extension(String input, String extension) {
404 int dot = input.lastIndexOf(".");
406 input = input.substring(0,dot);
407 return input.concat(extension);
410 public static File replace_extension(File input, String extension) {
411 return new File(replace_extension(input.getPath(), extension));