checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / FtdiChip.java
index 18b4087..a3d5f87 100644 (file)
@@ -13,6 +13,7 @@ public class FtdiChip {
     public FtdiChip() {
         example.ftdi_init(context);
         example.ftdi_usb_open(context, 0x6666, 0x3133);
+        example.ftdi_usb_reset(context);
         example.ftdi_set_baudrate(context, 750 * 1000);
         example.ftdi_set_line_property(context, 8, 0, 0);
     }
@@ -23,17 +24,12 @@ public class FtdiChip {
         return b[0];
     }
 
-    ByteArrayOutputStream baos = new ByteArrayOutputStream();
     public void flush() {
         try {
-            byte[] bytes = baos.toByteArray();
-            baos = new ByteArrayOutputStream();
-            out.write(bytes, 0, bytes.length);
-            out.flush();
-        } catch (IOException e) { throw new RuntimeException(e); }
+            getOutputStream().flush();
+        } catch (Exception e) { throw new RuntimeException(e); }
     }
 
-    public boolean buffered = false;
     protected static int mask =
         (1<<0) |
         (1<<1)// |
@@ -53,16 +49,20 @@ public class FtdiChip {
     }
 
     protected int dbits = 0;
+
+    public boolean buffered = false;
+    public void buffered() { /*buffered = true;*/ }
+    public void buffered(boolean buf) {
+        if (!buf) flush();
+        buffered = buf;
+    }
+
     protected synchronized void dbang(int bit, boolean val) {
         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
-        if (buffered) {
-            baos.write((byte)dbits);
-        } else {
-            try {
-                out.write((byte)dbits);
-                out.flush();
-            } catch (IOException e) { throw new RuntimeException(e); }
-        }
+        try {
+            out.write((byte)dbits);
+            if (!buffered) out.flush();
+        } catch (IOException e) { throw new RuntimeException(e); }
     }