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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altosuilib_3;
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_5.*;
28 public class AltosFlashUI
30 implements ActionListener
43 // Hex file with rom image
51 // Desired Rom configuration
52 AltosRomconfig rom_config;
55 AltosProgrammer programmer;
57 private static String[] pair_programmed = {
70 private boolean is_pair_programmed() {
73 String name = file.getName();
74 for (int i = 0; i < pair_programmed.length; i++) {
75 if (name.startsWith(pair_programmed[i]))
80 if (!device.matchProduct(AltosLib.product_altusmetrum) &&
81 (device.matchProduct(AltosLib.product_teledongle) ||
82 device.matchProduct(AltosLib.product_telebt)))
88 public void actionPerformed(ActionEvent e) {
89 if (e.getSource() == cancel) {
90 if (programmer != null)
95 String cmd = e.getActionCommand();
96 if (e.getID() == -1) {
97 JOptionPane.showMessageDialog(frame,
100 JOptionPane.ERROR_MESSAGE);
103 } else if (cmd.equals("done")) {
106 } else if (cmd.equals("start")) {
109 pbar.setValue(e.getID());
115 public void build_dialog() {
116 GridBagConstraints c;
117 Insets il = new Insets(4,4,4,4);
118 Insets ir = new Insets(4,4,4,4);
120 pane = getContentPane();
121 pane.setLayout(new GridBagLayout());
123 c = new GridBagConstraints();
124 c.gridx = 0; c.gridy = 0;
125 c.fill = GridBagConstraints.NONE;
126 c.anchor = GridBagConstraints.LINE_START;
128 serial_label = new JLabel("Serial:");
129 pane.add(serial_label, c);
131 c = new GridBagConstraints();
132 c.gridx = 1; c.gridy = 0;
133 c.fill = GridBagConstraints.HORIZONTAL;
135 c.anchor = GridBagConstraints.LINE_START;
137 serial_value = new JLabel("");
138 pane.add(serial_value, c);
140 c = new GridBagConstraints();
141 c.fill = GridBagConstraints.NONE;
142 c.gridx = 0; c.gridy = 1;
143 c.anchor = GridBagConstraints.LINE_START;
145 file_label = new JLabel("File:");
146 pane.add(file_label, c);
148 c = new GridBagConstraints();
149 c.fill = GridBagConstraints.HORIZONTAL;
151 c.gridx = 1; c.gridy = 1;
152 c.anchor = GridBagConstraints.LINE_START;
154 file_value = new JLabel(file.toString());
155 pane.add(file_value, c);
157 pbar = new JProgressBar();
159 pbar.setMaximum(100);
162 pbar.setStringPainted(true);
163 pbar.setPreferredSize(new Dimension(600, 20));
164 c = new GridBagConstraints();
165 c.fill = GridBagConstraints.HORIZONTAL;
166 c.anchor = GridBagConstraints.CENTER;
167 c.gridx = 0; c.gridy = 2;
168 c.gridwidth = GridBagConstraints.REMAINDER;
169 Insets ib = new Insets(4,4,4,4);
173 cancel = new JButton("Cancel");
174 c = new GridBagConstraints();
175 c.fill = GridBagConstraints.NONE;
176 c.anchor = GridBagConstraints.CENTER;
177 c.gridx = 0; c.gridy = 3;
178 c.gridwidth = GridBagConstraints.REMAINDER;
179 Insets ic = new Insets(4,4,4,4);
182 cancel.addActionListener(this);
184 setLocationRelativeTo(frame);
187 void set_serial(int serial_number) {
188 serial_value.setText(String.format("%d", serial_number));
191 static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
196 public AltosHexfileFilter(int product, String head, String description) {
197 this.product = product;
199 this.description = description;
202 public boolean accept(File file) {
203 return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
206 public String getDescription() {
211 static AltosHexfileFilter[] filters = {
212 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
213 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
214 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
215 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
216 new AltosHexfileFilter(AltosLib.product_easymega, "easymega", "EasyMega Image"),
219 boolean select_source_file() {
220 JFileChooser hexfile_chooser = new JFileChooser();
222 File firmwaredir = AltosUIPreferences.firmwaredir();
223 if (firmwaredir != null)
224 hexfile_chooser.setCurrentDirectory(firmwaredir);
226 hexfile_chooser.setDialogTitle("Select Flash Image");
228 for (int i = 0; i < filters.length; i++) {
229 hexfile_chooser.addChoosableFileFilter(filters[i]);
231 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
232 hexfile_chooser.addChoosableFileFilter(ihx_filter);
233 hexfile_chooser.setFileFilter(ihx_filter);
235 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
236 for (int i = 0; i < filters.length; i++) {
237 if (device != null && device.matchProduct(filters[i].product))
238 hexfile_chooser.setFileFilter(filters[i]);
242 int returnVal = hexfile_chooser.showOpenDialog(frame);
244 if (returnVal != JFileChooser.APPROVE_OPTION)
246 file = hexfile_chooser.getSelectedFile();
249 AltosUIPreferences.set_firmwaredir(file.getParentFile());
254 boolean select_device() {
255 int product = AltosLib.product_any;
257 device = AltosDeviceUIDialog.show(frame, AltosLib.product_any);
264 boolean update_rom_config_info(AltosRomconfig existing_config) {
265 AltosRomconfig new_config;
266 new_config = AltosRomconfigUI.show(frame, existing_config);
267 if (new_config == null)
269 rom_config = new_config;
270 set_serial(rom_config.serial_number);
275 void exception (Exception e) {
276 if (e instanceof FileNotFoundException) {
277 JOptionPane.showMessageDialog(frame,
278 ((FileNotFoundException) e).getMessage(),
280 JOptionPane.ERROR_MESSAGE);
281 } else if (e instanceof AltosSerialInUseException) {
282 JOptionPane.showMessageDialog(frame,
283 String.format("Device \"%s\" already in use",
284 device.toShortString()),
286 JOptionPane.ERROR_MESSAGE);
287 } else if (e instanceof IOException) {
288 JOptionPane.showMessageDialog(frame,
291 JOptionPane.ERROR_MESSAGE);
295 class flash_task implements Runnable, AltosFlashListener {
298 AltosProgrammer programmer;
300 public void position(String in_s, int in_percent) {
301 final String s = in_s;
302 final int percent = in_percent;
303 Runnable r = new Runnable() {
306 ui.actionPerformed(new ActionEvent(this,
309 } catch (Exception ex) {
313 SwingUtilities.invokeLater(r);
318 if (ui.is_pair_programmed())
319 programmer = new AltosFlash(ui.file, link, this);
321 programmer = new AltosSelfFlash(ui.file, link, this);
323 final AltosRomconfig current_config = programmer.romconfig();
325 final Semaphore await_rom_config = new Semaphore(0);
326 SwingUtilities.invokeLater(new Runnable() {
328 ui.programmer = programmer;
329 ui.update_rom_config_info(current_config);
330 await_rom_config.release();
333 await_rom_config.acquire();
335 if (ui.rom_config != null) {
336 programmer.set_romconfig(ui.rom_config);
339 } catch (InterruptedException ee) {
340 final Exception e = ee;
341 SwingUtilities.invokeLater(new Runnable() {
346 } catch (IOException ee) {
347 final Exception e = ee;
348 SwingUtilities.invokeLater(new Runnable() {
354 if (programmer != null)
359 public flash_task(AltosFlashUI in_ui) {
361 t = new Thread(this);
368 private boolean open_device() throws InterruptedException {
370 link = new AltosSerial(device);
371 if (is_pair_programmed())
375 throw new IOException(String.format("%s: open failed", device.toShortString()));
377 while (!link.is_loader()) {
380 java.util.List<AltosDevice> devices = null;
382 for (int tries = 0; tries < 10; tries++) {
384 devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
385 if (devices.size() != 0)
389 if (devices.size() == 1)
390 device = devices.get(0);
392 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
396 link = new AltosSerial(device);
399 } catch (AltosSerialInUseException ee) {
401 } catch (FileNotFoundException fe) {
403 } catch (IOException ie) {
410 * Execute the steps for flashing
411 * a device. Note that this returns immediately;
412 * this dialog is not modal
415 if (!select_device())
417 if (!select_source_file())
422 } catch (InterruptedException ie) {
426 flash_task f = new flash_task(this);
429 public static void show(AltosUIFrame frame) {
430 AltosFlashUI ui = new AltosFlashUI(frame);
434 public AltosFlashUI(AltosUIFrame in_frame) {
435 super(in_frame, "Program Altusmetrum Device", false);