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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
19 package org.altusmetrum.micropeak;
22 import java.awt.event.*;
25 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_14.*;
28 import org.altusmetrum.altosuilib_14.*;
30 public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog, WindowListener {
37 LinkedList<Integer> log_queue = new LinkedList<Integer>();
43 public void windowActivated(WindowEvent e) {
46 public void windowClosed(WindowEvent e) {
51 public void windowClosing(WindowEvent e) {
54 public void windowDeactivated(WindowEvent e) {
57 public void windowDeiconified(WindowEvent e) {
60 public void windowIconified(WindowEvent e) {
63 public void windowOpened(WindowEvent e) {
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",
72 JOptionPane.WARNING_MESSAGE);
74 status_value.setText("Received MicroPeak Data");
75 owner = owner.SetData(data);
76 MicroSave save = new MicroSave(owner, data);
78 owner.SetName(data.name);
81 JOptionPane.showMessageDialog(owner,
83 "Flight data corrupted",
84 JOptionPane.ERROR_MESSAGE);
90 public void drain_queue() {
94 if (log_queue.isEmpty()) {
98 c = log_queue.remove();
108 } else if (' ' <= c && c <= '~') {
109 byte[] bytes = new byte[1];
111 s = new String(bytes, AltosLib.unicode_set);
114 s = String.format("\\0x%02x", c & 0xff);
117 serial_log.append(s);
118 if (log_column > 40) {
119 serial_log.append("\n");
125 public void log_char(int c) {
128 if (log_run == null) {
129 log_run = new Runnable() {
134 SwingUtilities.invokeLater(log_run);
140 Runnable r = new Runnable() {
144 } catch (Exception ex) {
148 SwingUtilities.invokeLater(r);
155 data = new MicroData(serial, device.toShortString());
156 if (data != null && data.crc_valid)
158 } catch (MicroData.NonHexcharException nhe) {
161 } catch (FileNotFoundException fe) {
162 } catch (IOException ioe) {
163 } catch (InterruptedException ie) {
164 } catch (MicroData.FileEndedException fee) {
170 Thread serial_thread;
172 public void start() {
174 serial = new MicroSerial(device);
175 serial.set_log(this);
176 } catch (FileNotFoundException fe) {
179 serial_thread = new Thread(this);
180 serial_thread.start();
183 public void actionPerformed(ActionEvent ae) {
184 if (serial_thread != null) {
186 serial_thread.interrupt();
191 public MicroDownload(MicroPeak owner, AltosDevice device) {
192 super (owner, "Download MicroPeak Data", false);
196 GridBagConstraints c;
197 Insets il = new Insets(4,4,4,4);
198 Insets ir = new Insets(4,4,4,4);
201 this.device = device;
203 pane = getContentPane();
204 pane.setLayout(new GridBagLayout());
206 c = new GridBagConstraints();
207 c.gridx = 0; c.gridy = y;
208 c.fill = GridBagConstraints.NONE;
209 c.anchor = GridBagConstraints.LINE_START;
211 JLabel device_label = new JLabel("Device:");
212 pane.add(device_label, c);
214 c = new GridBagConstraints();
215 c.gridx = 1; c.gridy = y;
216 c.fill = GridBagConstraints.NONE;
218 c.anchor = GridBagConstraints.LINE_START;
220 JLabel device_value = new JLabel(device.toString());
221 pane.add(device_value, c);
224 c = new GridBagConstraints();
225 c.gridx = 0; c.gridy = y;
226 c.gridwidth = GridBagConstraints.REMAINDER;
227 c.fill = GridBagConstraints.HORIZONTAL;
229 c.anchor = GridBagConstraints.LINE_START;
231 JLabel help_text = new JLabel(
232 "<html><i>Turn on the MicroPeak and place the LED inside the<br>" +
233 "opening in the top of the MicroPeak USB adapter.<br> " +
234 "Verify that the blue LED in the side of the USB adapter<br>" +
235 "is blinking along with the orange LED on the MicroPeak.</i></html>");
236 // help_text.setEditable(false);
238 pane.add(help_text, c);
241 c = new GridBagConstraints();
242 c.gridx = 0; c.gridy = y;
244 c.fill = GridBagConstraints.HORIZONTAL;
246 c.anchor = GridBagConstraints.LINE_START;
248 status_value = new JLabel("Waiting for MicroPeak data...");
249 pane.add(status_value, c);
252 serial_log = new JTextArea(10, 20);
253 serial_log.setEditable(false);
255 JScrollPane serial_scroll = new JScrollPane(serial_log);
256 serial_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
258 c = new GridBagConstraints();
259 c.gridx = 0; c.gridy = y;
260 c.gridwidth = GridBagConstraints.REMAINDER;
261 c.fill = GridBagConstraints.BOTH;
264 c.anchor = GridBagConstraints.LINE_START;
267 pane.add(serial_scroll, c);
270 cancel = new JButton("Cancel");
271 c = new GridBagConstraints();
272 c.fill = GridBagConstraints.NONE;
273 c.anchor = GridBagConstraints.EAST;
274 c.gridx = 0; c.gridy = y;
275 c.gridwidth = GridBagConstraints.REMAINDER;
276 Insets ic = new Insets(4,4,4,4);
281 cancel.addActionListener(this);
284 setLocationRelativeTo(owner);