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
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 {
212 public AltosHexfileFilter(int product, String head, String description) {
213 this.product = product;
215 this.description = description;
218 public boolean accept(File file) {
219 return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
222 public String getDescription() {
227 static AltosHexfileFilter[] filters = {
228 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
229 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
230 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
231 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
232 new AltosHexfileFilter(AltosLib.product_easymega, "easymega", "EasyMega Image"),
235 boolean select_source_file() {
236 JFileChooser hexfile_chooser = new JFileChooser();
238 File firmwaredir = AltosUIPreferences.firmwaredir();
239 if (firmwaredir != null)
240 hexfile_chooser.setCurrentDirectory(firmwaredir);
242 hexfile_chooser.setDialogTitle("Select Flash Image");
244 for (int i = 0; i < filters.length; i++) {
245 hexfile_chooser.addChoosableFileFilter(filters[i]);
247 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
248 hexfile_chooser.addChoosableFileFilter(ihx_filter);
249 hexfile_chooser.setFileFilter(ihx_filter);
251 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
252 for (int i = 0; i < filters.length; i++) {
253 if (device != null && device.matchProduct(filters[i].product))
254 hexfile_chooser.setFileFilter(filters[i]);
258 int returnVal = hexfile_chooser.showOpenDialog(frame);
260 if (returnVal != JFileChooser.APPROVE_OPTION)
262 file = hexfile_chooser.getSelectedFile();
265 AltosUIPreferences.set_firmwaredir(file.getParentFile());
270 boolean select_device() {
271 int product = AltosLib.product_any;
273 device = AltosDeviceUIDialog.show(frame, AltosLib.product_any);
280 boolean rom_config_matches (AltosRomconfig a, AltosRomconfig b) {
281 if (a == null || b == null)
282 return (a == null && b == null);
284 if (!a.valid || !b.valid)
287 if (a.usb_id != null && b.usb_id != null &&
288 (a.usb_id.vid != b.usb_id.vid ||
289 a.usb_id.pid != b.usb_id.pid))
292 if (a.usb_product != null && b.usb_product != null &&
293 !a.usb_product.equals(b.usb_product))
299 boolean update_rom_config_info(AltosRomconfig existing_config, AltosRomconfig image_config) {
300 AltosRomconfig new_config;
302 if (!rom_config_matches(existing_config, image_config)) {
304 if (existing_config == null || !existing_config.valid) {
305 ret = JOptionPane.showConfirmDialog(this,
306 String.format("Cannot determine target device type\nImage is %04x:%04x %s\nFlash anyways?",
307 image_config.usb_id.vid,
308 image_config.usb_id.pid,
309 image_config.usb_product),
310 "Unknown Target Device",
311 JOptionPane.YES_NO_OPTION);
313 ret = JOptionPane.showConfirmDialog(this,
314 String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
315 existing_config.usb_id.vid,
316 existing_config.usb_id.pid,
317 existing_config.usb_product,
318 image_config.usb_id.vid,
319 image_config.usb_id.pid,
320 image_config.usb_product),
321 "Image doesn't match Device",
322 JOptionPane.YES_NO_OPTION);
324 if (ret != JOptionPane.YES_OPTION)
328 if (existing_config.radio_calibration_broken) {
329 int ret = JOptionPane.showConfirmDialog(this,
330 String.format("Radio calibration value %d may be incorrect\nFlash anyways?",
331 existing_config.radio_calibration),
332 "Radio Calibration Invalid",
333 JOptionPane.YES_NO_OPTION);
334 if (ret != JOptionPane.YES_OPTION)
339 new_config = AltosRomconfigUI.show(frame, existing_config);
340 if (new_config == null)
342 rom_config = new_config;
343 set_serial(rom_config.serial_number);
348 void exception (Exception e) {
349 if (e instanceof FileNotFoundException) {
350 JOptionPane.showMessageDialog(frame,
351 ((FileNotFoundException) e).getMessage(),
353 JOptionPane.ERROR_MESSAGE);
354 } else if (e instanceof AltosSerialInUseException) {
355 JOptionPane.showMessageDialog(frame,
356 String.format("Device \"%s\" already in use",
357 device.toShortString()),
359 JOptionPane.ERROR_MESSAGE);
361 JOptionPane.showMessageDialog(frame,
364 JOptionPane.ERROR_MESSAGE);
368 class flash_task implements Runnable, AltosFlashListener {
371 AltosProgrammer programmer;
373 public void position(String in_s, int in_percent) {
374 final String s = in_s;
375 final int percent = in_percent;
376 Runnable r = new Runnable() {
379 ui.actionPerformed(new ActionEvent(this,
382 } catch (Exception ex) {
386 SwingUtilities.invokeLater(r);
391 if (ui.is_pair_programmed())
392 programmer = new AltosFlash(ui.file, link, this);
394 programmer = new AltosSelfFlash(ui.file, link, this);
396 final AltosRomconfig current_config = programmer.target_romconfig();
398 final AltosRomconfig image_config = programmer.image_romconfig();
400 final Semaphore await_rom_config = new Semaphore(0);
401 SwingUtilities.invokeLater(new Runnable() {
403 ui.programmer = programmer;
404 ui.update_rom_config_info(current_config, image_config);
405 await_rom_config.release();
408 await_rom_config.acquire();
410 if (ui.rom_config != null) {
411 programmer.set_romconfig(ui.rom_config);
414 } catch (InterruptedException ee) {
415 final Exception e = ee;
416 SwingUtilities.invokeLater(new Runnable() {
421 } catch (IOException ee) {
422 final Exception e = ee;
423 SwingUtilities.invokeLater(new Runnable() {
429 if (programmer != null)
434 public flash_task(AltosFlashUI in_ui) {
436 t = new Thread(this);
444 class open_task implements Runnable {
449 public void do_exception(final Exception e) {
450 SwingUtilities.invokeLater(
453 try { dialog.open_exception(e); } catch (Exception ex) { }
458 public void do_success(final AltosLink link) {
459 SwingUtilities.invokeLater(
462 try { dialog.open_success(link); } catch (Exception ex) { }
467 public void do_failure() {
468 SwingUtilities.invokeLater(
471 try { dialog.open_failure(); } catch (Exception ex) { }
476 public void do_cancel() {
477 SwingUtilities.invokeLater(
480 try { dialog.open_cancel(); } catch (Exception ex) { }
487 AltosLink link = null;
490 System.out.printf("Attempting to open %s\n", device.toShortString());
492 link = new AltosSerial(device);
495 throw new IOException(String.format("%s: open failed",
496 device.toShortString()));
498 /* See if the link is usable already */
499 if (is_pair_programmed() || link.is_loader()) {
500 System.out.printf("Device ready for use\n");
505 java.util.List<AltosDevice> prev_devices =
506 AltosUSBDevice.list(AltosLib.product_altusmetrum);
508 /* Nope, switch to loader and
509 * wait for it to re-appear
512 System.out.printf("Switch to loader\n");
516 /* This is a bit fragile, but
517 * I'm not sure what else to
518 * do other than ask the user.
520 * Search for a device which
521 * wasn't there before we
522 * asked the target to switch
529 java.util.List<AltosDevice> devices =
530 AltosUSBDevice.list(AltosLib.product_altusmetrum);
532 for (AltosDevice d : devices) {
533 boolean matched = false;
534 System.out.printf("\tfound device %s\n", d.toShortString());
535 for (AltosDevice p : prev_devices)
541 System.out.printf("Identified new device %s\n", d.toShortString());
550 } catch (AltosSerialInUseException ee) {
552 } catch (FileNotFoundException fe) {
554 } catch (IOException ie) {
556 } catch (InterruptedException ie) {
560 public void cancel() {
564 public open_task(AltosDevice device, open_dialog dialog) {
565 this.device = device;
566 this.dialog = dialog;
567 t = new Thread(this);
573 extends AltosUIDialog
574 implements ActionListener
578 private JLabel opening_label;
579 private JButton cancel_button;
581 boolean done = false;
583 AltosLink link = null;
585 open_task open = null;
587 public void open_exception(Exception e) {
588 System.out.printf("open_exception\n");
594 public void open_success(AltosLink link) {
595 System.out.printf("open_success\n");
601 public void open_failure() {
602 System.out.printf("open_failure\n");
607 public void open_cancel() {
608 System.out.printf("open_cancel\n");
613 public AltosLink do_open(open_task open) throws InterruptedException {
619 public void actionPerformed(ActionEvent e) {
620 String cmd = e.getActionCommand();
622 if (cmd.equals("cancel"))
629 public open_dialog(AltosUIFrame in_owner) {
630 super(in_owner, "Open Flash Target Device", true);
633 Container pane = getContentPane();
634 GridBagConstraints c = new GridBagConstraints();
635 Insets i = new Insets(4,4,4,4);
638 pane.setLayout(new GridBagLayout());
640 opening_label = new JLabel("Opening Device");
641 c.fill = GridBagConstraints.HORIZONTAL;
642 c.anchor = GridBagConstraints.LINE_START;
650 pane.add(opening_label, c);
652 cancel_button = new JButton("Cancel");
653 cancel_button.addActionListener(this);
654 cancel_button.setActionCommand("cancel");
657 pane.add(cancel_button, c);
659 setLocationRelativeTo(owner);
663 private boolean open_device() throws InterruptedException {
665 open_dialog dialog = new open_dialog(frame);
667 open_task open = new open_task(device, dialog);
669 link = dialog.do_open(open);
675 * Execute the steps for flashing
676 * a device. Note that this returns immediately;
677 * this dialog is not modal
680 if (!select_device())
682 if (!select_source_file())
687 } catch (InterruptedException ie) {
691 flash_task f = new flash_task(this);
694 public static void show(AltosUIFrame frame) {
695 AltosFlashUI ui = new AltosFlashUI(frame);
699 public AltosFlashUI(AltosUIFrame in_frame) {
700 super(in_frame, "Program Altusmetrum Device", false);