Demonstrate using AltosLib from altosdroid
[fw/altos] / altoslib / src / org / altusmetrum / AltosLib / AltosLib.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.awt.*;
21 import java.util.*;
22 import java.text.*;
23 import java.nio.charset.Charset;
24
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';
39
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;
54
55         /* Added to flag invalid records */
56         public static final int AO_LOG_INVALID = -1;
57
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;
69
70         /* Telemetry modes */
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;
77
78         public static final String[] ao_telemetry_name = {
79                 "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
80         };
81
82         public static final String launch_sites_url = "http://www.altusmetrum.org/AltOS/launch-sites.txt";
83
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;
87
88         public static final int[] ao_telemetry_len = {
89                 0, 32, 95, 94
90         };
91
92         public static HashMap<String,Integer>   string_to_state = new HashMap<String,Integer>();
93
94         public static boolean map_initialized = false;
95
96         public static void initialize_map()
97         {
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;
110         }
111
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",
116                                                                  telemetry));
117         }
118
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",
123                                                                  telemetry));
124         }
125         
126         public static String[] state_to_string = {
127                 "startup",
128                 "idle",
129                 "pad",
130                 "boost",
131                 "fast",
132                 "coast",
133                 "drogue",
134                 "main",
135                 "landed",
136                 "invalid",
137         };
138
139         public static String[] state_to_string_capital = {
140                 "Startup",
141                 "Idle",
142                 "Pad",
143                 "Boost",
144                 "Fast",
145                 "Coast",
146                 "Drogue",
147                 "Main",
148                 "Landed",
149                 "Invalid",
150         };
151
152         public static int state(String state) {
153                 if (!map_initialized)
154                         initialize_map();
155                 if (string_to_state.containsKey(state))
156                         return string_to_state.get(state);
157                 return ao_flight_invalid;
158         }
159
160         public static String state_name(int state) {
161                 if (state < 0 || state_to_string.length <= state)
162                         return "invalid";
163                 return state_to_string[state];
164         }
165
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;
171
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;
178
179         public static boolean isspace(int c) {
180                 switch (c) {
181                 case ' ':
182                 case '\t':
183                         return true;
184                 }
185                 return false;
186         }
187
188         public static boolean ishex(int c) {
189                 if ('0' <= c && c <= '9')
190                         return true;
191                 if ('a' <= c && c <= 'f')
192                         return true;
193                 if ('A' <= c && c <= 'F')
194                         return true;
195                 return false;
196         }
197
198         public static boolean ishex(String s) {
199                 for (int i = 0; i < s.length(); i++)
200                         if (!ishex(s.charAt(i)))
201                                 return false;
202                 return true;
203         }
204
205         public static int fromhex(int c) {
206                 if ('0' <= c && c <= '9')
207                         return c - '0';
208                 if ('a' <= c && c <= 'f')
209                         return c - 'a' + 10;
210                 if ('A' <= c && c <= 'F')
211                         return c - 'A' + 10;
212                 return -1;
213         }
214
215         public static int fromhex(String s) throws NumberFormatException {
216                 int c, v = 0;
217                 for (int i = 0; i < s.length(); i++) {
218                         c = s.charAt(i);
219                         if (!ishex(c)) {
220                                 if (i == 0)
221                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
222                                 return v;
223                         }
224                         v = v * 16 + fromhex(c);
225                 }
226                 return v;
227         }
228
229         public static boolean isdec(int c) {
230                 if ('0' <= c && c <= '9')
231                         return true;
232                 return false;
233         }
234
235         public static boolean isdec(String s) {
236                 for (int i = 0; i < s.length(); i++)
237                         if (!isdec(s.charAt(i)))
238                                 return false;
239                 return true;
240         }
241
242         public static int fromdec(int c) {
243                 if ('0' <= c && c <= '9')
244                         return c - '0';
245                 return -1;
246         }
247
248         public static int int8(int[] bytes, int i) {
249                 return (int) (byte) bytes[i];
250         }
251
252         public static int uint8(int[] bytes, int i) {
253                 return bytes[i];
254         }
255
256         public static int int16(int[] bytes, int i) {
257                 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
258         }
259
260         public static int uint16(int[] bytes, int i) {
261                 return bytes[i] + (bytes[i+1] << 8);
262         }
263
264         public static int uint32(int[] bytes, int i) {
265                 return bytes[i] +
266                         (bytes[i+1] << 8) +
267                         (bytes[i+2] << 16) +
268                         (bytes[i+3] << 24);
269         }
270
271         public static final Charset     unicode_set = Charset.forName("UTF-8");
272
273         public static String string(int[] bytes, int s, int l) {
274                 if (s + l > bytes.length) {
275                         if (s > bytes.length) {
276                                 s = bytes.length;
277                                 l = 0;
278                         } else {
279                                 l = bytes.length - s;
280                         }
281                 }
282
283                 int i;
284                 for (i = l - 1; i >= 0; i--)
285                         if (bytes[s+i] != 0)
286                                 break;
287
288                 l = i + 1;
289                 byte[]  b = new byte[l];
290
291                 for (i = 0; i < l; i++)
292                         b[i] = (byte) bytes[s+i];
293                 String n = new String(b, unicode_set);
294                 return n;
295         }
296
297         public static int hexbyte(String s, int i) {
298                 int c0, c1;
299
300                 if (s.length() < i + 2)
301                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
302                 c0 = s.charAt(i);
303                 if (!ishex(c0))
304                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
305                 c1 = s.charAt(i+1);
306                 if (!ishex(c1))
307                         throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
308                 return fromhex(c0) * 16 + fromhex(c1);
309         }
310
311         public static int[] hexbytes(String s) {
312                 int     n;
313                 int[]   r;
314                 int     i;
315
316                 if ((s.length() & 1) != 0)
317                         throw new NumberFormatException(String.format("invalid line \"%s\"", s));
318                 n = s.length() / 2;
319                 r = new int[n];
320                 for (i = 0; i < n; i++)
321                         r[i] = hexbyte(s, i * 2);
322                 return r;
323         }
324
325         public static int fromdec(String s) throws NumberFormatException {
326                 int c, v = 0;
327                 int sign = 1;
328                 for (int i = 0; i < s.length(); i++) {
329                         c = s.charAt(i);
330                         if (i == 0 && c == '-') {
331                                 sign = -1;
332                         } else if (!isdec(c)) {
333                                 if (i == 0)
334                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
335                                 return v;
336                         } else
337                                 v = v * 10 + fromdec(c);
338                 }
339                 return v * sign;
340         }
341
342         public static String replace_extension(String input, String extension) {
343                 int dot = input.lastIndexOf(".");
344                 if (dot > 0)
345                         input = input.substring(0,dot);
346                 return input.concat(extension);
347         }
348 }