Switch to universal JavaApplicationStub for Mac OS X
[fw/altos] / micropeak / MicroDownload.java
1 /*
2  * Copyright © 2012 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.micropeak;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import java.util.*;
27 import org.altusmetrum.altoslib_13.*;
28 import org.altusmetrum.altosuilib_13.*;
29
30 public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog, WindowListener {
31         MicroPeak       owner;
32         Container       pane;
33         AltosDevice     device;
34         JButton         cancel;
35         MicroData       data;
36         MicroSerial     serial;
37         LinkedList<Integer> log_queue = new LinkedList<Integer>();
38         Runnable        log_run;
39         JTextArea       serial_log;
40         JLabel          status_value;
41         int             log_column;
42
43         public void windowActivated(WindowEvent e) {
44         }
45
46         public void windowClosed(WindowEvent e) {
47                 setVisible(false);
48                 dispose();
49         }
50
51         public void windowClosing(WindowEvent e) {
52         }
53
54         public void windowDeactivated(WindowEvent e) {
55         }
56
57         public void windowDeiconified(WindowEvent e) {
58         }
59
60         public void windowIconified(WindowEvent e) {
61         }
62
63         public void windowOpened(WindowEvent e) {
64         }
65
66         private void done_internal() {
67                 setVisible(false);
68                 dispose();
69
70                 if (data != null && data.crc_valid) {
71                         status_value.setText("Received MicroPeak Data");
72                         owner = owner.SetData(data);
73                         MicroSave save = new MicroSave(owner, data);
74                         if (save.runDialog())
75                                 owner.SetName(data.name);
76                 } else {
77                         JOptionPane.showMessageDialog(owner,
78                                                       "Download Failed",
79                                                       "Flight data corrupted",
80                                                       JOptionPane.ERROR_MESSAGE);
81                 }
82         }
83
84         public void drain_queue() {
85                 for (;;) {
86                         int     c;
87                         synchronized(this) {
88                                 if (log_queue.isEmpty()) {
89                                         log_run = null;
90                                         break;
91                                 }
92                                 c = log_queue.remove();
93                         }
94                         if (c == '\r')
95                                 continue;
96                         if (c == '\0')
97                                 continue;
98                         String s;
99                         if (c == '\n') {
100                                 s = "\n";
101                                 log_column = 0;
102                         } else if (' ' <= c && c <= '~') {
103                                 byte[] bytes = new byte[1];
104                                 bytes[0] = (byte) c;
105                                 s = new String(bytes, AltosLib.unicode_set);
106                                 log_column += 1;
107                         } else {
108                                 s = String.format("\\0x%02x", c & 0xff);
109                                 log_column += 5;
110                         }
111                         serial_log.append(s);
112                         if (log_column > 40) {
113                                 serial_log.append("\n");
114                                 log_column = 0;
115                         }
116                 }
117         }
118
119         public void log_char(int c) {
120                 synchronized(this) {
121                         log_queue.add(c);
122                         if (log_run == null) {
123                                 log_run = new Runnable() {
124                                                 public void run() {
125                                                         drain_queue();
126                                                 }
127                                         };
128                                 SwingUtilities.invokeLater(log_run);
129                         }
130                 }
131         }
132
133         public void done() {
134                 Runnable r = new Runnable() {
135                                 public void run() {
136                                         try {
137                                                 done_internal();
138                                         } catch (Exception ex) {
139                                         }
140                                 }
141                         };
142                 SwingUtilities.invokeLater(r);
143         }
144
145         public void run() {
146                 try {
147                         for (;;) {
148                                 try {
149                                         data = new MicroData(serial, device.toShortString());
150                                         if (data != null && data.crc_valid)
151                                                 break;
152                                 } catch (MicroData.NonHexcharException nhe) {
153                                 }
154                         }
155                 } catch (FileNotFoundException fe) {
156                 } catch (IOException ioe) {
157                 } catch (InterruptedException ie) {
158                 } catch (MicroData.FileEndedException fee) {
159                 }
160                 serial.close();
161                 done();
162         }
163
164         Thread  serial_thread;
165
166         public void start() {
167                 try {
168                         serial = new MicroSerial(device);
169                         serial.set_log(this);
170                 } catch (FileNotFoundException fe) {
171                         return;
172                 }
173                 serial_thread = new Thread(this);
174                 serial_thread.start();
175         }
176
177         public void actionPerformed(ActionEvent ae) {
178                 if (serial_thread != null) {
179                         serial.close();
180                         serial_thread.interrupt();
181                 }
182                 setVisible(false);
183         }
184
185         public MicroDownload(MicroPeak owner, AltosDevice device) {
186                 super (owner, "Download MicroPeak Data", false);
187
188                 int y = 0;
189
190                 GridBagConstraints c;
191                 Insets il = new Insets(4,4,4,4);
192                 Insets ir = new Insets(4,4,4,4);
193
194                 this.owner = owner;
195                 this.device = device;
196
197                 pane = getContentPane();
198                 pane.setLayout(new GridBagLayout());
199
200                 c = new GridBagConstraints();
201                 c.gridx = 0; c.gridy = y;
202                 c.fill = GridBagConstraints.NONE;
203                 c.anchor = GridBagConstraints.LINE_START;
204                 c.insets = il;
205                 JLabel device_label = new JLabel("Device:");
206                 pane.add(device_label, c);
207
208                 c = new GridBagConstraints();
209                 c.gridx = 1; c.gridy = y;
210                 c.fill = GridBagConstraints.NONE;
211                 c.weightx = 1;
212                 c.anchor = GridBagConstraints.LINE_START;
213                 c.insets = ir;
214                 JLabel device_value = new JLabel(device.toString());
215                 pane.add(device_value, c);
216                 y++;
217
218                 c = new GridBagConstraints();
219                 c.gridx = 0; c.gridy = y;
220                 c.gridwidth = GridBagConstraints.REMAINDER;
221                 c.fill = GridBagConstraints.HORIZONTAL;
222                 c.weightx = 0;
223                 c.anchor = GridBagConstraints.LINE_START;
224                 c.insets = ir;
225                 JLabel help_text = new JLabel(
226                         "<html><i>Turn on the MicroPeak and place the LED inside the<br>" +
227                         "opening in the top of the MicroPeak USB adapter.<br> " +
228                         "Verify that the blue LED in the side of the USB adapter<br>" +
229                         "is blinking along with the orange LED on the MicroPeak.</i></html>");
230 //              help_text.setEditable(false);
231
232                 pane.add(help_text, c);
233                 y++;
234
235                 c = new GridBagConstraints();
236                 c.gridx = 0; c.gridy = y;
237                 c.gridwidth = 2;
238                 c.fill = GridBagConstraints.HORIZONTAL;
239                 c.weightx = 1;
240                 c.anchor = GridBagConstraints.LINE_START;
241                 c.insets = ir;
242                 status_value = new JLabel("Waiting for MicroPeak data...");
243                 pane.add(status_value, c);
244                 y++;
245
246                 serial_log = new JTextArea(10, 20);
247                 serial_log.setEditable(false);
248
249                 JScrollPane serial_scroll = new JScrollPane(serial_log);
250                 serial_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
251
252                 c = new GridBagConstraints();
253                 c.gridx = 0; c.gridy = y;
254                 c.gridwidth = GridBagConstraints.REMAINDER;
255                 c.fill = GridBagConstraints.BOTH;
256                 c.weightx = 1;
257                 c.weighty = 1;
258                 c.anchor = GridBagConstraints.LINE_START;
259                 c.insets = ir;
260
261                 pane.add(serial_scroll, c);
262                 y++;
263
264                 cancel = new JButton("Cancel");
265                 c = new GridBagConstraints();
266                 c.fill = GridBagConstraints.NONE;
267                 c.anchor = GridBagConstraints.EAST;
268                 c.gridx = 0; c.gridy = y;
269                 c.gridwidth = GridBagConstraints.REMAINDER;
270                 Insets ic = new Insets(4,4,4,4);
271                 c.insets = ic;
272                 pane.add(cancel, c);
273                 y++;
274
275                 cancel.addActionListener(this);
276
277                 pack();
278                 setLocationRelativeTo(owner);
279                 setVisible(true);
280                 start();
281         }
282 }