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_11;
22 import java.awt.event.*;
24 import javax.swing.filechooser.FileNameExtensionFilter;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_11.*;
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 update_rom_config_info(AltosRomconfig existing_config) {
280 AltosRomconfig new_config;
281 new_config = AltosRomconfigUI.show(frame, existing_config);
282 if (new_config == null)
284 rom_config = new_config;
285 set_serial(rom_config.serial_number);
290 void exception (Exception e) {
291 if (e instanceof FileNotFoundException) {
292 JOptionPane.showMessageDialog(frame,
293 ((FileNotFoundException) e).getMessage(),
295 JOptionPane.ERROR_MESSAGE);
296 } else if (e instanceof AltosSerialInUseException) {
297 JOptionPane.showMessageDialog(frame,
298 String.format("Device \"%s\" already in use",
299 device.toShortString()),
301 JOptionPane.ERROR_MESSAGE);
302 } else if (e instanceof IOException) {
303 JOptionPane.showMessageDialog(frame,
306 JOptionPane.ERROR_MESSAGE);
310 class flash_task implements Runnable, AltosFlashListener {
313 AltosProgrammer programmer;
315 public void position(String in_s, int in_percent) {
316 final String s = in_s;
317 final int percent = in_percent;
318 Runnable r = new Runnable() {
321 ui.actionPerformed(new ActionEvent(this,
324 } catch (Exception ex) {
328 SwingUtilities.invokeLater(r);
333 if (ui.is_pair_programmed())
334 programmer = new AltosFlash(ui.file, link, this);
336 programmer = new AltosSelfFlash(ui.file, link, this);
338 final AltosRomconfig current_config = programmer.romconfig();
340 final Semaphore await_rom_config = new Semaphore(0);
341 SwingUtilities.invokeLater(new Runnable() {
343 ui.programmer = programmer;
344 ui.update_rom_config_info(current_config);
345 await_rom_config.release();
348 await_rom_config.acquire();
350 if (ui.rom_config != null) {
351 programmer.set_romconfig(ui.rom_config);
354 } catch (InterruptedException ee) {
355 final Exception e = ee;
356 SwingUtilities.invokeLater(new Runnable() {
361 } catch (IOException ee) {
362 final Exception e = ee;
363 SwingUtilities.invokeLater(new Runnable() {
369 if (programmer != null)
374 public flash_task(AltosFlashUI in_ui) {
376 t = new Thread(this);
383 private boolean open_device() throws InterruptedException {
385 link = new AltosSerial(device);
386 if (is_pair_programmed())
390 throw new IOException(String.format("%s: open failed", device.toShortString()));
392 while (!link.is_loader()) {
395 java.util.List<AltosDevice> devices = null;
397 for (int tries = 0; tries < 10; tries++) {
399 devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
400 if (devices.size() != 0)
404 if (devices.size() == 1)
405 device = devices.get(0);
407 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
411 link = new AltosSerial(device);
414 } catch (AltosSerialInUseException ee) {
416 } catch (FileNotFoundException fe) {
418 } catch (IOException ie) {
425 * Execute the steps for flashing
426 * a device. Note that this returns immediately;
427 * this dialog is not modal
430 if (!select_device())
432 if (!select_source_file())
437 } catch (InterruptedException ie) {
441 flash_task f = new flash_task(this);
444 public static void show(AltosUIFrame frame) {
445 AltosFlashUI ui = new AltosFlashUI(frame);
449 public AltosFlashUI(AltosUIFrame in_frame) {
450 super(in_frame, "Program Altusmetrum Device", false);