altoslib: Use existing unicode Charset in AltosLink
[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 {
30         MicroPeak       owner;
31         Container       pane;
32         AltosDevice     device;
33         JButton         cancel;
34         MicroData       data;
35         MicroSerial     serial;
36
37         private void done_internal() {
38                 setVisible(false);
39                 if (data != null) {
40                         if (data.crc_valid) {
41                                 owner = owner.SetData(data);
42                                 MicroSave save = new MicroSave(owner, data);
43                                 if (save.runDialog())
44                                         owner.SetName(data.name);
45                         } else {
46                                 JOptionPane.showMessageDialog(owner,
47                                                               "Flight data corrupted",
48                                                               "Download Failed",
49                                                               JOptionPane.ERROR_MESSAGE);
50                         }
51                 }
52                 dispose();
53         }
54
55         public void done() {
56                 Runnable r = new Runnable() {
57                                 public void run() {
58                                         try {
59                                                 done_internal();
60                                         } catch (Exception ex) {
61                                         }
62                                 }
63                         };
64                 SwingUtilities.invokeLater(r);
65         }
66
67         public void run() {
68                 try {
69                         data = new MicroData(serial, device.toShortString());
70                         serial.close();
71                 } catch (FileNotFoundException fe) {
72                 } catch (IOException ioe) {
73                 } catch (InterruptedException ie) {
74                 }
75                 done();
76         }
77
78         Thread  serial_thread;
79
80         public void start() {
81                 try {
82                         serial = new MicroSerial(device);
83                 } catch (FileNotFoundException fe) {
84                         return;
85                 }
86                 serial_thread = new Thread(this);
87                 serial_thread.start();
88         }
89
90         public void actionPerformed(ActionEvent ae) {
91                 if (serial_thread != null) {
92                         serial.close();
93                         serial_thread.interrupt();
94                 }
95                 setVisible(false);
96         }
97
98         public MicroDownload(MicroPeak owner, AltosDevice device) {
99                 super (owner, "Download MicroPeak Data", false);
100
101                 int y = 0;
102
103                 GridBagConstraints c;
104                 Insets il = new Insets(4,4,4,4);
105                 Insets ir = new Insets(4,4,4,4);
106
107                 this.owner = owner;
108                 this.device = device;
109
110                 pane = getContentPane();
111                 pane.setLayout(new GridBagLayout());
112
113                 c = new GridBagConstraints();
114                 c.gridx = 0; c.gridy = y;
115                 c.fill = GridBagConstraints.NONE;
116                 c.anchor = GridBagConstraints.LINE_START;
117                 c.insets = il;
118                 JLabel device_label = new JLabel("Device:");
119                 pane.add(device_label, c);
120
121                 c = new GridBagConstraints();
122                 c.gridx = 1; c.gridy = y;
123                 c.fill = GridBagConstraints.HORIZONTAL;
124                 c.weightx = 1;
125                 c.anchor = GridBagConstraints.LINE_START;
126                 c.insets = ir;
127                 JLabel device_value = new JLabel(device.toString());
128                 pane.add(device_value, c);
129                 y++;
130
131                 c = new GridBagConstraints();
132                 c.gridx = 0; c.gridy = y;
133                 c.gridwidth = GridBagConstraints.REMAINDER;
134                 c.fill = GridBagConstraints.HORIZONTAL;
135                 c.weightx = 1;
136                 c.anchor = GridBagConstraints.LINE_START;
137                 c.insets = ir;
138                 JTextArea help_text = new JTextArea(
139
140                         "Locate the photo transistor on the MicroPeak USB adapter\n" +
141                         "and place the LED on the MicroPeak directly in contact\n" +
142                         "with it.\n" +
143                         "\n" +
144                         "The MicroPeak LED and the MicroPeak USB adapter\n" +
145                         "photo need to be touching—even a millimeters of space\n" +
146                         "between them will reduce the light intensity from the LED\n" +
147                         "enough that the phototransistor will not sense it.\n" +
148                         "\n" +
149                         "Turn on the MicroPeak board and adjust the position until\n" +
150                         "the blue LED on the MicroPeak USB adapter blinks in time\n" +
151                         "with the orange LED on the MicroPeak board.");
152
153                 pane.add(help_text, c);
154                 y++;
155
156                 c = new GridBagConstraints();
157                 c.gridx = 0; c.gridy = y;
158                 c.gridwidth = 1;
159                 c.fill = GridBagConstraints.HORIZONTAL;
160                 c.weightx = 1;
161                 c.anchor = GridBagConstraints.LINE_START;
162                 c.insets = ir;
163                 JLabel waiting_value = new JLabel("Waiting for MicroPeak data...");
164                 pane.add(waiting_value, c);
165
166                 cancel = new JButton("Cancel");
167                 c = new GridBagConstraints();
168                 c.fill = GridBagConstraints.NONE;
169                 c.anchor = GridBagConstraints.CENTER;
170                 c.gridx = 1; c.gridy = y;
171                 c.gridwidth = GridBagConstraints.REMAINDER;
172                 Insets ic = new Insets(4,4,4,4);
173                 c.insets = ic;
174                 pane.add(cancel, c);
175                 y++;
176
177                 cancel.addActionListener(this);
178
179                 pack();
180                 setLocationRelativeTo(owner);
181                 setVisible(true);
182                 start();
183         }
184 }