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