2 * Copyright © 2010 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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_13;
22 import java.awt.event.*;
24 import javax.swing.filechooser.FileNameExtensionFilter;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_13.*;
29 public class AltosFlashUI
31 implements ActionListener
44 // Hex file with rom image
48 AltosUSBDevice device;
52 // Desired Rom configuration
53 AltosRomconfig rom_config;
56 AltosProgrammer programmer;
58 private static final String[] pair_programmed_files = {
71 private static final String[] pair_programmed_devices = {
85 private boolean is_pair_programmed() {
88 String name = file.getName();
89 for (int i = 0; i < pair_programmed_files.length; i++) {
90 if (name.startsWith(pair_programmed_files[i]))
95 String name = device.toString();
96 for (int i = 0; i < pair_programmed_devices.length; i++) {
97 if (name.startsWith(pair_programmed_devices[i]))
104 public void actionPerformed(ActionEvent e) {
105 if (e.getSource() == cancel) {
106 if (programmer != null)
111 String cmd = e.getActionCommand();
112 if (e.getID() == -1) {
113 JOptionPane.showMessageDialog(frame,
114 e.getActionCommand(),
116 JOptionPane.ERROR_MESSAGE);
119 } else if (cmd.equals(AltosFlashListener.flash_done)) {
122 } else if (cmd.equals(AltosFlashListener.flash_start)) {
125 pbar.setValue(e.getID());
131 public void build_dialog() {
132 GridBagConstraints c;
133 Insets il = new Insets(4,4,4,4);
134 Insets ir = new Insets(4,4,4,4);
136 pane = getContentPane();
137 pane.setLayout(new GridBagLayout());
139 c = new GridBagConstraints();
140 c.gridx = 0; c.gridy = 0;
141 c.fill = GridBagConstraints.NONE;
142 c.anchor = GridBagConstraints.LINE_START;
144 serial_label = new JLabel("Serial:");
145 pane.add(serial_label, c);
147 c = new GridBagConstraints();
148 c.gridx = 1; c.gridy = 0;
149 c.fill = GridBagConstraints.HORIZONTAL;
151 c.anchor = GridBagConstraints.LINE_START;
153 serial_value = new JLabel("");
154 pane.add(serial_value, c);
156 c = new GridBagConstraints();
157 c.fill = GridBagConstraints.NONE;
158 c.gridx = 0; c.gridy = 1;
159 c.anchor = GridBagConstraints.LINE_START;
161 file_label = new JLabel("File:");
162 pane.add(file_label, c);
164 c = new GridBagConstraints();
165 c.fill = GridBagConstraints.HORIZONTAL;
167 c.gridx = 1; c.gridy = 1;
168 c.anchor = GridBagConstraints.LINE_START;
170 file_value = new JLabel(file.toString());
171 pane.add(file_value, c);
173 pbar = new JProgressBar();
175 pbar.setMaximum(100);
178 pbar.setStringPainted(true);
179 pbar.setPreferredSize(new Dimension(600, 20));
180 c = new GridBagConstraints();
181 c.fill = GridBagConstraints.HORIZONTAL;
182 c.anchor = GridBagConstraints.CENTER;
183 c.gridx = 0; c.gridy = 2;
184 c.gridwidth = GridBagConstraints.REMAINDER;
185 Insets ib = new Insets(4,4,4,4);
189 cancel = new JButton("Cancel");
190 c = new GridBagConstraints();
191 c.fill = GridBagConstraints.NONE;
192 c.anchor = GridBagConstraints.CENTER;
193 c.gridx = 0; c.gridy = 3;
194 c.gridwidth = GridBagConstraints.REMAINDER;
195 Insets ic = new Insets(4,4,4,4);
198 cancel.addActionListener(this);
200 setLocationRelativeTo(frame);
203 void set_serial(int serial_number) {
204 serial_value.setText(String.format("%d", serial_number));
207 static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
211 public AltosHexfileFilter(String usb_product) {
214 /* Trim off any trailing variants (1.0a vs 1.0) */
215 for (l = usb_product.length(); l > 0; l--) {
216 char c = usb_product.charAt(l-1);
217 if (c < 'a' || 'z' < c)
220 head = usb_product.substring(0, l).toLowerCase();
221 description = String.format("%s Image File", usb_product);
224 public boolean accept(File file) {
225 return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
228 public String getDescription() {
233 boolean select_source_file() {
234 JFileChooser hexfile_chooser = new JFileChooser();
236 File firmwaredir = AltosUIPreferences.firmwaredir();
237 if (firmwaredir != null)
238 hexfile_chooser.setCurrentDirectory(firmwaredir);
240 hexfile_chooser.setDialogTitle("Select Flash Image");
242 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
243 hexfile_chooser.addChoosableFileFilter(ihx_filter);
244 hexfile_chooser.setFileFilter(ihx_filter);
246 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
247 AltosHexfileFilter filter = new AltosHexfileFilter(device.usb_product());
248 hexfile_chooser.addChoosableFileFilter(filter);
249 hexfile_chooser.setFileFilter(filter);
252 int returnVal = hexfile_chooser.showOpenDialog(frame);
254 if (returnVal != JFileChooser.APPROVE_OPTION)
256 file = hexfile_chooser.getSelectedFile();
259 AltosUIPreferences.set_firmwaredir(file.getParentFile());
264 boolean select_device() {
265 int product = AltosLib.product_any;
267 device = AltosDeviceUIDialog.show_usb(frame, AltosLib.product_any);
274 boolean rom_config_matches (AltosRomconfig a, AltosRomconfig b) {
275 if (a == null || b == null)
276 return (a == null && b == null);
278 if (!a.valid || !b.valid)
281 if (a.usb_id != null && b.usb_id != null &&
282 (a.usb_id.vid != b.usb_id.vid ||
283 a.usb_id.pid != b.usb_id.pid))
286 if (a.usb_product != null && b.usb_product != null &&
287 !a.usb_product.equals(b.usb_product))
293 boolean update_rom_config_info(AltosRomconfig existing_config, AltosRomconfig image_config) {
294 AltosRomconfig new_config;
296 if (!rom_config_matches(existing_config, image_config)) {
298 if (existing_config == null || !existing_config.valid) {
299 ret = JOptionPane.showConfirmDialog(this,
300 String.format("Cannot determine target device type\nImage is %04x:%04x %s\nFlash anyways?",
301 image_config.usb_id.vid,
302 image_config.usb_id.pid,
303 image_config.usb_product),
304 "Unknown Target Device",
305 JOptionPane.YES_NO_OPTION);
307 ret = JOptionPane.showConfirmDialog(this,
308 String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
309 existing_config.usb_id.vid,
310 existing_config.usb_id.pid,
311 existing_config.usb_product,
312 image_config.usb_id.vid,
313 image_config.usb_id.pid,
314 image_config.usb_product),
315 "Image doesn't match Device",
316 JOptionPane.YES_NO_OPTION);
318 if (ret != JOptionPane.YES_OPTION)
322 if (existing_config != null && existing_config.radio_calibration_broken) {
323 int ret = JOptionPane.showConfirmDialog(this,
324 String.format("Radio calibration value %d may be incorrect\nFlash anyways?",
325 existing_config.radio_calibration),
326 "Radio Calibration Invalid",
327 JOptionPane.YES_NO_OPTION);
328 if (ret != JOptionPane.YES_OPTION)
333 new_config = AltosRomconfigUI.show(frame, existing_config);
334 if (new_config == null)
336 rom_config = new_config;
337 set_serial(rom_config.serial_number);
342 void exception (Exception e) {
343 if (e instanceof FileNotFoundException) {
344 JOptionPane.showMessageDialog(frame,
345 ((FileNotFoundException) e).getMessage(),
347 JOptionPane.ERROR_MESSAGE);
348 } else if (e instanceof AltosSerialInUseException) {
349 JOptionPane.showMessageDialog(frame,
350 String.format("Device \"%s\" already in use",
351 device.toShortString()),
353 JOptionPane.ERROR_MESSAGE);
355 JOptionPane.showMessageDialog(frame,
358 JOptionPane.ERROR_MESSAGE);
362 class flash_task implements Runnable, AltosFlashListener {
365 AltosProgrammer programmer;
367 public void position(String in_s, int in_percent) {
368 final String s = in_s;
369 final int percent = in_percent;
370 Runnable r = new Runnable() {
373 ui.actionPerformed(new ActionEvent(this,
376 } catch (Exception ex) {
380 SwingUtilities.invokeLater(r);
385 if (ui.is_pair_programmed())
386 programmer = new AltosFlash(ui.file, link, this);
388 programmer = new AltosSelfFlash(ui.file, link, this);
390 final AltosRomconfig current_config = programmer.target_romconfig(device.usb_id(), device.usb_product());
392 final AltosRomconfig image_config = programmer.image_romconfig();
394 System.out.printf("product %s current %s image %s\n", device.usb_product(), current_config, image_config);
396 final Semaphore await_rom_config = new Semaphore(0);
397 SwingUtilities.invokeLater(new Runnable() {
399 ui.programmer = programmer;
400 ui.update_rom_config_info(current_config, image_config);
401 await_rom_config.release();
404 await_rom_config.acquire();
406 if (ui.rom_config != null) {
407 programmer.set_romconfig(ui.rom_config);
410 } catch (InterruptedException ee) {
411 final Exception e = ee;
412 SwingUtilities.invokeLater(new Runnable() {
417 } catch (IOException ee) {
418 final Exception e = ee;
419 SwingUtilities.invokeLater(new Runnable() {
425 if (programmer != null)
430 public flash_task(AltosFlashUI in_ui) {
432 t = new Thread(this);
440 class open_task implements Runnable {
445 public void do_exception(final Exception e) {
446 SwingUtilities.invokeLater(
449 try { dialog.open_exception(e); } catch (Exception ex) { }
454 public void do_success(final AltosLink link) {
455 SwingUtilities.invokeLater(
458 try { dialog.open_success(link); } catch (Exception ex) { }
463 public void do_failure() {
464 SwingUtilities.invokeLater(
467 try { dialog.open_failure(); } catch (Exception ex) { }
472 public void do_cancel() {
473 SwingUtilities.invokeLater(
476 try { dialog.open_cancel(); } catch (Exception ex) { }
483 AltosLink link = null;
484 boolean new_device = false;
487 System.out.printf("Attempting to open %s\n", device.toShortString());
489 for (int i = 0; i < 20; i++) {
490 link = new AltosSerial(device);
498 System.out.printf("Waiting for device to become ready\n");
503 throw new IOException(String.format("%s: open failed",
504 device.toShortString()));
506 /* See if the link is usable already */
507 if (is_pair_programmed() || link.is_loader()) {
508 System.out.printf("Device ready for use\n");
513 java.util.List<AltosDevice> prev_devices =
514 AltosUSBDevice.list(AltosLib.product_altusmetrum);
516 /* Nope, switch to loader and
517 * wait for it to re-appear
520 System.out.printf("Switch to loader\n");
524 /* This is a bit fragile, but
525 * I'm not sure what else to
526 * do other than ask the user.
528 * Search for a device which
529 * wasn't there before we
530 * asked the target to switch
537 java.util.List<AltosDevice> devices =
538 AltosUSBDevice.list(AltosLib.product_altusmetrum);
540 for (AltosDevice d : devices) {
541 boolean matched = false;
542 System.out.printf("\tfound device %s\n", d.toShortString());
543 for (AltosDevice p : prev_devices)
549 System.out.printf("Identified new device %s\n", d.toShortString());
550 device = (AltosUSBDevice) d;
559 } catch (AltosSerialInUseException ee) {
561 } catch (FileNotFoundException fe) {
563 } catch (IOException ie) {
565 } catch (InterruptedException ie) {
569 public void cancel() {
573 public open_task(AltosDevice device, open_dialog dialog) {
574 this.device = device;
575 this.dialog = dialog;
576 t = new Thread(this);
582 extends AltosUIDialog
583 implements ActionListener
587 private JLabel opening_label;
588 private JButton cancel_button;
590 boolean done = false;
592 AltosLink link = null;
594 open_task open = null;
596 public void open_exception(Exception e) {
597 System.out.printf("open_exception\n");
603 public void open_success(AltosLink link) {
604 System.out.printf("open_success\n");
610 public void open_failure() {
611 System.out.printf("open_failure\n");
616 public void open_cancel() {
617 System.out.printf("open_cancel\n");
622 public AltosLink do_open(open_task open) throws InterruptedException {
628 public void actionPerformed(ActionEvent e) {
629 String cmd = e.getActionCommand();
631 if (cmd.equals("cancel"))
638 public open_dialog(AltosUIFrame in_owner) {
639 super(in_owner, "Open Flash Target Device", true);
642 Container pane = getContentPane();
643 GridBagConstraints c = new GridBagConstraints();
644 Insets i = new Insets(4,4,4,4);
647 pane.setLayout(new GridBagLayout());
649 opening_label = new JLabel("Opening Device");
650 c.fill = GridBagConstraints.HORIZONTAL;
651 c.anchor = GridBagConstraints.LINE_START;
659 pane.add(opening_label, c);
661 cancel_button = new JButton("Cancel");
662 cancel_button.addActionListener(this);
663 cancel_button.setActionCommand("cancel");
666 pane.add(cancel_button, c);
668 setLocationRelativeTo(owner);
672 private boolean open_device() throws InterruptedException {
674 open_dialog dialog = new open_dialog(frame);
676 open_task open = new open_task(device, dialog);
678 link = dialog.do_open(open);
684 * Execute the steps for flashing
685 * a device. Note that this returns immediately;
686 * this dialog is not modal
689 if (!select_device())
691 if (!select_source_file())
696 } catch (InterruptedException ie) {
700 flash_task f = new flash_task(this);
703 public static void show(AltosUIFrame frame) {
704 AltosFlashUI ui = new AltosFlashUI(frame);
708 public AltosFlashUI(AltosUIFrame in_frame) {
709 super(in_frame, "Program Altusmetrum Device", false);