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;
27 import org.altusmetrum.AltosLib.*;
29 public class Altos extends AltosLib {
31 static final int tab_elt_pad = 5;
33 static Font label_font;
34 static Font value_font;
35 static Font status_font;
36 static Font table_label_font;
37 static Font table_value_font;
39 final static int font_size_small = 1;
40 final static int font_size_medium = 2;
41 final static int font_size_large = 3;
43 static void set_fonts(int size) {
55 case font_size_medium:
66 label_font = new Font("Dialog", Font.PLAIN, brief_size);
67 value_font = new Font("Monospaced", Font.PLAIN, brief_size);
68 status_font = new Font("SansSerif", Font.BOLD, status_size);
69 table_label_font = new Font("SansSerif", Font.PLAIN, table_size);
70 table_value_font = new Font("Monospaced", Font.PLAIN, table_size);
73 static final int text_width = 20;
75 static void initialize_map()
77 string_to_state.put("startup", ao_flight_startup);
78 string_to_state.put("idle", ao_flight_idle);
79 string_to_state.put("pad", ao_flight_pad);
80 string_to_state.put("boost", ao_flight_boost);
81 string_to_state.put("fast", ao_flight_fast);
82 string_to_state.put("coast", ao_flight_coast);
83 string_to_state.put("drogue", ao_flight_drogue);
84 string_to_state.put("apogee", ao_flight_coast);
85 string_to_state.put("main", ao_flight_main);
86 string_to_state.put("landed", ao_flight_landed);
87 string_to_state.put("invalid", ao_flight_invalid);
88 map_initialized = true;
91 static int telemetry_len(int telemetry) {
92 if (telemetry <= ao_telemetry_max)
93 return ao_telemetry_len[telemetry];
94 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
98 static String telemetry_name(int telemetry) {
99 if (telemetry <= ao_telemetry_max)
100 return ao_telemetry_name[telemetry];
101 throw new IllegalArgumentException(String.format("Invalid telemetry %d",
105 static String[] state_to_string = {
118 static String[] state_to_string_capital = {
131 static public int state(String state) {
132 if (!map_initialized)
134 if (string_to_state.containsKey(state))
135 return string_to_state.get(state);
136 return ao_flight_invalid;
139 static public String state_name(int state) {
140 if (state < 0 || state_to_string.length <= state)
142 return state_to_string[state];
145 static final int AO_GPS_VALID = (1 << 4);
146 static final int AO_GPS_RUNNING = (1 << 5);
147 static final int AO_GPS_DATE_VALID = (1 << 6);
148 static final int AO_GPS_NUM_SAT_SHIFT = 0;
149 static final int AO_GPS_NUM_SAT_MASK = 0xf;
151 static final int AO_LOG_FORMAT_UNKNOWN = 0;
152 static final int AO_LOG_FORMAT_FULL = 1;
153 static final int AO_LOG_FORMAT_TINY = 2;
154 static final int AO_LOG_FORMAT_TELEMETRY = 3;
155 static final int AO_LOG_FORMAT_TELESCIENCE = 4;
156 static final int AO_LOG_FORMAT_MEGAMETRUM = 5;
157 static final int AO_LOG_FORMAT_NONE = 127;
159 static boolean isspace(int c) {
168 static boolean ishex(int c) {
169 if ('0' <= c && c <= '9')
171 if ('a' <= c && c <= 'f')
173 if ('A' <= c && c <= 'F')
178 static boolean ishex(String s) {
179 for (int i = 0; i < s.length(); i++)
180 if (!ishex(s.charAt(i)))
185 static int fromhex(int c) {
186 if ('0' <= c && c <= '9')
188 if ('a' <= c && c <= 'f')
190 if ('A' <= c && c <= 'F')
195 static int fromhex(String s) throws NumberFormatException {
197 for (int i = 0; i < s.length(); i++) {
201 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
204 v = v * 16 + fromhex(c);
209 static boolean isdec(int c) {
210 if ('0' <= c && c <= '9')
215 static boolean isdec(String s) {
216 for (int i = 0; i < s.length(); i++)
217 if (!isdec(s.charAt(i)))
222 static int fromdec(int c) {
223 if ('0' <= c && c <= '9')
228 static int int8(int[] bytes, int i) {
229 return (int) (byte) bytes[i];
232 static int uint8(int[] bytes, int i) {
236 static int int16(int[] bytes, int i) {
237 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
240 static int uint16(int[] bytes, int i) {
241 return bytes[i] + (bytes[i+1] << 8);
244 static int uint32(int[] bytes, int i) {
251 static final Charset unicode_set = Charset.forName("UTF-8");
253 static String string(int[] bytes, int s, int l) {
254 if (s + l > bytes.length) {
255 if (s > bytes.length) {
259 l = bytes.length - s;
264 for (i = l - 1; i >= 0; i--)
269 byte[] b = new byte[l];
271 for (i = 0; i < l; i++)
272 b[i] = (byte) bytes[s+i];
273 String n = new String(b, unicode_set);
277 static int hexbyte(String s, int i) {
280 if (s.length() < i + 2)
281 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
283 if (!Altos.ishex(c0))
284 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
286 if (!Altos.ishex(c1))
287 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
288 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
291 static int[] hexbytes(String s) {
296 if ((s.length() & 1) != 0)
297 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
300 for (i = 0; i < n; i++)
301 r[i] = Altos.hexbyte(s, i * 2);
305 static int fromdec(String s) throws NumberFormatException {
308 for (int i = 0; i < s.length(); i++) {
310 if (i == 0 && c == '-') {
312 } else if (!isdec(c)) {
314 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
317 v = v * 10 + fromdec(c);
322 static String replace_extension(String input, String extension) {
323 int dot = input.lastIndexOf(".");
325 input = input.substring(0,dot);
326 return input.concat(extension);
329 static public boolean initialized = false;
330 static public boolean loaded_library = false;
332 public static boolean load_library() {
335 System.loadLibrary("altos");
336 libaltos.altos_init();
337 loaded_library = true;
338 } catch (UnsatisfiedLinkError e) {
340 System.loadLibrary("altos64");
341 libaltos.altos_init();
342 loaded_library = true;
343 } catch (UnsatisfiedLinkError e2) {
344 loaded_library = false;
349 return loaded_library;
352 static int usb_vendor_altusmetrum() {
357 static int usb_product_altusmetrum() {
362 static int usb_product_altusmetrum_min() {
367 static int usb_product_altusmetrum_max() {
372 static int usb_product_telemetrum() {
377 static int usb_product_teledongle() {
382 static int usb_product_teleterra() {
387 static int usb_product_telebt() {
392 static int usb_product_telelaunch() {
397 static int usb_product_telelco() {
402 static int usb_product_telescience() {
407 static int usb_product_telepyro() {
412 public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
413 public final static int product_altusmetrum = usb_product_altusmetrum();
414 public final static int product_telemetrum = usb_product_telemetrum();
415 public final static int product_teledongle = usb_product_teledongle();
416 public final static int product_teleterra = usb_product_teleterra();
417 public final static int product_telebt = usb_product_telebt();
418 public final static int product_telelaunch = usb_product_telelaunch();
419 public final static int product_tele10 = usb_product_telelco();
420 public final static int product_telescience = usb_product_telescience();
421 public final static int product_telepyro = usb_product_telepyro();
422 public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
423 public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
425 public final static int product_any = 0x10000;
426 public final static int product_basestation = 0x10000 + 1;
428 static String bt_product_telebt() {
433 public final static String bt_product_telebt = bt_product_telebt();
435 public static AltosBTKnown bt_known = new AltosBTKnown();