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_3.*;
27 import org.altusmetrum.altosuilib_1.*;
29 public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog {
36 LinkedList<Integer> log_queue = new LinkedList<Integer>();
41 private void done_internal() {
43 status_value.setText("Received MicroPeak Data");
45 owner = owner.SetData(data);
46 MicroSave save = new MicroSave(owner, data);
48 owner.SetName(data.name);
50 JOptionPane.showMessageDialog(owner,
51 "Flight data corrupted",
53 JOptionPane.ERROR_MESSAGE);
58 status_value.setText("Download Failed");
62 public void drain_queue() {
66 if (log_queue.isEmpty()) {
70 c = log_queue.remove();
74 byte[] bytes = new byte[1];
76 serial_log.append(new String(bytes, AltosLib.unicode_set));
80 public void log_char(int c) {
83 if (log_run == null) {
84 log_run = new Runnable() {
89 SwingUtilities.invokeLater(log_run);
95 Runnable r = new Runnable() {
99 } catch (Exception ex) {
103 SwingUtilities.invokeLater(r);
108 data = new MicroData(serial, device.toShortString());
110 } catch (FileNotFoundException fe) {
111 } catch (IOException ioe) {
112 } catch (InterruptedException ie) {
117 Thread serial_thread;
119 public void start() {
121 serial = new MicroSerial(device);
122 serial.set_log(this);
123 } catch (FileNotFoundException fe) {
126 serial_thread = new Thread(this);
127 serial_thread.start();
130 public void actionPerformed(ActionEvent ae) {
131 if (serial_thread != null) {
133 serial_thread.interrupt();
138 public MicroDownload(MicroPeak owner, AltosDevice device) {
139 super (owner, "Download MicroPeak Data", false);
143 GridBagConstraints c;
144 Insets il = new Insets(4,4,4,4);
145 Insets ir = new Insets(4,4,4,4);
148 this.device = device;
150 pane = getContentPane();
151 pane.setLayout(new GridBagLayout());
153 c = new GridBagConstraints();
154 c.gridx = 0; c.gridy = y;
155 c.fill = GridBagConstraints.NONE;
156 c.anchor = GridBagConstraints.LINE_START;
158 JLabel device_label = new JLabel("Device:");
159 pane.add(device_label, c);
161 c = new GridBagConstraints();
162 c.gridx = 1; c.gridy = y;
163 c.fill = GridBagConstraints.NONE;
165 c.anchor = GridBagConstraints.LINE_START;
167 JLabel device_value = new JLabel(device.toString());
168 pane.add(device_value, c);
171 c = new GridBagConstraints();
172 c.gridx = 0; c.gridy = y;
173 c.gridwidth = GridBagConstraints.REMAINDER;
174 c.fill = GridBagConstraints.HORIZONTAL;
176 c.anchor = GridBagConstraints.LINE_START;
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);
185 pane.add(help_text, c);
188 c = new GridBagConstraints();
189 c.gridx = 0; c.gridy = y;
191 c.fill = GridBagConstraints.HORIZONTAL;
193 c.anchor = GridBagConstraints.LINE_START;
195 status_value = new JLabel("Waiting for MicroPeak data...");
196 pane.add(status_value, c);
199 serial_log = new JTextArea(10, 20);
200 serial_log.setEditable(false);
202 JScrollPane serial_scroll = new JScrollPane(serial_log);
203 serial_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
205 c = new GridBagConstraints();
206 c.gridx = 0; c.gridy = y;
207 c.gridwidth = GridBagConstraints.REMAINDER;
208 c.fill = GridBagConstraints.HORIZONTAL;
210 c.anchor = GridBagConstraints.LINE_START;
213 pane.add(serial_scroll, c);
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);
227 cancel.addActionListener(this);
230 setLocationRelativeTo(owner);