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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_3.*;
27 import org.altusmetrum.altosuilib_1.*;
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 String[] pair_programmed = {
71 private boolean is_pair_programmed() {
74 String name = file.getName();
75 for (int i = 0; i < pair_programmed.length; i++) {
76 if (name.startsWith(pair_programmed[i]))
81 if (!device.matchProduct(AltosLib.product_altusmetrum) &&
82 (device.matchProduct(AltosLib.product_teledongle) ||
83 device.matchProduct(AltosLib.product_telebt)))
89 public void actionPerformed(ActionEvent e) {
90 if (e.getSource() == cancel) {
91 if (programmer != null)
96 String cmd = e.getActionCommand();
97 if (e.getID() == -1) {
98 JOptionPane.showMessageDialog(frame,
101 JOptionPane.ERROR_MESSAGE);
104 } else if (cmd.equals("done")) {
107 } else if (cmd.equals("start")) {
110 pbar.setValue(e.getID());
116 public void build_dialog() {
117 GridBagConstraints c;
118 Insets il = new Insets(4,4,4,4);
119 Insets ir = new Insets(4,4,4,4);
121 pane = getContentPane();
122 pane.setLayout(new GridBagLayout());
124 c = new GridBagConstraints();
125 c.gridx = 0; c.gridy = 0;
126 c.fill = GridBagConstraints.NONE;
127 c.anchor = GridBagConstraints.LINE_START;
129 serial_label = new JLabel("Serial:");
130 pane.add(serial_label, c);
132 c = new GridBagConstraints();
133 c.gridx = 1; c.gridy = 0;
134 c.fill = GridBagConstraints.HORIZONTAL;
136 c.anchor = GridBagConstraints.LINE_START;
138 serial_value = new JLabel("");
139 pane.add(serial_value, c);
141 c = new GridBagConstraints();
142 c.fill = GridBagConstraints.NONE;
143 c.gridx = 0; c.gridy = 1;
144 c.anchor = GridBagConstraints.LINE_START;
146 file_label = new JLabel("File:");
147 pane.add(file_label, c);
149 c = new GridBagConstraints();
150 c.fill = GridBagConstraints.HORIZONTAL;
152 c.gridx = 1; c.gridy = 1;
153 c.anchor = GridBagConstraints.LINE_START;
155 file_value = new JLabel(file.toString());
156 pane.add(file_value, c);
158 pbar = new JProgressBar();
160 pbar.setMaximum(100);
163 pbar.setStringPainted(true);
164 pbar.setPreferredSize(new Dimension(600, 20));
165 c = new GridBagConstraints();
166 c.fill = GridBagConstraints.HORIZONTAL;
167 c.anchor = GridBagConstraints.CENTER;
168 c.gridx = 0; c.gridy = 2;
169 c.gridwidth = GridBagConstraints.REMAINDER;
170 Insets ib = new Insets(4,4,4,4);
174 cancel = new JButton("Cancel");
175 c = new GridBagConstraints();
176 c.fill = GridBagConstraints.NONE;
177 c.anchor = GridBagConstraints.CENTER;
178 c.gridx = 0; c.gridy = 3;
179 c.gridwidth = GridBagConstraints.REMAINDER;
180 Insets ic = new Insets(4,4,4,4);
183 cancel.addActionListener(this);
185 setLocationRelativeTo(frame);
188 void set_serial(int serial_number) {
189 serial_value.setText(String.format("%d", serial_number));
192 static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
197 public AltosHexfileFilter(int product, String head, String description) {
198 this.product = product;
200 this.description = description;
203 public boolean accept(File file) {
204 return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
207 public String getDescription() {
212 static AltosHexfileFilter[] filters = {
213 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
214 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
215 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
216 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini 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 = Altos.product_any;
257 device = AltosDeviceUIDialog.show(frame, Altos.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 = AltosUSBDevice.list(AltosLib.product_altusmetrum);
381 if (devices.size() == 1)
382 device = devices.get(0);
384 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
388 link = new AltosSerial(device);
391 } catch (AltosSerialInUseException ee) {
393 } catch (FileNotFoundException fe) {
395 } catch (IOException ie) {
402 * Execute the steps for flashing
403 * a device. Note that this returns immediately;
404 * this dialog is not modal
407 if (!select_device())
409 if (!select_source_file())
414 } catch (InterruptedException ie) {
418 flash_task f = new flash_task(this);
421 static void show(JFrame frame) {
422 AltosFlashUI ui = new AltosFlashUI(frame);
427 public AltosFlashUI(JFrame in_frame) {
428 super(in_frame, "Program Altusmetrum Device", false);