+ }
+ }
+
+ class flash_task implements Runnable, AltosFlashListener {
+ AltosFlashUI ui;
+ Thread t;
+ AltosProgrammer programmer;
+
+ public void position(String in_s, int in_percent) {
+ final String s = in_s;
+ final int percent = in_percent;
+ Runnable r = new Runnable() {
+ public void run() {
+ try {
+ ui.actionPerformed(new ActionEvent(this,
+ percent,
+ s));
+ } catch (Exception ex) {
+ }
+ }
+ };
+ SwingUtilities.invokeLater(r);
+ }
+
+ public void run () {
+ try {
+ if (ui.is_pair_programmed())
+ programmer = new AltosFlash(ui.file, link, this);
+ else
+ programmer = new AltosSelfFlash(ui.file, link, this);
+
+ final AltosRomconfig current_config = programmer.romconfig();
+
+ final Semaphore await_rom_config = new Semaphore(0);
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ ui.programmer = programmer;
+ ui.update_rom_config_info(current_config);
+ await_rom_config.release();
+ }
+ });
+ await_rom_config.acquire();
+
+ if (ui.rom_config != null) {
+ programmer.set_romconfig(ui.rom_config);
+ programmer.flash();
+ }
+ } catch (InterruptedException ee) {
+ final Exception e = ee;
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ ui.exception(e);
+ }
+ });
+ } catch (IOException ee) {
+ final Exception e = ee;
+ SwingUtilities.invokeLater(new Runnable() {
+ public void run() {
+ ui.exception(e);
+ }
+ });
+ } finally {
+ if (programmer != null)
+ programmer.close();
+ }
+ }
+
+ public flash_task(AltosFlashUI in_ui) {
+ ui = in_ui;
+ t = new Thread(this);
+ t.start();
+ }
+ }
+
+ flash_task flasher;
+
+ private boolean open_device() throws InterruptedException {
+ try {
+ link = new AltosSerial(device);
+ if (is_pair_programmed())
+ return true;
+
+ if (link == null)
+ throw new IOException(String.format("%s: open failed", device.toShortString()));
+
+ while (!link.is_loader()) {
+ link.to_loader();
+
+ java.util.List<AltosDevice> devices = null;
+
+ for (int tries = 0; tries < 10; tries++) {
+ Thread.sleep(100);
+ devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
+ if (devices.size() != 0)
+ break;
+ }
+
+ if (devices.size() == 1)
+ device = devices.get(0);
+ else {
+ device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
+ if (device == null)
+ return false;
+ }
+ link = new AltosSerial(device);
+ }
+ return true;
+ } catch (AltosSerialInUseException ee) {
+ exception(ee);
+ } catch (FileNotFoundException fe) {
+ exception(fe);
+ } catch (IOException ie) {
+ exception (ie);
+ }
+ return false;
+ }
+
+ /*
+ * Execute the steps for flashing
+ * a device. Note that this returns immediately;
+ * this dialog is not modal
+ */
+ void showDialog() {
+ if (!select_device())
+ return;
+ if (!select_source_file())
+ return;
+ try {
+ if (!open_device())
+ return;