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;
23 import java.nio.charset.Charset;
25 public class AltosLib {
26 /* EEProm command letters */
27 public static final int AO_LOG_FLIGHT = 'F';
28 public static final int AO_LOG_SENSOR = 'A';
29 public static final int AO_LOG_TEMP_VOLT = 'T';
30 public static final int AO_LOG_DEPLOY = 'D';
31 public static final int AO_LOG_STATE = 'S';
32 public static final int AO_LOG_GPS_TIME = 'G';
33 public static final int AO_LOG_GPS_LAT = 'N';
34 public static final int AO_LOG_GPS_LON = 'W';
35 public static final int AO_LOG_GPS_ALT = 'H';
36 public static final int AO_LOG_GPS_SAT = 'V';
37 public static final int AO_LOG_GPS_DATE = 'Y';
38 public static final int AO_LOG_PRESSURE = 'P';
40 /* Added for header fields in eeprom files */
41 public static final int AO_LOG_CONFIG_VERSION = 1000;
42 public static final int AO_LOG_MAIN_DEPLOY = 1001;
43 public static final int AO_LOG_APOGEE_DELAY = 1002;
44 public static final int AO_LOG_RADIO_CHANNEL = 1003;
45 public static final int AO_LOG_CALLSIGN = 1004;
46 public static final int AO_LOG_ACCEL_CAL = 1005;
47 public static final int AO_LOG_RADIO_CAL = 1006;
48 public static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
49 public static final int AO_LOG_MANUFACTURER = 2000;
50 public static final int AO_LOG_PRODUCT = 2001;
51 public static final int AO_LOG_SERIAL_NUMBER = 2002;
52 public static final int AO_LOG_LOG_FORMAT = 2003;
53 public static final int AO_LOG_SOFTWARE_VERSION = 9999;
55 /* Added to flag invalid records */
56 public static final int AO_LOG_INVALID = -1;
58 /* Flight state numbers and names */
59 public static final int ao_flight_startup = 0;
60 public static final int ao_flight_idle = 1;
61 public static final int ao_flight_pad = 2;
62 public static final int ao_flight_boost = 3;
63 public static final int ao_flight_fast = 4;
64 public static final int ao_flight_coast = 5;
65 public static final int ao_flight_drogue = 6;
66 public static final int ao_flight_main = 7;
67 public static final int ao_flight_landed = 8;
68 public static final int ao_flight_invalid = 9;
71 public static final int ao_telemetry_off = 0;
72 public static final int ao_telemetry_min = 1;
73 public static final int ao_telemetry_standard = 1;
74 public static final int ao_telemetry_0_9 = 2;
75 public static final int ao_telemetry_0_8 = 3;
76 public static final int ao_telemetry_max = 3;
78 public static final String[] ao_telemetry_name = {
79 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
82 public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
84 public static final int ao_telemetry_standard_len = 32;
85 public static final int ao_telemetry_0_9_len = 95;
86 public static final int ao_telemetry_0_8_len = 94;
88 public static final int[] ao_telemetry_len = {
92 public static HashMap<String,Integer> string_to_state = new HashMap<String,Integer>();
94 public static boolean map_initialized = false;
96 public static void initialize_map()
98 string_to_state.put("startup", ao_flight_startup);
99 string_to_state.put("idle", ao_flight_idle);
100 string_to_state.put("pad", ao_flight_pad);
101 string_to_state.put("boost", ao_flight_boost);
102 string_to_state.put("fast", ao_flight_fast);
103 string_to_state.put("coast", ao_flight_coast);
104 string_to_state.put("drogue", ao_flight_drogue);
105 string_to_state.put("apogee", ao_flight_coast);
106 string_to_state.put("main", ao_flight_main);
107 string_to_state.put("landed", ao_flight_landed);
108 string_to_state.put("invalid", ao_flight_invalid);
109 map_initialized = true;
112 public static int telemetry_len(int telemetry) {
113 if (telemetry <= ao_telemetry_max)
114 return ao_telemetry_len[telemetry];
115 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
119 public static String telemetry_name(int telemetry) {
120 if (telemetry <= ao_telemetry_max)
121 return ao_telemetry_name[telemetry];
122 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
126 public static String[] state_to_string = {
139 public static String[] state_to_string_capital = {
152 public static int state(String state) {
153 if (!map_initialized)
155 if (string_to_state.containsKey(state))
156 return string_to_state.get(state);
157 return ao_flight_invalid;
160 public static String state_name(int state) {
161 if (state < 0 || state_to_string.length <= state)
163 return state_to_string[state];
166 public static final int AO_GPS_VALID = (1 << 4);
167 public static final int AO_GPS_RUNNING = (1 << 5);
168 public static final int AO_GPS_DATE_VALID = (1 << 6);
169 public static final int AO_GPS_NUM_SAT_SHIFT = 0;
170 public static final int AO_GPS_NUM_SAT_MASK = 0xf;
172 public static final int AO_LOG_FORMAT_UNKNOWN = 0;
173 public static final int AO_LOG_FORMAT_FULL = 1;
174 public static final int AO_LOG_FORMAT_TINY = 2;
175 public static final int AO_LOG_FORMAT_TELEMETRY = 3;
176 public static final int AO_LOG_FORMAT_TELESCIENCE = 4;
177 public static final int AO_LOG_FORMAT_NONE = 127;
179 public static boolean isspace(int c) {
188 public static boolean ishex(int c) {
189 if ('0' <= c && c <= '9')
191 if ('a' <= c && c <= 'f')
193 if ('A' <= c && c <= 'F')
198 public static boolean ishex(String s) {
199 for (int i = 0; i < s.length(); i++)
200 if (!ishex(s.charAt(i)))
205 public static int fromhex(int c) {
206 if ('0' <= c && c <= '9')
208 if ('a' <= c && c <= 'f')
210 if ('A' <= c && c <= 'F')
215 public static int fromhex(String s) throws NumberFormatException {
217 for (int i = 0; i < s.length(); i++) {
221 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
224 v = v * 16 + fromhex(c);
229 public static boolean isdec(int c) {
230 if ('0' <= c && c <= '9')
235 public static boolean isdec(String s) {
236 for (int i = 0; i < s.length(); i++)
237 if (!isdec(s.charAt(i)))
242 public static int fromdec(int c) {
243 if ('0' <= c && c <= '9')
248 public static int int8(int[] bytes, int i) {
249 return (int) (byte) bytes[i];
252 public static int uint8(int[] bytes, int i) {
256 public static int int16(int[] bytes, int i) {
257 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
260 public static int uint16(int[] bytes, int i) {
261 return bytes[i] + (bytes[i+1] << 8);
264 public static int uint32(int[] bytes, int i) {
271 public static final Charset unicode_set = Charset.forName("UTF-8");
273 public static String string(int[] bytes, int s, int l) {
274 if (s + l > bytes.length) {
275 if (s > bytes.length) {
279 l = bytes.length - s;
284 for (i = l - 1; i >= 0; i--)
289 byte[] b = new byte[l];
291 for (i = 0; i < l; i++)
292 b[i] = (byte) bytes[s+i];
293 String n = new String(b, unicode_set);
297 public static int hexbyte(String s, int i) {
300 if (s.length() < i + 2)
301 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
304 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
307 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
308 return fromhex(c0) * 16 + fromhex(c1);
311 public static int[] hexbytes(String s) {
316 if ((s.length() & 1) != 0)
317 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
320 for (i = 0; i < n; i++)
321 r[i] = hexbyte(s, i * 2);
325 public static int fromdec(String s) throws NumberFormatException {
328 for (int i = 0; i < s.length(); i++) {
330 if (i == 0 && c == '-') {
332 } else if (!isdec(c)) {
334 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
337 v = v * 10 + fromdec(c);
342 public static String replace_extension(String input, String extension) {
343 int dot = input.lastIndexOf(".");
345 input = input.substring(0,dot);
346 return input.concat(extension);