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 = {
84 private boolean is_pair_programmed() {
87 String name = file.getName();
88 for (int i = 0; i < pair_programmed_files.length; i++) {
89 if (name.startsWith(pair_programmed_files[i]))
94 String name = device.toString();
95 for (int i = 0; i < pair_programmed_devices.length; i++) {
96 if (name.startsWith(pair_programmed_devices[i]))
103 public void actionPerformed(ActionEvent e) {
104 if (e.getSource() == cancel) {
105 if (programmer != null)
110 String cmd = e.getActionCommand();
111 if (e.getID() == -1) {
112 JOptionPane.showMessageDialog(frame,
113 e.getActionCommand(),
115 JOptionPane.ERROR_MESSAGE);
118 } else if (cmd.equals("done")) {
121 } else if (cmd.equals("start")) {
124 pbar.setValue(e.getID());
130 public void build_dialog() {
131 GridBagConstraints c;
132 Insets il = new Insets(4,4,4,4);
133 Insets ir = new Insets(4,4,4,4);
135 pane = getContentPane();
136 pane.setLayout(new GridBagLayout());
138 c = new GridBagConstraints();
139 c.gridx = 0; c.gridy = 0;
140 c.fill = GridBagConstraints.NONE;
141 c.anchor = GridBagConstraints.LINE_START;
143 serial_label = new JLabel("Serial:");
144 pane.add(serial_label, c);
146 c = new GridBagConstraints();
147 c.gridx = 1; c.gridy = 0;
148 c.fill = GridBagConstraints.HORIZONTAL;
150 c.anchor = GridBagConstraints.LINE_START;
152 serial_value = new JLabel("");
153 pane.add(serial_value, c);
155 c = new GridBagConstraints();
156 c.fill = GridBagConstraints.NONE;
157 c.gridx = 0; c.gridy = 1;
158 c.anchor = GridBagConstraints.LINE_START;
160 file_label = new JLabel("File:");
161 pane.add(file_label, c);
163 c = new GridBagConstraints();
164 c.fill = GridBagConstraints.HORIZONTAL;
166 c.gridx = 1; c.gridy = 1;
167 c.anchor = GridBagConstraints.LINE_START;
169 file_value = new JLabel(file.toString());
170 pane.add(file_value, c);
172 pbar = new JProgressBar();
174 pbar.setMaximum(100);
177 pbar.setStringPainted(true);
178 pbar.setPreferredSize(new Dimension(600, 20));
179 c = new GridBagConstraints();
180 c.fill = GridBagConstraints.HORIZONTAL;
181 c.anchor = GridBagConstraints.CENTER;
182 c.gridx = 0; c.gridy = 2;
183 c.gridwidth = GridBagConstraints.REMAINDER;
184 Insets ib = new Insets(4,4,4,4);
188 cancel = new JButton("Cancel");
189 c = new GridBagConstraints();
190 c.fill = GridBagConstraints.NONE;
191 c.anchor = GridBagConstraints.CENTER;
192 c.gridx = 0; c.gridy = 3;
193 c.gridwidth = GridBagConstraints.REMAINDER;
194 Insets ic = new Insets(4,4,4,4);
197 cancel.addActionListener(this);
199 setLocationRelativeTo(frame);
202 void set_serial(int serial_number) {
203 serial_value.setText(String.format("%d", serial_number));
206 static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
211 public AltosHexfileFilter(int product, String head, String description) {
212 this.product = product;
214 this.description = description;
217 public boolean accept(File file) {
218 return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
221 public String getDescription() {
226 static AltosHexfileFilter[] filters = {
227 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
228 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
229 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
230 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
231 new AltosHexfileFilter(AltosLib.product_easymega, "easymega", "EasyMega Image"),
234 boolean select_source_file() {
235 JFileChooser hexfile_chooser = new JFileChooser();
237 File firmwaredir = AltosUIPreferences.firmwaredir();
238 if (firmwaredir != null)
239 hexfile_chooser.setCurrentDirectory(firmwaredir);
241 hexfile_chooser.setDialogTitle("Select Flash Image");
243 for (int i = 0; i < filters.length; i++) {
244 hexfile_chooser.addChoosableFileFilter(filters[i]);
246 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
247 hexfile_chooser.addChoosableFileFilter(ihx_filter);
248 hexfile_chooser.setFileFilter(ihx_filter);
250 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
251 for (int i = 0; i < filters.length; i++) {
252 if (device != null && device.matchProduct(filters[i].product))
253 hexfile_chooser.setFileFilter(filters[i]);
257 int returnVal = hexfile_chooser.showOpenDialog(frame);
259 if (returnVal != JFileChooser.APPROVE_OPTION)
261 file = hexfile_chooser.getSelectedFile();
264 AltosUIPreferences.set_firmwaredir(file.getParentFile());
269 boolean select_device() {
270 int product = AltosLib.product_any;
272 device = AltosDeviceUIDialog.show(frame, AltosLib.product_any);
279 boolean rom_config_matches (AltosRomconfig a, AltosRomconfig b) {
280 if (a.usb_id != null && b.usb_id != null &&
281 (a.usb_id.vid != b.usb_id.vid ||
282 a.usb_id.pid != b.usb_id.pid))
285 if (a.usb_product != null && b.usb_product != null &&
286 !a.usb_product.equals(b.usb_product))
292 boolean update_rom_config_info(AltosRomconfig existing_config, AltosRomconfig image_config) {
293 AltosRomconfig new_config;
295 if (!rom_config_matches(existing_config, image_config)) {
296 int ret = JOptionPane.showConfirmDialog(this,
297 String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
298 existing_config.usb_id.vid,
299 existing_config.usb_id.pid,
300 existing_config.usb_product,
301 image_config.usb_id.vid,
302 image_config.usb_id.pid,
303 image_config.usb_product),
304 "Image doesn't match Device",
305 JOptionPane.YES_NO_OPTION);
306 if (ret != JOptionPane.YES_OPTION)
310 new_config = AltosRomconfigUI.show(frame, existing_config);
311 if (new_config == null)
313 rom_config = new_config;
314 set_serial(rom_config.serial_number);
319 void exception (Exception e) {
320 if (e instanceof FileNotFoundException) {
321 JOptionPane.showMessageDialog(frame,
322 ((FileNotFoundException) e).getMessage(),
324 JOptionPane.ERROR_MESSAGE);
325 } else if (e instanceof AltosSerialInUseException) {
326 JOptionPane.showMessageDialog(frame,
327 String.format("Device \"%s\" already in use",
328 device.toShortString()),
330 JOptionPane.ERROR_MESSAGE);
331 } else if (e instanceof IOException) {
332 JOptionPane.showMessageDialog(frame,
335 JOptionPane.ERROR_MESSAGE);
339 class flash_task implements Runnable, AltosFlashListener {
342 AltosProgrammer programmer;
344 public void position(String in_s, int in_percent) {
345 final String s = in_s;
346 final int percent = in_percent;
347 Runnable r = new Runnable() {
350 ui.actionPerformed(new ActionEvent(this,
353 } catch (Exception ex) {
357 SwingUtilities.invokeLater(r);
362 if (ui.is_pair_programmed())
363 programmer = new AltosFlash(ui.file, link, this);
365 programmer = new AltosSelfFlash(ui.file, link, this);
367 final AltosRomconfig current_config = programmer.target_romconfig();
369 final AltosRomconfig image_config = programmer.image_romconfig();
371 final Semaphore await_rom_config = new Semaphore(0);
372 SwingUtilities.invokeLater(new Runnable() {
374 ui.programmer = programmer;
375 ui.update_rom_config_info(current_config, image_config);
376 await_rom_config.release();
379 await_rom_config.acquire();
381 if (ui.rom_config != null) {
382 programmer.set_romconfig(ui.rom_config);
385 } catch (InterruptedException ee) {
386 final Exception e = ee;
387 SwingUtilities.invokeLater(new Runnable() {
392 } catch (IOException ee) {
393 final Exception e = ee;
394 SwingUtilities.invokeLater(new Runnable() {
400 if (programmer != null)
405 public flash_task(AltosFlashUI in_ui) {
407 t = new Thread(this);
414 private boolean open_device() throws InterruptedException {
416 link = new AltosSerial(device);
417 if (is_pair_programmed())
421 throw new IOException(String.format("%s: open failed", device.toShortString()));
423 while (!link.is_loader()) {
426 java.util.List<AltosDevice> devices = null;
428 for (int tries = 0; tries < 10; tries++) {
430 devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
431 if (devices.size() != 0)
435 if (devices.size() == 1)
436 device = devices.get(0);
438 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
442 link = new AltosSerial(device);
445 } catch (AltosSerialInUseException ee) {
447 } catch (FileNotFoundException fe) {
449 } catch (IOException ie) {
456 * Execute the steps for flashing
457 * a device. Note that this returns immediately;
458 * this dialog is not modal
461 if (!select_device())
463 if (!select_source_file())
468 } catch (InterruptedException ie) {
472 flash_task f = new flash_task(this);
475 public static void show(AltosUIFrame frame) {
476 AltosFlashUI ui = new AltosFlashUI(frame);
480 public AltosFlashUI(AltosUIFrame in_frame) {
481 super(in_frame, "Program Altusmetrum Device", false);