2 * Copyright © 2012 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.
18 package org.altusmetrum.micropeak;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_10.*;
27 import org.altusmetrum.altosuilib_10.*;
29 public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog, WindowListener {
36 LinkedList<Integer> log_queue = new LinkedList<Integer>();
42 public void windowActivated(WindowEvent e) {
45 public void windowClosed(WindowEvent e) {
50 public void windowClosing(WindowEvent e) {
53 public void windowDeactivated(WindowEvent e) {
56 public void windowDeiconified(WindowEvent e) {
59 public void windowIconified(WindowEvent e) {
62 public void windowOpened(WindowEvent e) {
65 private void done_internal() {
69 if (data != null && data.crc_valid) {
70 status_value.setText("Received MicroPeak Data");
71 owner = owner.SetData(data);
72 MicroSave save = new MicroSave(owner, data);
74 owner.SetName(data.name);
76 JOptionPane.showMessageDialog(owner,
78 "Flight data corrupted",
79 JOptionPane.ERROR_MESSAGE);
83 public void drain_queue() {
87 if (log_queue.isEmpty()) {
91 c = log_queue.remove();
101 } else if (' ' <= c && c <= '~') {
102 byte[] bytes = new byte[1];
104 s = new String(bytes, AltosLib.unicode_set);
107 s = String.format("\\0x%02x", c & 0xff);
110 serial_log.append(s);
111 if (log_column > 40) {
112 serial_log.append("\n");
118 public void log_char(int c) {
121 if (log_run == null) {
122 log_run = new Runnable() {
127 SwingUtilities.invokeLater(log_run);
133 Runnable r = new Runnable() {
137 } catch (Exception ex) {
141 SwingUtilities.invokeLater(r);
148 data = new MicroData(serial, device.toShortString());
149 if (data != null && data.crc_valid)
151 } catch (MicroData.NonHexcharException nhe) {
154 } catch (FileNotFoundException fe) {
155 } catch (IOException ioe) {
156 } catch (InterruptedException ie) {
157 } catch (MicroData.FileEndedException fee) {
163 Thread serial_thread;
165 public void start() {
167 serial = new MicroSerial(device);
168 serial.set_log(this);
169 } catch (FileNotFoundException fe) {
172 serial_thread = new Thread(this);
173 serial_thread.start();
176 public void actionPerformed(ActionEvent ae) {
177 if (serial_thread != null) {
179 serial_thread.interrupt();
184 public MicroDownload(MicroPeak owner, AltosDevice device) {
185 super (owner, "Download MicroPeak Data", false);
189 GridBagConstraints c;
190 Insets il = new Insets(4,4,4,4);
191 Insets ir = new Insets(4,4,4,4);
194 this.device = device;
196 pane = getContentPane();
197 pane.setLayout(new GridBagLayout());
199 c = new GridBagConstraints();
200 c.gridx = 0; c.gridy = y;
201 c.fill = GridBagConstraints.NONE;
202 c.anchor = GridBagConstraints.LINE_START;
204 JLabel device_label = new JLabel("Device:");
205 pane.add(device_label, c);
207 c = new GridBagConstraints();
208 c.gridx = 1; c.gridy = y;
209 c.fill = GridBagConstraints.NONE;
211 c.anchor = GridBagConstraints.LINE_START;
213 JLabel device_value = new JLabel(device.toString());
214 pane.add(device_value, c);
217 c = new GridBagConstraints();
218 c.gridx = 0; c.gridy = y;
219 c.gridwidth = GridBagConstraints.REMAINDER;
220 c.fill = GridBagConstraints.HORIZONTAL;
222 c.anchor = GridBagConstraints.LINE_START;
224 JLabel help_text = new JLabel(
225 "<html><i>Turn on the MicroPeak and place the LED inside the<br>" +
226 "opening in the top of the MicroPeak USB adapter.<br> " +
227 "Verify that the blue LED in the side of the USB adapter<br>" +
228 "is blinking along with the orange LED on the MicroPeak.</i></html>");
229 // help_text.setEditable(false);
231 pane.add(help_text, c);
234 c = new GridBagConstraints();
235 c.gridx = 0; c.gridy = y;
237 c.fill = GridBagConstraints.HORIZONTAL;
239 c.anchor = GridBagConstraints.LINE_START;
241 status_value = new JLabel("Waiting for MicroPeak data...");
242 pane.add(status_value, c);
245 serial_log = new JTextArea(10, 20);
246 serial_log.setEditable(false);
248 JScrollPane serial_scroll = new JScrollPane(serial_log);
249 serial_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
251 c = new GridBagConstraints();
252 c.gridx = 0; c.gridy = y;
253 c.gridwidth = GridBagConstraints.REMAINDER;
254 c.fill = GridBagConstraints.BOTH;
257 c.anchor = GridBagConstraints.LINE_START;
260 pane.add(serial_scroll, c);
263 cancel = new JButton("Cancel");
264 c = new GridBagConstraints();
265 c.fill = GridBagConstraints.NONE;
266 c.anchor = GridBagConstraints.EAST;
267 c.gridx = 0; c.gridy = y;
268 c.gridwidth = GridBagConstraints.REMAINDER;
269 Insets ic = new Insets(4,4,4,4);
274 cancel.addActionListener(this);
277 setLocationRelativeTo(owner);