2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
33 public class AltosUI extends JFrame {
34 public AltosVoice voice = new AltosVoice();
36 public static boolean load_library(Frame frame) {
37 if (!AltosDevice.load_library()) {
38 JOptionPane.showMessageDialog(frame,
39 String.format("No AltOS library in \"%s\"",
40 System.getProperty("java.library.path","<undefined>")),
41 "Cannot load device access library",
42 JOptionPane.ERROR_MESSAGE);
48 void telemetry_window(AltosDevice device) {
50 AltosFlightReader reader = new AltosTelemetryReader(device);
52 new AltosFlightUI(voice, reader, device.getSerial());
53 } catch (FileNotFoundException ee) {
54 JOptionPane.showMessageDialog(AltosUI.this,
55 String.format("Cannot open device \"%s\"",
57 "Cannot open target device",
58 JOptionPane.ERROR_MESSAGE);
59 } catch (AltosSerialInUseException si) {
60 JOptionPane.showMessageDialog(AltosUI.this,
61 String.format("Device \"%s\" already in use",
64 JOptionPane.ERROR_MESSAGE);
65 } catch (IOException ee) {
66 JOptionPane.showMessageDialog(AltosUI.this,
69 JOptionPane.ERROR_MESSAGE);
74 GridBagLayout gridbag;
76 JButton addButton(int x, int y, String label) {
80 c = new GridBagConstraints();
81 c.gridx = x; c.gridy = y;
82 c.fill = GridBagConstraints.BOTH;
85 b = new JButton(label);
87 Dimension ps = b.getPreferredSize();
89 gridbag.setConstraints(b, c);
98 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
100 setIconImage(new ImageIcon(imgURL).getImage());
102 AltosPreferences.init(this);
104 pane = getContentPane();
105 gridbag = new GridBagLayout();
106 pane.setLayout(gridbag);
110 b = addButton(0, 0, "Monitor Flight");
111 b.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e) {
116 b = addButton(1, 0, "Save Flight Data");
117 b.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
122 b = addButton(2, 0, "Replay Flight");
123 b.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) {
128 b = addButton(0, 1, "Graph Data");
129 b.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent e) {
134 b = addButton(1, 1, "Export Data");
135 b.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e) {
140 b = addButton(2, 1, "Configure TeleMetrum");
141 b.addActionListener(new ActionListener() {
142 public void actionPerformed(ActionEvent e) {
143 ConfigureTeleMetrum();
147 b = addButton(0, 2, "Configure AltosUI");
148 b.addActionListener(new ActionListener() {
149 public void actionPerformed(ActionEvent e) {
154 b = addButton(1, 2, "Flash Image");
155 b.addActionListener(new ActionListener() {
156 public void actionPerformed(ActionEvent e) {
161 b = addButton(2, 2, "Quit");
162 b.addActionListener(new ActionListener() {
163 public void actionPerformed(ActionEvent e) {
178 Insets i = getInsets();
179 Dimension ps = rootPane.getPreferredSize();
180 ps.width += i.left + i.right;
181 ps.height += i.top + i.bottom;
182 setPreferredSize(ps);
184 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
185 addWindowListener(new WindowAdapter() {
187 public void windowClosing(WindowEvent e) {
193 private void ConnectToDevice() {
194 AltosDevice device = AltosDeviceDialog.show(AltosUI.this,
195 AltosDevice.product_basestation);
198 telemetry_window(device);
201 void ConfigureCallsign() {
203 result = JOptionPane.showInputDialog(AltosUI.this,
204 "Configure Callsign",
205 AltosPreferences.callsign());
207 AltosPreferences.set_callsign(result);
210 void ConfigureTeleMetrum() {
211 new AltosConfig(AltosUI.this);
215 new AltosFlashUI(AltosUI.this);
219 * Replay a flight from telemetry data
221 private void Replay() {
222 AltosLogfileChooser chooser = new AltosLogfileChooser(
224 AltosRecordIterable iterable = chooser.runDialog();
225 if (iterable != null) {
226 AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
228 new AltosFlightUI(voice, reader);
232 /* Connect to TeleMetrum, either directly or through
233 * a TeleDongle over the packet link
235 private void SaveFlightData() {
236 new AltosEepromDownload(AltosUI.this);
239 /* Load a flight log file and write out a CSV file containing
240 * all of the data in standard units
243 private void ExportData() {
244 new AltosCSVUI(AltosUI.this);
247 /* Load a flight log CSV file and display a pretty graph.
250 private void GraphData() {
251 new AltosGraphUI(AltosUI.this);
254 private void ConfigureAltosUI() {
255 new AltosConfigureUI(AltosUI.this, voice);
258 static AltosRecordIterable open_logfile(String filename) {
259 File file = new File (filename);
263 in = new FileInputStream(file);
264 if (filename.endsWith("eeprom"))
265 return new AltosEepromIterable(in);
267 return new AltosTelemetryIterable(in);
268 } catch (FileNotFoundException fe) {
269 System.out.printf("Cannot open '%s'\n", filename);
274 static AltosWriter open_csv(String filename) {
275 File file = new File (filename);
277 return new AltosCSV(file);
278 } catch (FileNotFoundException fe) {
279 System.out.printf("Cannot open '%s'\n", filename);
284 static AltosWriter open_kml(String filename) {
285 File file = new File (filename);
287 return new AltosKML(file);
288 } catch (FileNotFoundException fe) {
289 System.out.printf("Cannot open '%s'\n", filename);
294 static final int process_csv = 1;
295 static final int process_kml = 2;
297 static void process_file(String input, int process) {
298 AltosRecordIterable iterable = open_logfile(input);
299 if (iterable == null)
302 process = process_csv;
303 if ((process & process_csv) != 0) {
304 String output = Altos.replace_extension(input,".csv");
305 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
306 if (input.equals(output)) {
307 System.out.printf("Not processing '%s'\n", input);
309 AltosWriter writer = open_csv(output);
310 if (writer != null) {
311 writer.write(iterable);
316 if ((process & process_kml) != 0) {
317 String output = Altos.replace_extension(input,".kml");
318 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
319 if (input.equals(output)) {
320 System.out.printf("Not processing '%s'\n", input);
322 AltosWriter writer = open_kml(output);
325 writer.write(iterable);
331 public static void main(final String[] args) {
333 /* Handle batch-mode */
334 if (args.length == 2 && args[0].equals("--replay")) {
335 String filename = args[1];
338 in = new FileInputStream(filename);
339 } catch (Exception e) {
340 System.out.printf("Failed to open file '%s'\n", filename);
343 AltosRecordIterable recs;
344 AltosReplayReader reader;
345 if (filename.endsWith("eeprom")) {
346 recs = new AltosEepromIterable(in);
348 recs = new AltosTelemetryIterable(in);
350 reader = new AltosReplayReader(recs.iterator(), filename);
351 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
352 flight_ui.set_exit_on_close();
354 } else if (args.length > 0) {
355 for (int i = 0; i < args.length; i++) {
356 if (args[i].equals("--kml"))
357 process |= process_kml;
358 else if (args[i].equals("--csv"))
359 process |= process_csv;
361 process_file(args[i], process);
364 AltosUI altosui = new AltosUI();
365 altosui.setVisible(true);
367 AltosDevice[] devices = AltosDevice.list(AltosDevice.product_basestation);
368 for (int i = 0; i < devices.length; i++)
369 altosui.telemetry_window(devices[i]);