9d5b2e02369d3eef870ac083be1fd881f2c31e04
[fw/altos] / 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         /* Telemetry modes */
67         static final int ao_telemetry_off = 0;
68         static final int ao_telemetry_full = 1;
69         static final int ao_telemetry_tiny = 2;
70
71         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
72
73         static boolean map_initialized = false;
74
75         static final int tab_elt_pad = 5;
76
77         static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
78         static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
79         static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
80
81         static final int text_width = 16;
82
83         static void initialize_map()
84         {
85                 string_to_state.put("startup", ao_flight_startup);
86                 string_to_state.put("idle", ao_flight_idle);
87                 string_to_state.put("pad", ao_flight_pad);
88                 string_to_state.put("boost", ao_flight_boost);
89                 string_to_state.put("fast", ao_flight_fast);
90                 string_to_state.put("coast", ao_flight_coast);
91                 string_to_state.put("drogue", ao_flight_drogue);
92                 string_to_state.put("main", ao_flight_main);
93                 string_to_state.put("landed", ao_flight_landed);
94                 string_to_state.put("invalid", ao_flight_invalid);
95                 map_initialized = true;
96         }
97
98         static String[] state_to_string = {
99                 "startup",
100                 "idle",
101                 "pad",
102                 "boost",
103                 "fast",
104                 "coast",
105                 "drogue",
106                 "main",
107                 "landed",
108                 "invalid",
109         };
110
111         static public int state(String state) {
112                 if (!map_initialized)
113                         initialize_map();
114                 if (string_to_state.containsKey(state))
115                         return string_to_state.get(state);
116                 return ao_flight_invalid;
117         }
118
119         static public String state_name(int state) {
120                 if (state < 0 || state_to_string.length <= state)
121                         return "invalid";
122                 return state_to_string[state];
123         }
124
125         static final int AO_GPS_VALID = (1 << 4);
126         static final int AO_GPS_RUNNING = (1 << 5);
127         static final int AO_GPS_DATE_VALID = (1 << 6);
128         static final int AO_GPS_NUM_SAT_SHIFT = 0;
129         static final int AO_GPS_NUM_SAT_MASK = 0xf;
130
131         static boolean isspace(int c) {
132                 switch (c) {
133                 case ' ':
134                 case '\t':
135                         return true;
136                 }
137                 return false;
138         }
139
140         static boolean ishex(int c) {
141                 if ('0' <= c && c <= '9')
142                         return true;
143                 if ('a' <= c && c <= 'f')
144                         return true;
145                 if ('A' <= c && c <= 'F')
146                         return true;
147                 return false;
148         }
149
150         static boolean ishex(String s) {
151                 for (int i = 0; i < s.length(); i++)
152                         if (!ishex(s.charAt(i)))
153                                 return false;
154                 return true;
155         }
156
157         static int fromhex(int c) {
158                 if ('0' <= c && c <= '9')
159                         return c - '0';
160                 if ('a' <= c && c <= 'f')
161                         return c - 'a' + 10;
162                 if ('A' <= c && c <= 'F')
163                         return c - 'A' + 10;
164                 return -1;
165         }
166
167         static int fromhex(String s) throws NumberFormatException {
168                 int c, v = 0;
169                 for (int i = 0; i < s.length(); i++) {
170                         c = s.charAt(i);
171                         if (!ishex(c)) {
172                                 if (i == 0)
173                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
174                                 return v;
175                         }
176                         v = v * 16 + fromhex(c);
177                 }
178                 return v;
179         }
180
181         static boolean isdec(int c) {
182                 if ('0' <= c && c <= '9')
183                         return true;
184                 return false;
185         }
186
187         static boolean isdec(String s) {
188                 for (int i = 0; i < s.length(); i++)
189                         if (!isdec(s.charAt(i)))
190                                 return false;
191                 return true;
192         }
193
194         static int fromdec(int c) {
195                 if ('0' <= c && c <= '9')
196                         return c - '0';
197                 return -1;
198         }
199
200         static int fromdec(String s) throws NumberFormatException {
201                 int c, v = 0;
202                 int sign = 1;
203                 for (int i = 0; i < s.length(); i++) {
204                         c = s.charAt(i);
205                         if (i == 0 && c == '-') {
206                                 sign = -1;
207                         } else if (!isdec(c)) {
208                                 if (i == 0)
209                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
210                                 return v;
211                         } else
212                                 v = v * 10 + fromdec(c);
213                 }
214                 return v * sign;
215         }
216
217         static String replace_extension(String input, String extension) {
218                 int dot = input.lastIndexOf(".");
219                 if (dot > 0)
220                         input = input.substring(0,dot);
221                 return input.concat(extension);
222         }
223 }