altosui: Make serial debug more complete and accurate
[fw/altos] / altosui / AltosSerial.java
index 6dce6f3d284c3b2326f6990a561dbe80c153a217..e609c6b5eeab6d840d541d39e786ebdac62875c8 100644 (file)
@@ -47,6 +47,12 @@ public class AltosSerial implements Runnable {
        byte[] line_bytes;
        int line_count;
        boolean monitor_mode;
+       static boolean debug;
+       LinkedList<String> pending_output = new LinkedList<String>();
+
+       static void set_debug(boolean new_debug) {
+               debug = new_debug;
+       }
 
        public void run () {
                int c;
@@ -78,13 +84,14 @@ public class AltosSerial implements Runnable {
                                                                for (int i = 0; i < line_count; i++)
                                                                        line = line + line_bytes[i];
                                                        }
+                                                       if (debug)
+                                                               System.out.printf("\t\t\t\t\t%s\n", line);
                                                        if (line.startsWith("VERSION") || line.startsWith("CRC")) {
                                                                for (int e = 0; e < monitors.size(); e++) {
                                                                        LinkedBlockingQueue<AltosLine> q = monitors.get(e);
                                                                        q.put(new AltosLine (line));
                                                                }
                                                        } else {
-//                                                             System.out.printf("GOT: %s\n", line);
                                                                reply_queue.put(new AltosLine (line));
                                                        }
                                                        line_count = 0;
@@ -108,8 +115,12 @@ public class AltosSerial implements Runnable {
        }
 
        public void flush_output() {
-               if (altos != null)
+               if (altos != null) {
+                       for (String s : pending_output)
+                               System.out.print(s);
+                       pending_output.clear();
                        libaltos.altos_flush(altos);
+               }
        }
 
        public void flush_input() {
@@ -174,15 +185,18 @@ public class AltosSerial implements Runnable {
                synchronized (devices_opened) {
                        devices_opened.remove(device.getPath());
                }
+               if (debug)
+                       System.out.printf("Closing %s\n", device.getPath());
        }
 
-       public void putc(char c) {
+       private void putc(char c) {
                if (altos != null)
                        libaltos.altos_putchar(altos, c);
        }
 
        public void print(String data) {
-//             System.out.printf("\"%s\" ", data);
+               if (debug)
+                       pending_output.add(data);
                for (int i = 0; i < data.length(); i++)
                        putc(data.charAt(i));
        }
@@ -202,6 +216,8 @@ public class AltosSerial implements Runnable {
                        close();
                        throw new FileNotFoundException(device.toShortString());
                }
+               if (debug)
+                       System.out.printf("Open %s\n", device.getPath());
                input_thread = new Thread(this);
                input_thread.start();
                print("~\nE 0\n");
@@ -250,6 +266,7 @@ public class AltosSerial implements Runnable {
 
        public void stop_remote() {
                printf ("~");
+               flush_output();
        }
 
        public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {