cd109b796bd71b7be99bf3d8c3b88befb6262fe3
[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; 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 org.altusmetrum.micropeak;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.concurrent.*;
25 import java.util.*;
26 import org.altusmetrum.altoslib_3.*;
27 import org.altusmetrum.altosuilib_1.*;
28
29 public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog {
30         MicroPeak       owner;
31         Container       pane;
32         AltosDevice     device;
33         JButton         cancel;
34         MicroData       data;
35         MicroSerial     serial;
36         LinkedList<Integer> log_queue = new LinkedList<Integer>();
37         Runnable        log_run;
38         JTextArea       serial_log;
39         JLabel          status_value;
40
41         private void done_internal() {
42                 if (data != null) {
43                         status_value.setText("Received MicroPeak Data");
44                         if (data.crc_valid) {
45                                 owner = owner.SetData(data);
46                                 MicroSave save = new MicroSave(owner, data);
47                                 if (save.runDialog())
48                                         owner.SetName(data.name);
49                         } else {
50                                 JOptionPane.showMessageDialog(owner,
51                                                               "Flight data corrupted",
52                                                               "Download Failed",
53                                                               JOptionPane.ERROR_MESSAGE);
54                         }
55                         setVisible(false);
56                         dispose();
57                 } else {
58                         status_value.setText("Download Failed");
59                 }
60         }
61
62         public void drain_queue() {
63                 for (;;) {
64                         int     c;
65                         synchronized(this) {
66                                 if (log_queue.isEmpty()) {
67                                         log_run = null;
68                                         break;
69                                 }
70                                 c = log_queue.remove();
71                         }
72                         if (c == '\r')
73                                 continue;
74                         byte[] bytes = new byte[1];
75                         bytes[0] = (byte) c;
76                         serial_log.append(new String(bytes, AltosLib.unicode_set));
77                 }
78         }
79
80         public void log_char(int c) {
81                 synchronized(this) {
82                         log_queue.add(c);
83                         if (log_run == null) {
84                                 log_run = new Runnable() {
85                                                 public void run() {
86                                                         drain_queue();
87                                                 }
88                                         };
89                                 SwingUtilities.invokeLater(log_run);
90                         }
91                 }
92         }
93
94         public void done() {
95                 Runnable r = new Runnable() {
96                                 public void run() {
97                                         try {
98                                                 done_internal();
99                                         } catch (Exception ex) {
100                                         }
101                                 }
102                         };
103                 SwingUtilities.invokeLater(r);
104         }
105
106         public void run() {
107                 try {
108                         data = new MicroData(serial, device.toShortString());
109                         serial.close();
110                 } catch (FileNotFoundException fe) {
111                 } catch (IOException ioe) {
112                 } catch (InterruptedException ie) {
113                 }
114                 done();
115         }
116
117         Thread  serial_thread;
118
119         public void start() {
120                 try {
121                         serial = new MicroSerial(device);
122                         serial.set_log(this);
123                 } catch (FileNotFoundException fe) {
124                         return;
125                 }
126                 serial_thread = new Thread(this);
127                 serial_thread.start();
128         }
129
130         public void actionPerformed(ActionEvent ae) {
131                 if (serial_thread != null) {
132                         serial.close();
133                         serial_thread.interrupt();
134                 }
135                 setVisible(false);
136         }
137
138         public MicroDownload(MicroPeak owner, AltosDevice device) {
139                 super (owner, "Download MicroPeak Data", false);
140
141                 int y = 0;
142
143                 GridBagConstraints c;
144                 Insets il = new Insets(4,4,4,4);
145                 Insets ir = new Insets(4,4,4,4);
146
147                 this.owner = owner;
148                 this.device = device;
149
150                 pane = getContentPane();
151                 pane.setLayout(new GridBagLayout());
152
153                 c = new GridBagConstraints();
154                 c.gridx = 0; c.gridy = y;
155                 c.fill = GridBagConstraints.NONE;
156                 c.anchor = GridBagConstraints.LINE_START;
157                 c.insets = il;
158                 JLabel device_label = new JLabel("Device:");
159                 pane.add(device_label, c);
160
161                 c = new GridBagConstraints();
162                 c.gridx = 1; c.gridy = y;
163                 c.fill = GridBagConstraints.NONE;
164                 c.weightx = 1;
165                 c.anchor = GridBagConstraints.LINE_START;
166                 c.insets = ir;
167                 JLabel device_value = new JLabel(device.toString());
168                 pane.add(device_value, c);
169                 y++;
170
171                 c = new GridBagConstraints();
172                 c.gridx = 0; c.gridy = y;
173                 c.gridwidth = GridBagConstraints.REMAINDER;
174                 c.fill = GridBagConstraints.HORIZONTAL;
175                 c.weightx = 1;
176                 c.anchor = GridBagConstraints.LINE_START;
177                 c.insets = ir;
178                 JLabel help_text = new JLabel(
179                         "<html><i>Turn on the MicroPeak and place the LED inside the<br>" +
180                         "opening in the top of the MicroPeak USB adapter.<br>" +
181                         "Verify that the blue LED in the side of the USB adapter<br>" +
182                         "is blinking along with the orange LED on the MicroPeak.</i></html>");
183 //              help_text.setEditable(false);
184
185                 pane.add(help_text, c);
186                 y++;
187
188                 c = new GridBagConstraints();
189                 c.gridx = 0; c.gridy = y;
190                 c.gridwidth = 2;
191                 c.fill = GridBagConstraints.HORIZONTAL;
192                 c.weightx = 1;
193                 c.anchor = GridBagConstraints.LINE_START;
194                 c.insets = ir;
195                 status_value = new JLabel("Waiting for MicroPeak data...");
196                 pane.add(status_value, c);
197                 y++;
198
199                 serial_log = new JTextArea(10, 20);
200                 serial_log.setEditable(false);
201
202                 JScrollPane serial_scroll = new JScrollPane(serial_log);
203                 serial_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
204
205                 c = new GridBagConstraints();
206                 c.gridx = 0; c.gridy = y;
207                 c.gridwidth = GridBagConstraints.REMAINDER;
208                 c.fill = GridBagConstraints.HORIZONTAL;
209                 c.weightx = 1;
210                 c.anchor = GridBagConstraints.LINE_START;
211                 c.insets = ir;
212
213                 pane.add(serial_scroll, c);
214                 y++;
215
216                 cancel = new JButton("Cancel");
217                 c = new GridBagConstraints();
218                 c.fill = GridBagConstraints.NONE;
219                 c.anchor = GridBagConstraints.EAST;
220                 c.gridx = 0; c.gridy = y;
221                 c.gridwidth = GridBagConstraints.REMAINDER;
222                 Insets ic = new Insets(4,4,4,4);
223                 c.insets = ic;
224                 pane.add(cancel, c);
225                 y++;
226
227                 cancel.addActionListener(this);
228
229                 pack();
230                 setLocationRelativeTo(owner);
231                 setVisible(true);
232                 start();
233         }
234 }