altosui: Skip unknown data when parsing ADC for idle monitoring
[fw/altos] / altosui / AltosIdleMonitorUI.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.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30 import org.altusmetrum.AltosLib.*;
31
32 class AltosADC {
33         int     tick;
34         int     accel;
35         int     pres;
36         int     temp;
37         int     batt;
38         int     drogue;
39         int     main;
40
41         public AltosADC(AltosSerial serial) throws InterruptedException, TimeoutException {
42                 serial.printf("a\n");
43                 for (;;) {
44                         String line = serial.get_reply_no_dialog(5000);
45                         if (line == null) {
46                                 throw new TimeoutException();
47                         }
48                         if (!line.startsWith("tick:"))
49                                 continue;
50                         String[] items = line.split("\\s+");
51                         for (int i = 0; i < items.length;) {
52                                 if (items[i].equals("tick:")) {
53                                         tick = Integer.parseInt(items[i+1]);
54                                         i += 2;
55                                         continue;
56                                 }
57                                 if (items[i].equals("accel:")) {
58                                         accel = Integer.parseInt(items[i+1]);
59                                         i += 2;
60                                         continue;
61                                 }
62                                 if (items[i].equals("pres:")) {
63                                         pres = Integer.parseInt(items[i+1]);
64                                         i += 2;
65                                         continue;
66                                 }
67                                 if (items[i].equals("temp:")) {
68                                         temp = Integer.parseInt(items[i+1]);
69                                         i += 2;
70                                         continue;
71                                 }
72                                 if (items[i].equals("batt:")) {
73                                         batt = Integer.parseInt(items[i+1]);
74                                         i += 2;
75                                         continue;
76                                 }
77                                 if (items[i].equals("drogue:")) {
78                                         drogue = Integer.parseInt(items[i+1]);
79                                         i += 2;
80                                         continue;
81                                 }
82                                 if (items[i].equals("main:")) {
83                                         main = Integer.parseInt(items[i+1]);
84                                         i += 2;
85                                         continue;
86                                 }
87                                 i++;
88                         }
89                         break;
90                 }
91         }
92 }
93
94 class AltosGPSQuery extends AltosGPS {
95         public AltosGPSQuery (AltosSerial serial, AltosConfigData config_data)
96                 throws TimeoutException, InterruptedException {
97                 boolean says_done = config_data.compare_version("1.0") >= 0;
98                 serial.printf("g\n");
99                 for (;;) {
100                         String line = serial.get_reply_no_dialog(5000);
101                         if (line == null)
102                                 throw new TimeoutException();
103                         String[] bits = line.split("\\s+");
104                         if (bits.length == 0)
105                                 continue;
106                         if (line.startsWith("Date:")) {
107                                 if (bits.length < 2)
108                                         continue;
109                                 String[] d = bits[1].split(":");
110                                 if (d.length < 3)
111                                         continue;
112                                 year = Integer.parseInt(d[0]) + 2000;
113                                 month = Integer.parseInt(d[1]);
114                                 day = Integer.parseInt(d[2]);
115                                 continue;
116                         }
117                         if (line.startsWith("Time:")) {
118                                 if (bits.length < 2)
119                                         continue;
120                                 String[] d = bits[1].split("/");
121                                 if (d.length < 3)
122                                         continue;
123                                 hour = Integer.parseInt(d[0]);
124                                 minute = Integer.parseInt(d[1]);
125                                 second = Integer.parseInt(d[2]);
126                                 continue;
127                         }
128                         if (line.startsWith("Lat/Lon:")) {
129                                 if (bits.length < 3)
130                                         continue;
131                                 lat = Integer.parseInt(bits[1]) * 1.0e-7;
132                                 lon = Integer.parseInt(bits[2]) * 1.0e-7;
133                                 continue;
134                         }
135                         if (line.startsWith("Alt:")) {
136                                 if (bits.length < 2)
137                                         continue;
138                                 alt = Integer.parseInt(bits[1]);
139                                 continue;
140                         }
141                         if (line.startsWith("Flags:")) {
142                                 if (bits.length < 2)
143                                         continue;
144                                 int status = Integer.decode(bits[1]);
145                                 connected = (status & Altos.AO_GPS_RUNNING) != 0;
146                                 locked = (status & Altos.AO_GPS_VALID) != 0;
147                                 if (!says_done)
148                                         break;
149                                 continue;
150                         }
151                         if (line.startsWith("Sats:")) {
152                                 if (bits.length < 2)
153                                         continue;
154                                 nsat = Integer.parseInt(bits[1]);
155                                 cc_gps_sat = new AltosGPSSat[nsat];
156                                 for (int i = 0; i < nsat; i++) {
157                                         int     svid = Integer.parseInt(bits[2+i*2]);
158                                         int     cc_n0 = Integer.parseInt(bits[3+i*2]);
159                                         cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
160                                 }
161                         }
162                         if (line.startsWith("done"))
163                                 break;
164                         if (line.startsWith("Syntax error"))
165                                 break;
166                 }
167         }
168 }
169
170 class AltosIdleMonitor extends Thread {
171         AltosDevice             device;
172         AltosSerial             serial;
173         AltosIdleMonitorUI      ui;
174         AltosState              state;
175         boolean                 remote;
176         double                  frequency;
177         AltosState              previous_state;
178         AltosConfigData         config_data;
179         AltosADC                adc;
180         AltosGPS                gps;
181
182         int AltosRSSI() throws TimeoutException, InterruptedException {
183                 serial.printf("s\n");
184                 String line = serial.get_reply_no_dialog(5000);
185                 if (line == null)
186                         throw new TimeoutException();
187                 String[] items = line.split("\\s+");
188                 if (items.length < 2)
189                         return 0;
190                 if (!items[0].equals("RSSI:"))
191                         return 0;
192                 int rssi = Integer.parseInt(items[1]);
193                 return rssi;
194         }
195
196         void update_state() throws InterruptedException, TimeoutException {
197                 AltosRecordTM   record = new AltosRecordTM();
198                 int             rssi;
199
200                 try {
201                         if (remote) {
202                                 serial.set_radio_frequency(frequency);
203                                 serial.start_remote();
204                         } else
205                                 serial.flush_input();
206                         config_data = new AltosConfigData(serial);
207                         adc = new AltosADC(serial);
208                         gps = new AltosGPSQuery(serial, config_data);
209                 } finally {
210                         if (remote) {
211                                 serial.stop_remote();
212                                 rssi = AltosRSSI();
213                         } else
214                                 rssi = 0;
215                 }
216
217                 record.version = 0;
218                 record.callsign = config_data.callsign;
219                 record.serial = config_data.serial;
220                 record.flight = config_data.log_available() > 0 ? 255 : 0;
221                 record.rssi = rssi;
222                 record.status = 0;
223                 record.state = Altos.ao_flight_idle;
224
225                 record.tick = adc.tick;
226
227                 record.accel = adc.accel;
228                 record.pres = adc.pres;
229                 record.batt = adc.batt;
230                 record.temp = adc.temp;
231                 record.drogue = adc.drogue;
232                 record.main = adc.main;
233
234                 record.ground_accel = record.accel;
235                 record.ground_pres = record.pres;
236                 record.accel_plus_g = config_data.accel_cal_plus;
237                 record.accel_minus_g = config_data.accel_cal_minus;
238                 record.acceleration = 0;
239                 record.speed = 0;
240                 record.height = 0;
241                 record.gps = gps;
242                 state = new AltosState (record, state);
243         }
244
245         void set_frequency(double in_frequency) {
246                 frequency = in_frequency;
247         }
248
249         public void post_state() {
250                 Runnable r = new Runnable() {
251                                 public void run() {
252                                         ui.update(state);
253                                 }
254                         };
255                 SwingUtilities.invokeLater(r);
256         }
257
258         public void run() {
259                 try {
260                         for (;;) {
261                                 try {
262                                         update_state();
263                                         post_state();
264                                 } catch (TimeoutException te) {
265                                         if (AltosSerial.debug)
266                                                 System.out.printf ("monitor idle data timeout\n");
267                                 }
268                                 Thread.sleep(1000);
269                         }
270                 } catch (InterruptedException ie) {
271                         serial.close();
272                 }
273         }
274
275         public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
276                 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
277                 device = in_device;
278                 ui = in_ui;
279                 serial = new AltosSerial(device);
280                 remote = in_remote;
281                 state = null;
282         }
283 }
284
285 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
286         AltosDevice             device;
287         JTabbedPane             pane;
288         AltosPad                pad;
289         AltosInfoTable          flightInfo;
290         AltosFlightStatus       flightStatus;
291         AltosIdleMonitor        thread;
292         int                     serial;
293         boolean                 remote;
294
295         void stop_display() {
296                 if (thread != null && thread.isAlive()) {
297                         thread.interrupt();
298                         try {
299                                 thread.join();
300                         } catch (InterruptedException ie) {}
301                 }
302                 thread = null;
303         }
304
305         void disconnect() {
306                 stop_display();
307         }
308
309         public void reset() {
310                 pad.reset();
311                 flightInfo.clear();
312         }
313
314         public void set_font() {
315                 pad.set_font();
316                 flightInfo.set_font();
317         }
318
319         public void font_size_changed(int font_size) {
320                 set_font();
321         }
322
323         AltosFlightStatusUpdate status_update;
324
325         public void show(AltosState state, int crc_errors) {
326                 status_update.saved_state = state;
327                 try {
328                         pad.show(state, crc_errors);
329                         flightStatus.show(state, crc_errors);
330                         flightInfo.show(state, crc_errors);
331                 } catch (Exception e) {
332                         System.out.print("Show exception" + e);
333                 }
334         }
335
336         public void update(AltosState state) {
337                 show (state, 0);
338         }
339
340         Container       bag;
341         AltosFreqList   frequencies;
342
343         public AltosIdleMonitorUI(JFrame in_owner)
344                 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
345
346                 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
347                 remote = false;
348                 if (!device.matchProduct(Altos.product_altimeter))
349                         remote = true;
350
351                 serial = device.getSerial();
352                 bag = getContentPane();
353                 bag.setLayout(new GridBagLayout());
354
355                 GridBagConstraints c = new GridBagConstraints();
356
357                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
358                 if (imgURL != null)
359                         setIconImage(new ImageIcon(imgURL).getImage());
360
361                 setTitle(String.format("AltOS %s", device.toShortString()));
362
363                 /* Stick frequency selector at top of table for telemetry monitoring */
364                 if (remote && serial >= 0) {
365                         // Frequency menu
366                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
367                         frequencies.addActionListener(new ActionListener() {
368                                         public void actionPerformed(ActionEvent e) {
369                                                 double frequency = frequencies.frequency();
370                                                 thread.set_frequency(frequency);
371                                                 AltosUIPreferences.set_frequency(device.getSerial(),
372                                                                                frequency);
373                                         }
374                         });
375                         c.gridx = 0;
376                         c.gridy = 0;
377                         c.insets = new Insets(3, 3, 3, 3);
378                         c.anchor = GridBagConstraints.WEST;
379                         bag.add (frequencies, c);
380                 }
381
382
383                 /* Flight status is always visible */
384                 flightStatus = new AltosFlightStatus();
385                 c.gridx = 0;
386                 c.gridy = 1;
387                 c.fill = GridBagConstraints.HORIZONTAL;
388                 c.weightx = 1;
389                 c.gridwidth = 2;
390                 bag.add(flightStatus, c);
391                 c.gridwidth = 1;
392
393                 /* The rest of the window uses a tabbed pane to
394                  * show one of the alternate data views
395                  */
396                 pane = new JTabbedPane();
397
398                 pad = new AltosPad();
399                 pane.add("Launch Pad", pad);
400
401                 flightInfo = new AltosInfoTable();
402                 pane.add("Table", new JScrollPane(flightInfo));
403
404                 /* Make the tabbed pane use the rest of the window space */
405                 c.gridx = 0;
406                 c.gridy = 2;
407                 c.fill = GridBagConstraints.BOTH;
408                 c.weightx = 1;
409                 c.weighty = 1;
410                 c.gridwidth = 2;
411                 bag.add(pane, c);
412
413                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
414
415                 AltosUIPreferences.register_font_listener(this);
416
417                 addWindowListener(new WindowAdapter() {
418                                 @Override
419                                 public void windowClosing(WindowEvent e) {
420                                         disconnect();
421                                         setVisible(false);
422                                         dispose();
423                                         AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
424                                 }
425                         });
426
427                 pack();
428                 setVisible(true);
429
430                 thread = new AltosIdleMonitor(this, device, remote);
431
432                 status_update = new AltosFlightStatusUpdate(flightStatus);
433
434                 new javax.swing.Timer(100, status_update).start();
435
436                 thread.start();
437         }
438 }