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.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 System.out.printf ("device %s filter %d\n", device, filters[i].product);
229 if (device != null && device.matchProduct(filters[i].product)) {
230 System.out.printf ("select filter %s\n", filters[i].head);
231 hexfile_chooser.setFileFilter(filters[i]);
236 int returnVal = hexfile_chooser.showOpenDialog(frame);
238 if (returnVal != JFileChooser.APPROVE_OPTION)
240 file = hexfile_chooser.getSelectedFile();
243 AltosUIPreferences.set_firmwaredir(file.getParentFile());
248 boolean select_device() {
249 int product = Altos.product_any;
251 device = AltosDeviceUIDialog.show(frame, Altos.product_any);
258 boolean update_rom_config_info(AltosRomconfig existing_config) {
259 AltosRomconfig new_config;
260 new_config = AltosRomconfigUI.show(frame, existing_config);
261 if (new_config == null)
263 rom_config = new_config;
264 set_serial(rom_config.serial_number);
269 void exception (Exception e) {
270 if (e instanceof FileNotFoundException) {
271 JOptionPane.showMessageDialog(frame,
272 ((FileNotFoundException) e).getMessage(),
274 JOptionPane.ERROR_MESSAGE);
275 } else if (e instanceof AltosSerialInUseException) {
276 JOptionPane.showMessageDialog(frame,
277 String.format("Device \"%s\" already in use",
278 device.toShortString()),
280 JOptionPane.ERROR_MESSAGE);
281 } else if (e instanceof IOException) {
282 JOptionPane.showMessageDialog(frame,
285 JOptionPane.ERROR_MESSAGE);
289 class flash_task implements Runnable, AltosFlashListener {
292 AltosProgrammer programmer;
294 public void position(String in_s, int in_percent) {
295 final String s = in_s;
296 final int percent = in_percent;
297 Runnable r = new Runnable() {
300 ui.actionPerformed(new ActionEvent(this,
303 } catch (Exception ex) {
307 SwingUtilities.invokeLater(r);
312 if (ui.is_pair_programmed())
313 programmer = new AltosFlash(ui.file, link, this);
315 programmer = new AltosSelfFlash(ui.file, link, this);
317 final AltosRomconfig current_config = programmer.romconfig();
319 final Semaphore await_rom_config = new Semaphore(0);
320 SwingUtilities.invokeLater(new Runnable() {
322 ui.programmer = programmer;
323 ui.update_rom_config_info(current_config);
324 await_rom_config.release();
327 await_rom_config.acquire();
329 if (ui.rom_config != null) {
330 programmer.set_romconfig(ui.rom_config);
333 } catch (InterruptedException ee) {
334 final Exception e = ee;
335 SwingUtilities.invokeLater(new Runnable() {
340 } catch (IOException ee) {
341 final Exception e = ee;
342 SwingUtilities.invokeLater(new Runnable() {
348 if (programmer != null)
353 public flash_task(AltosFlashUI in_ui) {
355 t = new Thread(this);
362 private boolean open_device() {
364 link = new AltosSerial(device);
365 if (is_pair_programmed())
369 throw new IOException(String.format("%s: open failed", device.toShortString()));
371 while (!link.is_loader()) {
374 java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
375 if (devices.size() == 1)
376 device = devices.get(0);
378 device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
382 link = new AltosSerial(device);
385 } catch (AltosSerialInUseException ee) {
387 } catch (FileNotFoundException fe) {
389 } catch (IOException ie) {
396 * Execute the steps for flashing
397 * a device. Note that this returns immediately;
398 * this dialog is not modal
401 if (!select_device())
403 if (!select_source_file())
408 flash_task f = new flash_task(this);
411 static void show(JFrame frame) {
412 AltosFlashUI ui = new AltosFlashUI(frame);
417 public AltosFlashUI(JFrame in_frame) {
418 super(in_frame, "Program Altusmetrum Device", false);