altosdroid: initial implementation of telemetry logging.
[fw/altos] / altosui / AltosFlashUI.java
1 /*
2  * Copyright © 2010 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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import java.io.*;
25 import java.util.concurrent.*;
26
27 public class AltosFlashUI
28         extends AltosDialog
29         implements ActionListener
30 {
31         Container       pane;
32         Box             box;
33         JLabel          serial_label;
34         JLabel          serial_value;
35         JLabel          file_label;
36         JLabel          file_value;
37         JProgressBar    pbar;
38         JButton         cancel;
39
40         JFrame          frame;
41
42         // Hex file with rom image
43         File            file;
44
45         // Debug connection
46         AltosDevice     debug_dongle;
47
48         // Desired Rom configuration
49         AltosRomconfig  rom_config;
50
51         // Flash controller
52         AltosFlash      flash;
53
54         public void actionPerformed(ActionEvent e) {
55                 if (e.getSource() == cancel) {
56                         if (flash != null)
57                                 flash.abort();
58                         setVisible(false);
59                         dispose();
60                 } else {
61                         String  cmd = e.getActionCommand();
62                         if (e.getID() == -1) {
63                                 JOptionPane.showMessageDialog(frame,
64                                                               e.getActionCommand(),
65                                                               file.toString(),
66                                                               JOptionPane.ERROR_MESSAGE);
67                                 setVisible(false);
68                                 dispose();
69                         } else if (cmd.equals("done")) {
70                                 setVisible(false);
71                                 dispose();
72                         } else if (cmd.equals("start")) {
73                                 setVisible(true);
74                         } else {
75                                 pbar.setValue(e.getID());
76                                 pbar.setString(cmd);
77                         }
78                 }
79         }
80
81         public void build_dialog() {
82                 GridBagConstraints c;
83                 Insets il = new Insets(4,4,4,4);
84                 Insets ir = new Insets(4,4,4,4);
85
86                 pane = getContentPane();
87                 pane.setLayout(new GridBagLayout());
88
89                 c = new GridBagConstraints();
90                 c.gridx = 0; c.gridy = 0;
91                 c.fill = GridBagConstraints.NONE;
92                 c.anchor = GridBagConstraints.LINE_START;
93                 c.insets = il;
94                 serial_label = new JLabel("Serial:");
95                 pane.add(serial_label, c);
96
97                 c = new GridBagConstraints();
98                 c.gridx = 1; c.gridy = 0;
99                 c.fill = GridBagConstraints.HORIZONTAL;
100                 c.weightx = 1;
101                 c.anchor = GridBagConstraints.LINE_START;
102                 c.insets = ir;
103                 serial_value = new JLabel("");
104                 pane.add(serial_value, c);
105
106                 c = new GridBagConstraints();
107                 c.fill = GridBagConstraints.NONE;
108                 c.gridx = 0; c.gridy = 1;
109                 c.anchor = GridBagConstraints.LINE_START;
110                 c.insets = il;
111                 file_label = new JLabel("File:");
112                 pane.add(file_label, c);
113
114                 c = new GridBagConstraints();
115                 c.fill = GridBagConstraints.HORIZONTAL;
116                 c.weightx = 1;
117                 c.gridx = 1; c.gridy = 1;
118                 c.anchor = GridBagConstraints.LINE_START;
119                 c.insets = ir;
120                 file_value = new JLabel(file.toString());
121                 pane.add(file_value, c);
122
123                 pbar = new JProgressBar();
124                 pbar.setMinimum(0);
125                 pbar.setMaximum(100);
126                 pbar.setValue(0);
127                 pbar.setString("");
128                 pbar.setStringPainted(true);
129                 pbar.setPreferredSize(new Dimension(600, 20));
130                 c = new GridBagConstraints();
131                 c.fill = GridBagConstraints.HORIZONTAL;
132                 c.anchor = GridBagConstraints.CENTER;
133                 c.gridx = 0; c.gridy = 2;
134                 c.gridwidth = GridBagConstraints.REMAINDER;
135                 Insets ib = new Insets(4,4,4,4);
136                 c.insets = ib;
137                 pane.add(pbar, c);
138
139                 cancel = new JButton("Cancel");
140                 c = new GridBagConstraints();
141                 c.fill = GridBagConstraints.NONE;
142                 c.anchor = GridBagConstraints.CENTER;
143                 c.gridx = 0; c.gridy = 3;
144                 c.gridwidth = GridBagConstraints.REMAINDER;
145                 Insets ic = new Insets(4,4,4,4);
146                 c.insets = ic;
147                 pane.add(cancel, c);
148                 cancel.addActionListener(this);
149                 pack();
150                 setLocationRelativeTo(frame);
151         }
152
153         void set_serial(int serial_number) {
154                 serial_value.setText(String.format("%d", serial_number));
155         }
156
157         boolean select_source_file() {
158                 JFileChooser    hexfile_chooser = new JFileChooser();
159
160                 File firmwaredir = AltosUIPreferences.firmwaredir();
161                 if (firmwaredir != null)
162                         hexfile_chooser.setCurrentDirectory(firmwaredir);
163
164                 hexfile_chooser.setDialogTitle("Select Flash Image");
165                 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
166                 int returnVal = hexfile_chooser.showOpenDialog(frame);
167
168                 if (returnVal != JFileChooser.APPROVE_OPTION)
169                         return false;
170                 file = hexfile_chooser.getSelectedFile();
171                 if (file == null)
172                         return false;
173                 AltosUIPreferences.set_firmwaredir(file.getParentFile());
174                 return true;
175         }
176
177         boolean select_debug_dongle() {
178                 debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any);
179
180                 if (debug_dongle == null)
181                         return false;
182                 return true;
183         }
184
185         boolean update_rom_config_info(AltosRomconfig existing_config) {
186                 AltosRomconfig  new_config;
187                 new_config = AltosRomconfigUI.show(frame, existing_config);
188                 if (new_config == null)
189                         return false;
190                 rom_config = new_config;
191                 set_serial(rom_config.serial_number);
192                 setVisible(true);
193                 return true;
194         }
195
196         void exception (Exception e) {
197                 if (e instanceof FileNotFoundException) {
198                         JOptionPane.showMessageDialog(frame,
199                                                       ((FileNotFoundException) e).getMessage(),
200                                                       "Cannot open file",
201                                                       JOptionPane.ERROR_MESSAGE);
202                 } else if (e instanceof AltosSerialInUseException) {
203                         JOptionPane.showMessageDialog(frame,
204                                                       String.format("Device \"%s\" already in use",
205                                                                     debug_dongle.toShortString()),
206                                                       "Device in use",
207                                                       JOptionPane.ERROR_MESSAGE);
208                 } else if (e instanceof IOException) {
209                         JOptionPane.showMessageDialog(frame,
210                                                       e.getMessage(),
211                                                       file.toString(),
212                                                       JOptionPane.ERROR_MESSAGE);
213                 }
214         }
215
216         class flash_task implements Runnable {
217                 AltosFlashUI    ui;
218                 Thread          t;
219                 AltosFlash      flash;
220
221                 public void run () {
222                         try {
223                                 flash = new AltosFlash(ui.file, ui.debug_dongle);
224                                 flash.addActionListener(ui);
225
226                                 final AltosRomconfig    current_config = flash.romconfig();
227
228                                 final Semaphore await_rom_config = new Semaphore(0);
229                                 SwingUtilities.invokeLater(new Runnable() {
230                                                 public void run() {
231                                                         ui.flash = flash;
232                                                         ui.update_rom_config_info(current_config);
233                                                         await_rom_config.release();
234                                                 }
235                                         });
236                                 await_rom_config.acquire();
237
238                                 if (ui.rom_config != null) {
239                                         flash.set_romconfig(ui.rom_config);
240                                         flash.flash();
241                                 }
242                         } catch (InterruptedException ee) {
243                                 final Exception e = ee;
244                                 SwingUtilities.invokeLater(new Runnable() {
245                                                 public void run() {
246                                                         ui.exception(e);
247                                                 }
248                                         });
249                         } catch (IOException ee) {
250                                 final Exception e = ee;
251                                 SwingUtilities.invokeLater(new Runnable() {
252                                                 public void run() {
253                                                         ui.exception(e);
254                                                 }
255                                         });
256                         } catch (AltosSerialInUseException ee) {
257                                 final Exception e = ee;
258                                 SwingUtilities.invokeLater(new Runnable() {
259                                                 public void run() {
260                                                         ui.exception(e);
261                                                 }
262                                         });
263                         } finally {
264                                 if (flash != null)
265                                         flash.close();
266                         }
267                 }
268
269                 public flash_task(AltosFlashUI in_ui) {
270                         ui = in_ui;
271                         t = new Thread(this);
272                         t.start();
273                 }
274         }
275
276         flash_task      flasher;
277
278         /*
279          * Execute the steps for flashing
280          * a device. Note that this returns immediately;
281          * this dialog is not modal
282          */
283         void showDialog() {
284                 if (!select_debug_dongle())
285                         return;
286                 if (!select_source_file())
287                         return;
288                 build_dialog();
289                 //flash_task    f = new flash_task(this);
290         }
291
292         static void show(JFrame frame) {
293                 AltosFlashUI    ui = new AltosFlashUI(frame);
294
295                 ui.showDialog();
296         }
297
298         public AltosFlashUI(JFrame in_frame) {
299                 super(in_frame, "Program Altusmetrum Device", false);
300
301                 frame = in_frame;
302         }
303 }