Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[fw/altos] / ao-tools / altosui / Altos.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 altosui;
19
20 import java.awt.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class Altos {
25         /* EEProm command letters */
26         static final int AO_LOG_FLIGHT = 'F';
27         static final int AO_LOG_SENSOR = 'A';
28         static final int AO_LOG_TEMP_VOLT = 'T';
29         static final int AO_LOG_DEPLOY = 'D';
30         static final int AO_LOG_STATE = 'S';
31         static final int AO_LOG_GPS_TIME = 'G';
32         static final int AO_LOG_GPS_LAT = 'N';
33         static final int AO_LOG_GPS_LON = 'W';
34         static final int AO_LOG_GPS_ALT = 'H';
35         static final int AO_LOG_GPS_SAT = 'V';
36         static final int AO_LOG_GPS_DATE = 'Y';
37
38         /* Added for header fields in eeprom files */
39         static final int AO_LOG_CONFIG_VERSION = 1000;
40         static final int AO_LOG_MAIN_DEPLOY = 1001;
41         static final int AO_LOG_APOGEE_DELAY = 1002;
42         static final int AO_LOG_RADIO_CHANNEL = 1003;
43         static final int AO_LOG_CALLSIGN = 1004;
44         static final int AO_LOG_ACCEL_CAL = 1005;
45         static final int AO_LOG_RADIO_CAL = 1006;
46         static final int AO_LOG_MANUFACTURER = 1007;
47         static final int AO_LOG_PRODUCT = 1008;
48         static final int AO_LOG_SERIAL_NUMBER = 1009;
49         static final int AO_LOG_SOFTWARE_VERSION = 1010;
50
51         /* Added to flag invalid records */
52         static final int AO_LOG_INVALID = -1;
53
54         /* Flight state numbers and names */
55         static final int ao_flight_startup = 0;
56         static final int ao_flight_idle = 1;
57         static final int ao_flight_pad = 2;
58         static final int ao_flight_boost = 3;
59         static final int ao_flight_fast = 4;
60         static final int ao_flight_coast = 5;
61         static final int ao_flight_drogue = 6;
62         static final int ao_flight_main = 7;
63         static final int ao_flight_landed = 8;
64         static final int ao_flight_invalid = 9;
65
66         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
67
68         static boolean map_initialized = false;
69
70         static final int tab_elt_pad = 5;
71
72         static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
73         static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
74         static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
75
76         static void initialize_map()
77         {
78                 string_to_state.put("startup", ao_flight_startup);
79                 string_to_state.put("idle", ao_flight_idle);
80                 string_to_state.put("pad", ao_flight_pad);
81                 string_to_state.put("boost", ao_flight_boost);
82                 string_to_state.put("fast", ao_flight_fast);
83                 string_to_state.put("coast", ao_flight_coast);
84                 string_to_state.put("drogue", ao_flight_drogue);
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;
89         }
90
91         static String[] state_to_string = {
92                 "startup",
93                 "idle",
94                 "pad",
95                 "boost",
96                 "fast",
97                 "coast",
98                 "drogue",
99                 "main",
100                 "landed",
101                 "invalid",
102         };
103
104         static public int state(String state) {
105                 if (!map_initialized)
106                         initialize_map();
107                 if (string_to_state.containsKey(state))
108                         return string_to_state.get(state);
109                 return ao_flight_invalid;
110         }
111
112         static public String state_name(int state) {
113                 if (state < 0 || state_to_string.length <= state)
114                         return "invalid";
115                 return state_to_string[state];
116         }
117
118         static final int AO_GPS_VALID = (1 << 4);
119         static final int AO_GPS_RUNNING = (1 << 5);
120         static final int AO_GPS_DATE_VALID = (1 << 6);
121         static final int AO_GPS_NUM_SAT_SHIFT = 0;
122         static final int AO_GPS_NUM_SAT_MASK = 0xf;
123
124         static boolean isspace(int c) {
125                 switch (c) {
126                 case ' ':
127                 case '\t':
128                         return true;
129                 }
130                 return false;
131         }
132
133         static boolean ishex(int c) {
134                 if ('0' <= c && c <= '9')
135                         return true;
136                 if ('a' <= c && c <= 'f')
137                         return true;
138                 if ('A' <= c && c <= 'F')
139                         return true;
140                 return false;
141         }
142
143         static boolean ishex(String s) {
144                 for (int i = 0; i < s.length(); i++)
145                         if (!ishex(s.charAt(i)))
146                                 return false;
147                 return true;
148         }
149
150         static int fromhex(int c) {
151                 if ('0' <= c && c <= '9')
152                         return c - '0';
153                 if ('a' <= c && c <= 'f')
154                         return c - 'a' + 10;
155                 if ('A' <= c && c <= 'F')
156                         return c - 'A' + 10;
157                 return -1;
158         }
159
160         static int fromhex(String s) throws NumberFormatException {
161                 int c, v = 0;
162                 for (int i = 0; i < s.length(); i++) {
163                         c = s.charAt(i);
164                         if (!ishex(c)) {
165                                 if (i == 0)
166                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
167                                 return v;
168                         }
169                         v = v * 16 + fromhex(c);
170                 }
171                 return v;
172         }
173
174         static boolean isdec(int c) {
175                 if ('0' <= c && c <= '9')
176                         return true;
177                 return false;
178         }
179
180         static boolean isdec(String s) {
181                 for (int i = 0; i < s.length(); i++)
182                         if (!isdec(s.charAt(i)))
183                                 return false;
184                 return true;
185         }
186
187         static int fromdec(int c) {
188                 if ('0' <= c && c <= '9')
189                         return c - '0';
190                 return -1;
191         }
192
193         static int fromdec(String s) throws NumberFormatException {
194                 int c, v = 0;
195                 int sign = 1;
196                 for (int i = 0; i < s.length(); i++) {
197                         c = s.charAt(i);
198                         if (i == 0 && c == '-') {
199                                 sign = -1;
200                         } else if (!isdec(c)) {
201                                 if (i == 0)
202                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
203                                 return v;
204                         } else
205                                 v = v * 10 + fromdec(c);
206                 }
207                 return v * sign;
208         }
209
210         static String replace_extension(String input, String extension) {
211                 int dot = input.lastIndexOf(".");
212                 if (dot > 0)
213                         input = input.substring(0,dot);
214                 return input.concat(extension);
215         }
216 }