src: Add easymini-v3.0
[fw/altos] / micropeak / MicroDownload.java
index cd109b796bd71b7be99bf3d8c3b88befb6262fe3..4dcb7a360e928e2457b084bc0bc5d3d6d545c3cd 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -23,10 +24,10 @@ import javax.swing.*;
 import java.io.*;
 import java.util.concurrent.*;
 import java.util.*;
-import org.altusmetrum.altoslib_3.*;
-import org.altusmetrum.altosuilib_1.*;
+import org.altusmetrum.altoslib_14.*;
+import org.altusmetrum.altosuilib_14.*;
 
-public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog {
+public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener, MicroSerialLog, WindowListener {
        MicroPeak       owner;
        Container       pane;
        AltosDevice     device;
@@ -37,26 +38,53 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
        Runnable        log_run;
        JTextArea       serial_log;
        JLabel          status_value;
+       int             log_column;
+
+       public void windowActivated(WindowEvent e) {
+       }
+
+       public void windowClosed(WindowEvent e) {
+               setVisible(false);
+               dispose();
+       }
+
+       public void windowClosing(WindowEvent e) {
+       }
+
+       public void windowDeactivated(WindowEvent e) {
+       }
+
+       public void windowDeiconified(WindowEvent e) {
+       }
+
+       public void windowIconified(WindowEvent e) {
+       }
+
+       public void windowOpened(WindowEvent e) {
+       }
 
        private void done_internal() {
-               if (data != null) {
-                       status_value.setText("Received MicroPeak Data");
-                       if (data.crc_valid) {
+               if (data != null && data.crc_valid) {
+                       if (data.nsamples == 0) {
+                               JOptionPane.showMessageDialog(owner,
+                                                             "No Flight Data Present",
+                                                             "Empty Log",
+                                                             JOptionPane.WARNING_MESSAGE);
+                       } else {
+                               status_value.setText("Received MicroPeak Data");
                                owner = owner.SetData(data);
                                MicroSave save = new MicroSave(owner, data);
                                if (save.runDialog())
                                        owner.SetName(data.name);
-                       } else {
-                               JOptionPane.showMessageDialog(owner,
-                                                             "Flight data corrupted",
-                                                             "Download Failed",
-                                                             JOptionPane.ERROR_MESSAGE);
                        }
-                       setVisible(false);
-                       dispose();
                } else {
-                       status_value.setText("Download Failed");
+                       JOptionPane.showMessageDialog(owner,
+                                                     "Download Failed",
+                                                     "Flight data corrupted",
+                                                     JOptionPane.ERROR_MESSAGE);
                }
+               setVisible(false);
+               dispose();
        }
 
        public void drain_queue() {
@@ -71,9 +99,26 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
                        }
                        if (c == '\r')
                                continue;
-                       byte[] bytes = new byte[1];
-                       bytes[0] = (byte) c;
-                       serial_log.append(new String(bytes, AltosLib.unicode_set));
+                       if (c == '\0')
+                               continue;
+                       String s;
+                       if (c == '\n') {
+                               s = "\n";
+                               log_column = 0;
+                       } else if (' ' <= c && c <= '~') {
+                               byte[] bytes = new byte[1];
+                               bytes[0] = (byte) c;
+                               s = new String(bytes, AltosLib.unicode_set);
+                               log_column += 1;
+                       } else {
+                               s = String.format("\\0x%02x", c & 0xff);
+                               log_column += 5;
+                       }
+                       serial_log.append(s);
+                       if (log_column > 40) {
+                               serial_log.append("\n");
+                               log_column = 0;
+                       }
                }
        }
 
@@ -105,26 +150,53 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
 
        public void run() {
                try {
-                       data = new MicroData(serial, device.toShortString());
-                       serial.close();
+                       for (;;) {
+                               try {
+                                       data = new MicroData(serial, device.toShortString());
+                                       if (data != null && data.crc_valid)
+                                               break;
+                               } catch (MicroData.NonHexcharException nhe) {
+                               }
+                       }
+                       write_thread.join();
                } catch (FileNotFoundException fe) {
                } catch (IOException ioe) {
                } catch (InterruptedException ie) {
+               } catch (MicroData.FileEndedException fee) {
                }
+               serial.close();
                done();
        }
 
        Thread  serial_thread;
+       Thread  write_thread;
 
-       public void start() {
-               try {
-                       serial = new MicroSerial(device);
-                       serial.set_log(this);
-               } catch (FileNotFoundException fe) {
-                       return;
+       public class SerialWriter implements Runnable {
+               MicroSerial serial;
+
+               public void run () {
+                       try {
+                               Thread.sleep(100);
+                               serial.write('l');
+                               serial.write('\n');
+                               serial.flush();
+                       } catch (InterruptedException ie) {
+                       }
                }
+
+               public SerialWriter(MicroSerial serial) {
+                       this.serial = serial;
+               }
+       }
+
+       public void start() {
+               serial.set_log(this);
                serial_thread = new Thread(this);
                serial_thread.start();
+
+               SerialWriter writer = new SerialWriter(serial);
+               write_thread = new Thread(writer);
+               write_thread.start();
        }
 
        public void actionPerformed(ActionEvent ae) {
@@ -135,7 +207,7 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
                setVisible(false);
        }
 
-       public MicroDownload(MicroPeak owner, AltosDevice device) {
+       public MicroDownload(MicroPeak owner, AltosDevice device, MicroSerial serial) {
                super (owner, "Download MicroPeak Data", false);
 
                int y = 0;
@@ -146,8 +218,9 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
 
                this.owner = owner;
                this.device = device;
+               this.serial = serial;
 
-               pane = getContentPane();
+               pane = getScrollablePane();
                pane.setLayout(new GridBagLayout());
 
                c = new GridBagConstraints();
@@ -172,12 +245,12 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
                c.gridx = 0; c.gridy = y;
                c.gridwidth = GridBagConstraints.REMAINDER;
                c.fill = GridBagConstraints.HORIZONTAL;
-               c.weightx = 1;
+               c.weightx = 0;
                c.anchor = GridBagConstraints.LINE_START;
                c.insets = ir;
                JLabel help_text = new JLabel(
                        "<html><i>Turn on the MicroPeak and place the LED inside the<br>" +
-                       "opening in the top of the MicroPeak USB adapter.<br>" +
+                       "opening in the top of the MicroPeak USB adapter.<br> " +
                        "Verify that the blue LED in the side of the USB adapter<br>" +
                        "is blinking along with the orange LED on the MicroPeak.</i></html>");
 //             help_text.setEditable(false);
@@ -205,8 +278,9 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
                c = new GridBagConstraints();
                c.gridx = 0; c.gridy = y;
                c.gridwidth = GridBagConstraints.REMAINDER;
-               c.fill = GridBagConstraints.HORIZONTAL;
+               c.fill = GridBagConstraints.BOTH;
                c.weightx = 1;
+               c.weighty = 1;
                c.anchor = GridBagConstraints.LINE_START;
                c.insets = ir;