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.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;
486 System.out.printf("Attempting to open %s\n", device.toShortString());
488 link = new AltosSerial(device);
491 throw new IOException(String.format("%s: open failed",
492 device.toShortString()));
494 /* See if the link is usable already */
495 if (is_pair_programmed() || link.is_loader()) {
496 System.out.printf("Device ready for use\n");
501 java.util.List<AltosDevice> prev_devices =
502 AltosUSBDevice.list(AltosLib.product_altusmetrum);
504 /* Nope, switch to loader and
505 * wait for it to re-appear
508 System.out.printf("Switch to loader\n");
512 /* This is a bit fragile, but
513 * I'm not sure what else to
514 * do other than ask the user.
516 * Search for a device which
517 * wasn't there before we
518 * asked the target to switch
525 java.util.List<AltosDevice> devices =
526 AltosUSBDevice.list(AltosLib.product_altusmetrum);
528 for (AltosDevice d : devices) {
529 boolean matched = false;
530 System.out.printf("\tfound device %s\n", d.toShortString());
531 for (AltosDevice p : prev_devices)
537 System.out.printf("Identified new device %s\n", d.toShortString());
538 device = (AltosUSBDevice) d;
546 } catch (AltosSerialInUseException ee) {
548 } catch (FileNotFoundException fe) {
550 } catch (IOException ie) {
552 } catch (InterruptedException ie) {
556 public void cancel() {
560 public open_task(AltosDevice device, open_dialog dialog) {
561 this.device = device;
562 this.dialog = dialog;
563 t = new Thread(this);
569 extends AltosUIDialog
570 implements ActionListener
574 private JLabel opening_label;
575 private JButton cancel_button;
577 boolean done = false;
579 AltosLink link = null;
581 open_task open = null;
583 public void open_exception(Exception e) {
584 System.out.printf("open_exception\n");
590 public void open_success(AltosLink link) {
591 System.out.printf("open_success\n");
597 public void open_failure() {
598 System.out.printf("open_failure\n");
603 public void open_cancel() {
604 System.out.printf("open_cancel\n");
609 public AltosLink do_open(open_task open) throws InterruptedException {
615 public void actionPerformed(ActionEvent e) {
616 String cmd = e.getActionCommand();
618 if (cmd.equals("cancel"))
625 public open_dialog(AltosUIFrame in_owner) {
626 super(in_owner, "Open Flash Target Device", true);
629 Container pane = getContentPane();
630 GridBagConstraints c = new GridBagConstraints();
631 Insets i = new Insets(4,4,4,4);
634 pane.setLayout(new GridBagLayout());
636 opening_label = new JLabel("Opening Device");
637 c.fill = GridBagConstraints.HORIZONTAL;
638 c.anchor = GridBagConstraints.LINE_START;
646 pane.add(opening_label, c);
648 cancel_button = new JButton("Cancel");
649 cancel_button.addActionListener(this);
650 cancel_button.setActionCommand("cancel");
653 pane.add(cancel_button, c);
655 setLocationRelativeTo(owner);
659 private boolean open_device() throws InterruptedException {
661 open_dialog dialog = new open_dialog(frame);
663 open_task open = new open_task(device, dialog);
665 link = dialog.do_open(open);
671 * Execute the steps for flashing
672 * a device. Note that this returns immediately;
673 * this dialog is not modal
676 if (!select_device())
678 if (!select_source_file())
683 } catch (InterruptedException ie) {
687 flash_task f = new flash_task(this);
690 public static void show(AltosUIFrame frame) {
691 AltosFlashUI ui = new AltosFlashUI(frame);
695 public AltosFlashUI(AltosUIFrame in_frame) {
696 super(in_frame, "Program Altusmetrum Device", false);