7e3d1e593dfc6658d737902ec5546afecdffb905
[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_14.*;
28 import org.altusmetrum.altosuilib_14.*;
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                 if (data != null && data.crc_valid) {
68                         if (data.nsamples == 0) {
69                                 JOptionPane.showMessageDialog(owner,
70                                                               "No Flight Data Present",
71                                                               "Empty Log",
72                                                               JOptionPane.WARNING_MESSAGE);
73                         } else {
74                                 status_value.setText("Received MicroPeak Data");
75                                 owner = owner.SetData(data);
76                                 MicroSave save = new MicroSave(owner, data);
77                                 if (save.runDialog())
78                                         owner.SetName(data.name);
79                         }
80                 } else {
81                         JOptionPane.showMessageDialog(owner,
82                                                       "Download Failed",
83                                                       "Flight data corrupted",
84                                                       JOptionPane.ERROR_MESSAGE);
85                 }
86                 setVisible(false);
87                 dispose();
88         }
89
90         public void drain_queue() {
91                 for (;;) {
92                         int     c;
93                         synchronized(this) {
94                                 if (log_queue.isEmpty()) {
95                                         log_run = null;
96                                         break;
97                                 }
98                                 c = log_queue.remove();
99                         }
100                         if (c == '\r')
101                                 continue;
102                         if (c == '\0')
103                                 continue;
104                         String s;
105                         if (c == '\n') {
106                                 s = "\n";
107                                 log_column = 0;
108                         } else if (' ' <= c && c <= '~') {
109                                 byte[] bytes = new byte[1];
110                                 bytes[0] = (byte) c;
111                                 s = new String(bytes, AltosLib.unicode_set);
112                                 log_column += 1;
113                         } else {
114                                 s = String.format("\\0x%02x", c & 0xff);
115                                 log_column += 5;
116                         }
117                         serial_log.append(s);
118                         if (log_column > 40) {
119                                 serial_log.append("\n");
120                                 log_column = 0;
121                         }
122                 }
123         }
124
125         public void log_char(int c) {
126                 synchronized(this) {
127                         log_queue.add(c);
128                         if (log_run == null) {
129                                 log_run = new Runnable() {
130                                                 public void run() {
131                                                         drain_queue();
132                                                 }
133                                         };
134                                 SwingUtilities.invokeLater(log_run);
135                         }
136                 }
137         }
138
139         public void done() {
140                 Runnable r = new Runnable() {
141                                 public void run() {
142                                         try {
143                                                 done_internal();
144                                         } catch (Exception ex) {
145                                         }
146                                 }
147                         };
148                 SwingUtilities.invokeLater(r);
149         }
150
151         public void run() {
152                 try {
153                         for (;;) {
154                                 try {
155                                         data = new MicroData(serial, device.toShortString());
156                                         if (data != null && data.crc_valid)
157                                                 break;
158                                 } catch (MicroData.NonHexcharException nhe) {
159                                 }
160                         }
161                         write_thread.join();
162                 } catch (FileNotFoundException fe) {
163                 } catch (IOException ioe) {
164                 } catch (InterruptedException ie) {
165                 } catch (MicroData.FileEndedException fee) {
166                 }
167                 serial.close();
168                 done();
169         }
170
171         Thread  serial_thread;
172         Thread  write_thread;
173
174         public class SerialWriter implements Runnable {
175                 MicroSerial serial;
176
177                 public void run () {
178                         try {
179                                 Thread.sleep(100);
180                                 serial.write('l');
181                                 serial.write('\n');
182                                 serial.flush();
183                         } catch (InterruptedException ie) {
184                         }
185                 }
186
187                 public SerialWriter(MicroSerial serial) {
188                         this.serial = serial;
189                 }
190         }
191
192         public void start() {
193                 try {
194                         serial = new MicroSerial(device);
195                         serial.set_log(this);
196                 } catch (FileNotFoundException fe) {
197                         return;
198                 }
199                 serial_thread = new Thread(this);
200                 serial_thread.start();
201
202                 SerialWriter writer = new SerialWriter(serial);
203                 write_thread = new Thread(writer);
204                 write_thread.start();
205         }
206
207         public void actionPerformed(ActionEvent ae) {
208                 if (serial_thread != null) {
209                         serial.close();
210                         serial_thread.interrupt();
211                 }
212                 setVisible(false);
213         }
214
215         public MicroDownload(MicroPeak owner, AltosDevice device) {
216                 super (owner, "Download MicroPeak Data", false);
217
218                 int y = 0;
219
220                 GridBagConstraints c;
221                 Insets il = new Insets(4,4,4,4);
222                 Insets ir = new Insets(4,4,4,4);
223
224                 this.owner = owner;
225                 this.device = device;
226
227                 pane = getScrollablePane();
228                 pane.setLayout(new GridBagLayout());
229
230                 c = new GridBagConstraints();
231                 c.gridx = 0; c.gridy = y;
232                 c.fill = GridBagConstraints.NONE;
233                 c.anchor = GridBagConstraints.LINE_START;
234                 c.insets = il;
235                 JLabel device_label = new JLabel("Device:");
236                 pane.add(device_label, c);
237
238                 c = new GridBagConstraints();
239                 c.gridx = 1; c.gridy = y;
240                 c.fill = GridBagConstraints.NONE;
241                 c.weightx = 1;
242                 c.anchor = GridBagConstraints.LINE_START;
243                 c.insets = ir;
244                 JLabel device_value = new JLabel(device.toString());
245                 pane.add(device_value, c);
246                 y++;
247
248                 c = new GridBagConstraints();
249                 c.gridx = 0; c.gridy = y;
250                 c.gridwidth = GridBagConstraints.REMAINDER;
251                 c.fill = GridBagConstraints.HORIZONTAL;
252                 c.weightx = 0;
253                 c.anchor = GridBagConstraints.LINE_START;
254                 c.insets = ir;
255                 JLabel help_text = new JLabel(
256                         "<html><i>Turn on the MicroPeak and place the LED inside the<br>" +
257                         "opening in the top of the MicroPeak USB adapter.<br> " +
258                         "Verify that the blue LED in the side of the USB adapter<br>" +
259                         "is blinking along with the orange LED on the MicroPeak.</i></html>");
260 //              help_text.setEditable(false);
261
262                 pane.add(help_text, c);
263                 y++;
264
265                 c = new GridBagConstraints();
266                 c.gridx = 0; c.gridy = y;
267                 c.gridwidth = 2;
268                 c.fill = GridBagConstraints.HORIZONTAL;
269                 c.weightx = 1;
270                 c.anchor = GridBagConstraints.LINE_START;
271                 c.insets = ir;
272                 status_value = new JLabel("Waiting for MicroPeak data...");
273                 pane.add(status_value, c);
274                 y++;
275
276                 serial_log = new JTextArea(10, 20);
277                 serial_log.setEditable(false);
278
279                 JScrollPane serial_scroll = new JScrollPane(serial_log);
280                 serial_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
281
282                 c = new GridBagConstraints();
283                 c.gridx = 0; c.gridy = y;
284                 c.gridwidth = GridBagConstraints.REMAINDER;
285                 c.fill = GridBagConstraints.BOTH;
286                 c.weightx = 1;
287                 c.weighty = 1;
288                 c.anchor = GridBagConstraints.LINE_START;
289                 c.insets = ir;
290
291                 pane.add(serial_scroll, c);
292                 y++;
293
294                 cancel = new JButton("Cancel");
295                 c = new GridBagConstraints();
296                 c.fill = GridBagConstraints.NONE;
297                 c.anchor = GridBagConstraints.EAST;
298                 c.gridx = 0; c.gridy = y;
299                 c.gridwidth = GridBagConstraints.REMAINDER;
300                 Insets ic = new Insets(4,4,4,4);
301                 c.insets = ic;
302                 pane.add(cancel, c);
303                 y++;
304
305                 cancel.addActionListener(this);
306
307                 pack();
308                 setLocationRelativeTo(owner);
309                 setVisible(true);
310                 start();
311         }
312 }