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_12;
22 import java.awt.event.*;
24 import javax.swing.filechooser.FileNameExtensionFilter;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_12.*;
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("done")) {
122 } else if (cmd.equals("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.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)) {
297 int ret = JOptionPane.showConfirmDialog(this,
298 String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
299 existing_config.usb_id.vid,
300 existing_config.usb_id.pid,
301 existing_config.usb_product,
302 image_config.usb_id.vid,
303 image_config.usb_id.pid,
304 image_config.usb_product),
305 "Image doesn't match Device",
306 JOptionPane.YES_NO_OPTION);
307 if (ret != JOptionPane.YES_OPTION)
311 new_config = AltosRomconfigUI.show(frame, existing_config);
312 if (new_config == null)
314 rom_config = new_config;
315 set_serial(rom_config.serial_number);
320 void exception (Exception e) {
321 if (e instanceof FileNotFoundException) {
322 JOptionPane.showMessageDialog(frame,
323 ((FileNotFoundException) e).getMessage(),
325 JOptionPane.ERROR_MESSAGE);
326 } else if (e instanceof AltosSerialInUseException) {
327 JOptionPane.showMessageDialog(frame,
328 String.format("Device \"%s\" already in use",
329 device.toShortString()),
331 JOptionPane.ERROR_MESSAGE);
332 } else if (e instanceof IOException) {
333 JOptionPane.showMessageDialog(frame,
336 JOptionPane.ERROR_MESSAGE);
340 class flash_task implements Runnable, AltosFlashListener {
343 AltosProgrammer programmer;
345 public void position(String in_s, int in_percent) {
346 final String s = in_s;
347 final int percent = in_percent;
348 Runnable r = new Runnable() {
351 ui.actionPerformed(new ActionEvent(this,
354 } catch (Exception ex) {
358 SwingUtilities.invokeLater(r);
363 if (ui.is_pair_programmed())
364 programmer = new AltosFlash(ui.file, link, this);
366 programmer = new AltosSelfFlash(ui.file, link, this);
368 final AltosRomconfig current_config = programmer.target_romconfig();
370 final AltosRomconfig image_config = programmer.image_romconfig();
372 final Semaphore await_rom_config = new Semaphore(0);
373 SwingUtilities.invokeLater(new Runnable() {
375 ui.programmer = programmer;
376 ui.update_rom_config_info(current_config, image_config);
377 await_rom_config.release();
380 await_rom_config.acquire();
382 if (ui.rom_config != null) {
383 programmer.set_romconfig(ui.rom_config);
386 } catch (InterruptedException ee) {
387 final Exception e = ee;
388 SwingUtilities.invokeLater(new Runnable() {
393 } catch (IOException ee) {
394 final Exception e = ee;
395 SwingUtilities.invokeLater(new Runnable() {
401 if (programmer != null)
406 public flash_task(AltosFlashUI in_ui) {
408 t = new Thread(this);
415 private boolean open_device() throws InterruptedException {
417 link = new AltosSerial(device);
418 if (is_pair_programmed())
422 throw new IOException(String.format("%s: open failed", device.toShortString()));
424 while (!link.is_loader()) {
427 java.util.List<AltosDevice> devices = null;
429 for (int tries = 0; tries < 10; tries++) {
431 devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
432 if (devices.size() != 0)
436 if (devices.size() == 1)
437 device = devices.get(0);
439 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
443 link = new AltosSerial(device);
446 } catch (AltosSerialInUseException ee) {
448 } catch (FileNotFoundException fe) {
450 } catch (IOException ie) {
457 * Execute the steps for flashing
458 * a device. Note that this returns immediately;
459 * this dialog is not modal
462 if (!select_device())
464 if (!select_source_file())
469 } catch (InterruptedException ie) {
473 flash_task f = new flash_task(this);
476 public static void show(AltosUIFrame frame) {
477 AltosFlashUI ui = new AltosFlashUI(frame);
481 public AltosFlashUI(AltosUIFrame in_frame) {
482 super(in_frame, "Program Altusmetrum Device", false);