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 public int state(String state) {
78 if (string_to_state.containsKey(state))
79 return string_to_state.get(state);
80 return ao_flight_invalid;
83 static public String state_name(int state) {
84 if (state < 0 || state_to_string.length <= state)
86 return state_to_string[state];
89 static final int AO_GPS_VALID = (1 << 4);
90 static final int AO_GPS_RUNNING = (1 << 5);
91 static final int AO_GPS_DATE_VALID = (1 << 6);
92 static final int AO_GPS_NUM_SAT_SHIFT = 0;
93 static final int AO_GPS_NUM_SAT_MASK = 0xf;
95 static final int AO_LOG_FORMAT_UNKNOWN = 0;
96 static final int AO_LOG_FORMAT_FULL = 1;
97 static final int AO_LOG_FORMAT_TINY = 2;
98 static final int AO_LOG_FORMAT_TELEMETRY = 3;
99 static final int AO_LOG_FORMAT_TELESCIENCE = 4;
100 static final int AO_LOG_FORMAT_MEGAMETRUM = 5;
101 static final int AO_LOG_FORMAT_NONE = 127;
103 static boolean isspace(int c) {
112 static boolean ishex(int c) {
113 if ('0' <= c && c <= '9')
115 if ('a' <= c && c <= 'f')
117 if ('A' <= c && c <= 'F')
122 static boolean ishex(String s) {
123 for (int i = 0; i < s.length(); i++)
124 if (!ishex(s.charAt(i)))
129 static int fromhex(int c) {
130 if ('0' <= c && c <= '9')
132 if ('a' <= c && c <= 'f')
134 if ('A' <= c && c <= 'F')
139 static int fromhex(String s) throws NumberFormatException {
141 for (int i = 0; i < s.length(); i++) {
145 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
148 v = v * 16 + fromhex(c);
153 static boolean isdec(int c) {
154 if ('0' <= c && c <= '9')
159 static boolean isdec(String s) {
160 for (int i = 0; i < s.length(); i++)
161 if (!isdec(s.charAt(i)))
166 static int fromdec(int c) {
167 if ('0' <= c && c <= '9')
172 static int int8(int[] bytes, int i) {
173 return (int) (byte) bytes[i];
176 static int uint8(int[] bytes, int i) {
180 static int int16(int[] bytes, int i) {
181 return (int) (short) (bytes[i] + (bytes[i+1] << 8));
184 static int uint16(int[] bytes, int i) {
185 return bytes[i] + (bytes[i+1] << 8);
188 static int uint32(int[] bytes, int i) {
195 static final Charset unicode_set = Charset.forName("UTF-8");
197 static String string(int[] bytes, int s, int l) {
198 if (s + l > bytes.length) {
199 if (s > bytes.length) {
203 l = bytes.length - s;
208 for (i = l - 1; i >= 0; i--)
213 byte[] b = new byte[l];
215 for (i = 0; i < l; i++)
216 b[i] = (byte) bytes[s+i];
217 String n = new String(b, unicode_set);
221 static int hexbyte(String s, int i) {
224 if (s.length() < i + 2)
225 throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
227 if (!Altos.ishex(c0))
228 throw new NumberFormatException(String.format("invalid hex \"%c\"", c0));
230 if (!Altos.ishex(c1))
231 throw new NumberFormatException(String.format("invalid hex \"%c\"", c1));
232 return Altos.fromhex(c0) * 16 + Altos.fromhex(c1);
235 static int[] hexbytes(String s) {
240 if ((s.length() & 1) != 0)
241 throw new NumberFormatException(String.format("invalid line \"%s\"", s));
244 for (i = 0; i < n; i++)
245 r[i] = Altos.hexbyte(s, i * 2);
249 static int fromdec(String s) throws NumberFormatException {
252 for (int i = 0; i < s.length(); i++) {
254 if (i == 0 && c == '-') {
256 } else if (!isdec(c)) {
258 throw new NumberFormatException(String.format("invalid number \"%s\"", s));
261 v = v * 10 + fromdec(c);
266 static String replace_extension(String input, String extension) {
267 int dot = input.lastIndexOf(".");
269 input = input.substring(0,dot);
270 return input.concat(extension);
273 static public boolean initialized = false;
274 static public boolean loaded_library = false;
276 public static boolean load_library() {
279 System.loadLibrary("altos");
280 libaltos.altos_init();
281 loaded_library = true;
282 } catch (UnsatisfiedLinkError e) {
284 System.loadLibrary("altos64");
285 libaltos.altos_init();
286 loaded_library = true;
287 } catch (UnsatisfiedLinkError e2) {
288 loaded_library = false;
293 return loaded_library;
296 static int usb_vendor_altusmetrum() {
301 static int usb_product_altusmetrum() {
306 static int usb_product_altusmetrum_min() {
311 static int usb_product_altusmetrum_max() {
316 static int usb_product_telemetrum() {
321 static int usb_product_teledongle() {
326 static int usb_product_teleterra() {
331 static int usb_product_telebt() {
336 static int usb_product_telelaunch() {
341 static int usb_product_telelco() {
346 static int usb_product_telescience() {
351 static int usb_product_telepyro() {
356 public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
357 public final static int product_altusmetrum = usb_product_altusmetrum();
358 public final static int product_telemetrum = usb_product_telemetrum();
359 public final static int product_teledongle = usb_product_teledongle();
360 public final static int product_teleterra = usb_product_teleterra();
361 public final static int product_telebt = usb_product_telebt();
362 public final static int product_telelaunch = usb_product_telelaunch();
363 public final static int product_tele10 = usb_product_telelco();
364 public final static int product_telescience = usb_product_telescience();
365 public final static int product_telepyro = usb_product_telepyro();
366 public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
367 public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
369 public final static int product_any = 0x10000;
370 public final static int product_basestation = 0x10000 + 1;
372 static String bt_product_telebt() {
377 public final static String bt_product_telebt = bt_product_telebt();
379 public static AltosBTKnown bt_known = new AltosBTKnown();