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