micropeak: Keep reading until we get valid data
authorKeith Packard <keithp@keithp.com>
Sat, 8 Feb 2014 06:32:17 +0000 (22:32 -0800)
committerKeith Packard <keithp@keithp.com>
Sat, 8 Feb 2014 06:32:17 +0000 (22:32 -0800)
Don't stop just because we saw something invalid on the serial line;
let the user try again, or cancel when they get bored.

However, if the serial line disappears or some other fatal error
occurs, then do stop and put up an error dialog.

Signed-off-by: Keith Packard <keithp@keithp.com>
micropeak/MicroData.java
micropeak/MicroDownload.java

index 04e9ad3..e786ff1 100644 (file)
@@ -100,13 +100,13 @@ public class MicroData implements AltosUIDataSet {
        String                  name;
        MicroStats              stats;
        
-       class FileEndedException extends Exception {
+       public class FileEndedException extends Exception {
        }
 
-       class NonHexcharException extends Exception {
+       public class NonHexcharException extends Exception {
        }
 
-       class InvalidCrcException extends Exception {
+       public class InvalidCrcException extends Exception {
        }
 
        private int getc(InputStream f) throws IOException, FileEndedException {
@@ -167,14 +167,10 @@ public class MicroData implements AltosUIDataSet {
                return h;
        }
 
-       private boolean find_header(InputStream f) throws IOException {
-               try {
-                       for (;;) {
-                               if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
-                                       return true;
-                       }
-               } catch (FileEndedException fe) {
-                       return false;
+       private boolean find_header(InputStream f) throws IOException, FileEndedException {
+               for (;;) {
+                       if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
+                               return true;
                }
        } 
 
@@ -339,7 +335,7 @@ public class MicroData implements AltosUIDataSet {
                this.name = name;
        }
 
-       public MicroData (InputStream f, String name) throws IOException, InterruptedException {
+       public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
                this.name = name;
                bytes = new ArrayList<Integer>();
                if (!find_header(f))
@@ -384,8 +380,6 @@ public class MicroData implements AltosUIDataSet {
                        stats = new MicroStats(this);
                } catch (FileEndedException fe) {
                        throw new IOException("File Ended Unexpectedly");
-               } catch (NonHexcharException ne) {
-                       throw new IOException("Non hexadecimal character found");
                }
        }
 
index cd109b7..7d2e9eb 100644 (file)
@@ -26,7 +26,7 @@ import java.util.*;
 import org.altusmetrum.altoslib_3.*;
 import org.altusmetrum.altosuilib_1.*;
 
-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,25 +37,46 @@ 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) {
+               setVisible(false);
+               dispose();
+
+               if (data != null && data.crc_valid) {
                        status_value.setText("Received MicroPeak Data");
-                       if (data.crc_valid) {
-                               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();
+                       owner = owner.SetData(data);
+                       MicroSave save = new MicroSave(owner, data);
+                       if (save.runDialog())
+                               owner.SetName(data.name);
                } else {
-                       status_value.setText("Download Failed");
+                       JOptionPane.showMessageDialog(owner,
+                                                     "Download Failed",
+                                                     "Flight data corrupted",
+                                                     JOptionPane.ERROR_MESSAGE);
                }
        }
 
@@ -71,9 +92,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,12 +143,20 @@ 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) {
+                               }
+                       }
                } catch (FileNotFoundException fe) {
                } catch (IOException ioe) {
                } catch (InterruptedException ie) {
+               } catch (MicroData.FileEndedException fee) {
                }
+               serial.close();
                done();
        }
 
@@ -172,12 +218,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 +251,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;