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