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_2.*;
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() {
72 String name = file.getName();
73 for (int i = 0; i < pair_programmed.length; i++) {
74 if (name.startsWith(pair_programmed[i]))
80 public void actionPerformed(ActionEvent e) {
81 if (e.getSource() == cancel) {
82 if (programmer != null)
87 String cmd = e.getActionCommand();
88 if (e.getID() == -1) {
89 JOptionPane.showMessageDialog(frame,
92 JOptionPane.ERROR_MESSAGE);
95 } else if (cmd.equals("done")) {
98 } else if (cmd.equals("start")) {
101 pbar.setValue(e.getID());
107 public void build_dialog() {
108 GridBagConstraints c;
109 Insets il = new Insets(4,4,4,4);
110 Insets ir = new Insets(4,4,4,4);
112 pane = getContentPane();
113 pane.setLayout(new GridBagLayout());
115 c = new GridBagConstraints();
116 c.gridx = 0; c.gridy = 0;
117 c.fill = GridBagConstraints.NONE;
118 c.anchor = GridBagConstraints.LINE_START;
120 serial_label = new JLabel("Serial:");
121 pane.add(serial_label, c);
123 c = new GridBagConstraints();
124 c.gridx = 1; c.gridy = 0;
125 c.fill = GridBagConstraints.HORIZONTAL;
127 c.anchor = GridBagConstraints.LINE_START;
129 serial_value = new JLabel("");
130 pane.add(serial_value, c);
132 c = new GridBagConstraints();
133 c.fill = GridBagConstraints.NONE;
134 c.gridx = 0; c.gridy = 1;
135 c.anchor = GridBagConstraints.LINE_START;
137 file_label = new JLabel("File:");
138 pane.add(file_label, c);
140 c = new GridBagConstraints();
141 c.fill = GridBagConstraints.HORIZONTAL;
143 c.gridx = 1; c.gridy = 1;
144 c.anchor = GridBagConstraints.LINE_START;
146 file_value = new JLabel(file.toString());
147 pane.add(file_value, c);
149 pbar = new JProgressBar();
151 pbar.setMaximum(100);
154 pbar.setStringPainted(true);
155 pbar.setPreferredSize(new Dimension(600, 20));
156 c = new GridBagConstraints();
157 c.fill = GridBagConstraints.HORIZONTAL;
158 c.anchor = GridBagConstraints.CENTER;
159 c.gridx = 0; c.gridy = 2;
160 c.gridwidth = GridBagConstraints.REMAINDER;
161 Insets ib = new Insets(4,4,4,4);
165 cancel = new JButton("Cancel");
166 c = new GridBagConstraints();
167 c.fill = GridBagConstraints.NONE;
168 c.anchor = GridBagConstraints.CENTER;
169 c.gridx = 0; c.gridy = 3;
170 c.gridwidth = GridBagConstraints.REMAINDER;
171 Insets ic = new Insets(4,4,4,4);
174 cancel.addActionListener(this);
176 setLocationRelativeTo(frame);
179 void set_serial(int serial_number) {
180 serial_value.setText(String.format("%d", serial_number));
183 static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
188 public AltosHexfileFilter(int product, String head, String description) {
189 this.product = product;
191 this.description = description;
194 public boolean accept(File file) {
195 return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
198 public String getDescription() {
203 static AltosHexfileFilter[] filters = {
204 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
205 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
206 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
207 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
210 boolean select_source_file() {
211 JFileChooser hexfile_chooser = new JFileChooser();
213 File firmwaredir = AltosUIPreferences.firmwaredir();
214 if (firmwaredir != null)
215 hexfile_chooser.setCurrentDirectory(firmwaredir);
217 hexfile_chooser.setDialogTitle("Select Flash Image");
219 for (int i = 0; i < filters.length; i++) {
220 hexfile_chooser.addChoosableFileFilter(filters[i]);
222 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
223 hexfile_chooser.addChoosableFileFilter(ihx_filter);
224 hexfile_chooser.setFileFilter(ihx_filter);
226 if (!device.matchProduct(AltosLib.product_altusmetrum)) {
227 for (int i = 0; i < filters.length; i++) {
228 if (device != null && device.matchProduct(filters[i].product))
229 hexfile_chooser.setFileFilter(filters[i]);
233 int returnVal = hexfile_chooser.showOpenDialog(frame);
235 if (returnVal != JFileChooser.APPROVE_OPTION)
237 file = hexfile_chooser.getSelectedFile();
240 AltosUIPreferences.set_firmwaredir(file.getParentFile());
245 boolean select_device() {
246 int product = Altos.product_any;
248 device = AltosDeviceUIDialog.show(frame, Altos.product_any);
255 boolean update_rom_config_info(AltosRomconfig existing_config) {
256 AltosRomconfig new_config;
257 new_config = AltosRomconfigUI.show(frame, existing_config);
258 if (new_config == null)
260 rom_config = new_config;
261 set_serial(rom_config.serial_number);
266 void exception (Exception e) {
267 if (e instanceof FileNotFoundException) {
268 JOptionPane.showMessageDialog(frame,
269 ((FileNotFoundException) e).getMessage(),
271 JOptionPane.ERROR_MESSAGE);
272 } else if (e instanceof AltosSerialInUseException) {
273 JOptionPane.showMessageDialog(frame,
274 String.format("Device \"%s\" already in use",
275 device.toShortString()),
277 JOptionPane.ERROR_MESSAGE);
278 } else if (e instanceof IOException) {
279 JOptionPane.showMessageDialog(frame,
282 JOptionPane.ERROR_MESSAGE);
286 class flash_task implements Runnable, AltosFlashListener {
289 AltosProgrammer programmer;
291 public void position(String in_s, int in_percent) {
292 final String s = in_s;
293 final int percent = in_percent;
294 Runnable r = new Runnable() {
297 ui.actionPerformed(new ActionEvent(this,
300 } catch (Exception ex) {
304 SwingUtilities.invokeLater(r);
309 if (ui.is_pair_programmed())
310 programmer = new AltosFlash(ui.file, link, this);
312 programmer = new AltosSelfFlash(ui.file, link, this);
314 final AltosRomconfig current_config = programmer.romconfig();
316 final Semaphore await_rom_config = new Semaphore(0);
317 SwingUtilities.invokeLater(new Runnable() {
319 ui.programmer = programmer;
320 ui.update_rom_config_info(current_config);
321 await_rom_config.release();
324 await_rom_config.acquire();
326 if (ui.rom_config != null) {
327 programmer.set_romconfig(ui.rom_config);
330 } catch (InterruptedException ee) {
331 final Exception e = ee;
332 SwingUtilities.invokeLater(new Runnable() {
337 } catch (IOException ee) {
338 final Exception e = ee;
339 SwingUtilities.invokeLater(new Runnable() {
345 if (programmer != null)
350 public flash_task(AltosFlashUI in_ui) {
352 t = new Thread(this);
359 private boolean open_device() {
361 link = new AltosSerial(device);
362 if (is_pair_programmed())
366 throw new IOException(String.format("%s: open failed", device.toShortString()));
368 while (!link.is_loader()) {
371 java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
372 if (devices.size() == 1)
373 device = devices.get(0);
375 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
379 link = new AltosSerial(device);
382 } catch (AltosSerialInUseException ee) {
384 } catch (FileNotFoundException fe) {
386 } catch (IOException ie) {
393 * Execute the steps for flashing
394 * a device. Note that this returns immediately;
395 * this dialog is not modal
398 if (!select_device())
400 if (!select_source_file())
405 flash_task f = new flash_task(this);
408 static void show(JFrame frame) {
409 AltosFlashUI ui = new AltosFlashUI(frame);
414 public AltosFlashUI(JFrame in_frame) {
415 super(in_frame, "Program Altusmetrum Device", false);