checkpoint
[slipway.git] / src / edu / berkeley / slipway / FtdiBoard.java
index 7c83724..ba84988 100644 (file)
@@ -23,7 +23,7 @@ public class FtdiBoard extends Fpslic implements Board {
 
     public FtdiBoard() throws Exception {
         super(24, 24);
-        chip = new FpslicBoot(new FpslicBootPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000)));
+        chip = new FpslicBoot(new FpslicBootPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000/2)));
         String bstFile = this.getClass().getName();
         bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
         bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";
@@ -77,6 +77,7 @@ public class FtdiBoard extends Fpslic implements Board {
                 bytes[3] == (byte)'T' &&
                 bytes[4] == (byte)'S') {
                 System.out.println("\rsignature: got proper signature                  ");
+                chip.purge();
                 break;
             }
         }
@@ -86,10 +87,14 @@ public class FtdiBoard extends Fpslic implements Board {
             public void run() {
                 while(true) {
                     try {
-                        while(callbacks.size() == 0) Thread.sleep(500);
+                        while(callbacks.size() == 0) Thread.sleep(50);
                         byte b = in.readByte();
                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
+                        //System.out.println("readback " + b + " in " + (System.currentTimeMillis()-bc.time));
                         bc.call(b);
+                        synchronized(lock) {
+                            lock.notifyAll();
+                        }
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
@@ -108,7 +113,7 @@ public class FtdiBoard extends Fpslic implements Board {
         return cache[x][y][z];
     }
 
-    public void mode4(int z, int y, int x, int d) {
+    public synchronized void mode4(int z, int y, int x, int d) {
         try {
             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
@@ -124,15 +129,34 @@ public class FtdiBoard extends Fpslic implements Board {
         }
     }
 
-    public void flush() { try { out.flush(); } catch (IOException e) { throw new RuntimeException(e); } }
+    public synchronized void flush() {
+        try { out.flush(); } catch (IOException e) { throw new RuntimeException(e); } }
 
 
     // Callbacks //////////////////////////////////////////////////////////////////////////////
 
     private Vector callbacks = new Vector();
 
+    private Object lock = new Object();
+    private static final int limit = 40;
+
+    private void enqueue(ByteCallback bcb) {
+        synchronized(lock) {
+            try {
+                while (callbacks.size() >= limit) {
+                    lock.wait(100);
+                }
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+        bcb.time = System.currentTimeMillis();
+        callbacks.add(bcb);
+    }
+
     public static abstract class ByteCallback {
         public int result;
+        public long time;
         public abstract void call(byte b) throws Exception;
     }
 
@@ -145,11 +169,17 @@ public class FtdiBoard extends Fpslic implements Board {
                             ((in.read() & 0xff) << 16) |
                             ((in.read() & 0xff) << 8) |
                             ((in.read() & 0xff) << 0);
+                        timer =
+                            ((in.read() & 0xff) << 24) |
+                            ((in.read() & 0xff) << 16) |
+                            ((in.read() & 0xff) << 8) |
+                            ((in.read() & 0xff) << 0);
+                        //System.out.println("timer => " + Integer.toString(timer, 16));
                         this.notify();
                     }
                 };
             synchronized(bc) {
-                callbacks.add(bc);
+                enqueue(bc);
                 out.writeByte(3);
                 out.flush();
                 bc.wait();
@@ -158,18 +188,20 @@ public class FtdiBoard extends Fpslic implements Board {
         } catch (Exception e) { throw new RuntimeException(e); }
     }
 
+    public int timer = 0;
     public synchronized void readBus(ByteCallback bc) throws IOException {
-        callbacks.add(bc);
+        enqueue(bc);
         out.writeByte(2);
         out.flush();
     }
 
+    /*
     public synchronized void readInterrupts(ByteCallback bc) throws IOException {
-        callbacks.add(bc);
+        enqueue(bc);
         out.writeByte(3);
         out.flush();
     }
-
+    */
 
     // Util //////////////////////////////////////////////////////////////////////////////