altos/altosui: Log averaged baro sensor data in Tm/Tn
[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         static final int AO_LOG_PRESSURE = 'P';
38
39         /* Added for header fields in eeprom files */
40         static final int AO_LOG_CONFIG_VERSION = 1000;
41         static final int AO_LOG_MAIN_DEPLOY = 1001;
42         static final int AO_LOG_APOGEE_DELAY = 1002;
43         static final int AO_LOG_RADIO_CHANNEL = 1003;
44         static final int AO_LOG_CALLSIGN = 1004;
45         static final int AO_LOG_ACCEL_CAL = 1005;
46         static final int AO_LOG_RADIO_CAL = 1006;
47         static final int AO_LOG_MAX_FLIGHT_LOG = 1007;
48         static final int AO_LOG_MANUFACTURER = 2000;
49         static final int AO_LOG_PRODUCT = 2001;
50         static final int AO_LOG_SERIAL_NUMBER = 2002;
51         static final int AO_LOG_SOFTWARE_VERSION = 9999;
52
53         /* Added to flag invalid records */
54         static final int AO_LOG_INVALID = -1;
55
56         /* Flight state numbers and names */
57         static final int ao_flight_startup = 0;
58         static final int ao_flight_idle = 1;
59         static final int ao_flight_pad = 2;
60         static final int ao_flight_boost = 3;
61         static final int ao_flight_fast = 4;
62         static final int ao_flight_coast = 5;
63         static final int ao_flight_drogue = 6;
64         static final int ao_flight_main = 7;
65         static final int ao_flight_landed = 8;
66         static final int ao_flight_invalid = 9;
67
68         /* Telemetry modes */
69         static final int ao_telemetry_off = 0;
70         static final int ao_telemetry_full = 1;
71         static final int ao_telemetry_tiny = 2;
72
73         static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
74
75         static boolean map_initialized = false;
76
77         static final int tab_elt_pad = 5;
78
79         static final Font label_font = new Font("Dialog", Font.PLAIN, 22);
80         static final Font value_font = new Font("Monospaced", Font.PLAIN, 22);
81         static final Font status_font = new Font("SansSerif", Font.BOLD, 24);
82
83         static final int text_width = 16;
84
85         static void initialize_map()
86         {
87                 string_to_state.put("startup", ao_flight_startup);
88                 string_to_state.put("idle", ao_flight_idle);
89                 string_to_state.put("pad", ao_flight_pad);
90                 string_to_state.put("boost", ao_flight_boost);
91                 string_to_state.put("fast", ao_flight_fast);
92                 string_to_state.put("coast", ao_flight_coast);
93                 string_to_state.put("drogue", ao_flight_drogue);
94                 string_to_state.put("main", ao_flight_main);
95                 string_to_state.put("landed", ao_flight_landed);
96                 string_to_state.put("invalid", ao_flight_invalid);
97                 map_initialized = true;
98         }
99
100         static String[] state_to_string = {
101                 "startup",
102                 "idle",
103                 "pad",
104                 "boost",
105                 "fast",
106                 "coast",
107                 "drogue",
108                 "main",
109                 "landed",
110                 "invalid",
111         };
112
113         static public int state(String state) {
114                 if (!map_initialized)
115                         initialize_map();
116                 if (string_to_state.containsKey(state))
117                         return string_to_state.get(state);
118                 return ao_flight_invalid;
119         }
120
121         static public String state_name(int state) {
122                 if (state < 0 || state_to_string.length <= state)
123                         return "invalid";
124                 return state_to_string[state];
125         }
126
127         static final int AO_GPS_VALID = (1 << 4);
128         static final int AO_GPS_RUNNING = (1 << 5);
129         static final int AO_GPS_DATE_VALID = (1 << 6);
130         static final int AO_GPS_NUM_SAT_SHIFT = 0;
131         static final int AO_GPS_NUM_SAT_MASK = 0xf;
132
133         static boolean isspace(int c) {
134                 switch (c) {
135                 case ' ':
136                 case '\t':
137                         return true;
138                 }
139                 return false;
140         }
141
142         static boolean ishex(int c) {
143                 if ('0' <= c && c <= '9')
144                         return true;
145                 if ('a' <= c && c <= 'f')
146                         return true;
147                 if ('A' <= c && c <= 'F')
148                         return true;
149                 return false;
150         }
151
152         static boolean ishex(String s) {
153                 for (int i = 0; i < s.length(); i++)
154                         if (!ishex(s.charAt(i)))
155                                 return false;
156                 return true;
157         }
158
159         static int fromhex(int c) {
160                 if ('0' <= c && c <= '9')
161                         return c - '0';
162                 if ('a' <= c && c <= 'f')
163                         return c - 'a' + 10;
164                 if ('A' <= c && c <= 'F')
165                         return c - 'A' + 10;
166                 return -1;
167         }
168
169         static int fromhex(String s) throws NumberFormatException {
170                 int c, v = 0;
171                 for (int i = 0; i < s.length(); i++) {
172                         c = s.charAt(i);
173                         if (!ishex(c)) {
174                                 if (i == 0)
175                                         throw new NumberFormatException(String.format("invalid hex \"%s\"", s));
176                                 return v;
177                         }
178                         v = v * 16 + fromhex(c);
179                 }
180                 return v;
181         }
182
183         static boolean isdec(int c) {
184                 if ('0' <= c && c <= '9')
185                         return true;
186                 return false;
187         }
188
189         static boolean isdec(String s) {
190                 for (int i = 0; i < s.length(); i++)
191                         if (!isdec(s.charAt(i)))
192                                 return false;
193                 return true;
194         }
195
196         static int fromdec(int c) {
197                 if ('0' <= c && c <= '9')
198                         return c - '0';
199                 return -1;
200         }
201
202         static int fromdec(String s) throws NumberFormatException {
203                 int c, v = 0;
204                 int sign = 1;
205                 for (int i = 0; i < s.length(); i++) {
206                         c = s.charAt(i);
207                         if (i == 0 && c == '-') {
208                                 sign = -1;
209                         } else if (!isdec(c)) {
210                                 if (i == 0)
211                                         throw new NumberFormatException(String.format("invalid number \"%s\"", s));
212                                 return v;
213                         } else
214                                 v = v * 10 + fromdec(c);
215                 }
216                 return v * sign;
217         }
218
219         static String replace_extension(String input, String extension) {
220                 int dot = input.lastIndexOf(".");
221                 if (dot > 0)
222                         input = input.substring(0,dot);
223                 return input.concat(extension);
224         }
225 }