checkpoint
authoradam <adam@megacz.com>
Mon, 11 Sep 2006 11:47:47 +0000 (12:47 +0100)
committeradam <adam@megacz.com>
Mon, 11 Sep 2006 11:47:47 +0000 (12:47 +0100)
src/edu/berkeley/obits/device/atmel/ChipImpl.java
src/edu/berkeley/obits/device/atmel/FtdiChip.java [new file with mode: 0644]

index 45076e3..6231c02 100644 (file)
@@ -2,10 +2,12 @@ package edu.berkeley.obits.device.atmel;
 import com.ftdi.usb.*;
 import java.io.*;
 
-public class ChipImpl implements Chip {
+public class ChipImpl extends FtdiChip implements Chip {
 
-    private SWIGTYPE_p_ftdi_context context;
-    private int bits = 0;
+    public ChipImpl() {
+        super();
+        doReset();
+    }
 
     public void doReset() {
         dbangmode();
@@ -35,114 +37,6 @@ public class ChipImpl implements Chip {
         }
     }
 
-    public ChipImpl() {
-        context = example.new_ftdi_context();
-
-        int result = example.ftdi_init(context);
-        if (result != 0)
-            throw new RuntimeException("ftdi_initErr() returned " + result);
-
-        result = example.ftdi_usb_open(context, 0x6666, 0x3133);
-        if (result != 0)
-            throw new RuntimeException("ftdi_usb_open() returned " + result);
-
-        result = example.ftdi_set_baudrate(context, 750 * 1000);
-        if (result != 0)
-            throw new RuntimeException("ftdi_set_baudrate() returned " + result);
-
-        result = example.ftdi_set_line_property(context, 8, 0, 0);
-        if (result != 0)
-            throw new RuntimeException("ftdi_set_baudrate() returned " + result);
-
-        doReset();
-    }
-
-    public synchronized int readPins() {
-        byte[] b = new byte[1];
-        int result = example.ftdi_read_pins(context, b);
-        if (result != 0)
-            throw new RuntimeException("ftdi_read_pins() returned " + result);
-        return b[0];
-    }
-    ByteArrayOutputStream baos = new ByteArrayOutputStream();
-    public void flush() {
-        byte[] bytes = baos.toByteArray();
-        baos = new ByteArrayOutputStream();
-        dbang(bytes, bytes.length);
-    }
-    public boolean buffered = false;
-
-    private static int mask =
-        (1<<0) |
-        (1<<1)// |
-        //(1<<2) |
-        //(1<<3)
-        ;
-
-    private static int dmask =
-        //(1<<0) |
-
-        (1<<1) |
-        (1<<2) |
-
-        //(1<<3) |
-        //(1<<4) |
-        (1<<5) |
-        (1<<6) |
-        (1<<7);
-
-    public synchronized void purge() {
-        example.ftdi_usb_purge_buffers(context);
-
-        int result = example.ftdi_setflowctrl(context, (1 << 8));
-        if (result != 0)
-            throw new RuntimeException("ftdi_setflowcontrol() returned " +
-                                       result);
-    }
-    public synchronized void uart() {
-        int result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
-        if (result != 0)
-            throw new RuntimeException("ftdi_set_bitmode() returned " + result);
-
-        result = example.ftdi_setflowctrl(context, (1 << 8));
-        if (result != 0)
-            throw new RuntimeException("ftdi_setflowcontrol() returned " +
-                                       result);
-
-    }
-    public synchronized void dbangmode() {
-        int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
-        if (result != 0)
-            throw new RuntimeException("ftdi_set_bitmode() returned " + result);
-    }
-
-    private synchronized void cbangmode() {
-        int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
-        if (result != 0)
-            throw new RuntimeException("ftdi_set_bitmode() returned " + result);
-    }
-
-    private int dbits = 0;
-    private synchronized void dbang(int bit, boolean val) {
-        dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
-        if (buffered) {
-            baos.write((byte)dbits);
-        } else {
-            dbang((byte)dbits);
-        }
-    }
-    int write = 0;
-    private synchronized void dbang(byte by) {
-        byte[] b = new byte[1];
-        b[0] = by;
-        int result = example.ftdi_write_data(context, b, 1);
-        if (result != 1)
-            throw new RuntimeException("ftdi_write_data() returned " + result);
-    }
-    int queued = 0;
-    private synchronized void dbang(byte[] b, int len) {
-        example.ftdi_write_data(context, b, len);
-    }
 
     //
 
diff --git a/src/edu/berkeley/obits/device/atmel/FtdiChip.java b/src/edu/berkeley/obits/device/atmel/FtdiChip.java
new file mode 100644 (file)
index 0000000..bd9c1d9
--- /dev/null
@@ -0,0 +1,115 @@
+package edu.berkeley.obits.device.atmel;
+import com.ftdi.usb.*;
+import java.io.*;
+
+public class FtdiChip {
+    protected SWIGTYPE_p_ftdi_context context;
+    protected int bits = 0;
+    public FtdiChip() {
+        context = example.new_ftdi_context();
+
+        int result = example.ftdi_init(context);
+        if (result != 0)
+            throw new RuntimeException("ftdi_initErr() returned " + result);
+
+        result = example.ftdi_usb_open(context, 0x6666, 0x3133);
+        if (result != 0)
+            throw new RuntimeException("ftdi_usb_open() returned " + result);
+
+        int result = example.ftdi_set_baudrate(context, 750 * 1000);
+        if (result != 0)
+            throw new RuntimeException("ftdi_set_baudrate() returned " + result);
+
+        result = example.ftdi_set_line_property(context, 8, 0, 0);
+        if (result != 0)
+            throw new RuntimeException("ftdi_set_baudrate() returned " + result);
+    }
+
+    public synchronized int readPins() {
+        byte[] b = new byte[1];
+        int result = example.ftdi_read_pins(context, b);
+        if (result != 0)
+            throw new RuntimeException("ftdi_read_pins() returned " + result);
+        return b[0];
+    }
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    public void flush() {
+        byte[] bytes = baos.toByteArray();
+        baos = new ByteArrayOutputStream();
+        dbang(bytes, bytes.length);
+    }
+    public boolean buffered = false;
+
+    protected static int mask =
+        (1<<0) |
+        (1<<1)// |
+        //(1<<2) |
+        //(1<<3)
+        ;
+
+    protected static int dmask =
+        //(1<<0) |
+
+        (1<<1) |
+        (1<<2) |
+
+        //(1<<3) |
+        //(1<<4) |
+        (1<<5) |
+        (1<<6) |
+        (1<<7);
+
+    public synchronized void purge() {
+        example.ftdi_usb_purge_buffers(context);
+
+        int result = example.ftdi_setflowctrl(context, (1 << 8));
+        if (result != 0)
+            throw new RuntimeException("ftdi_setflowcontrol() returned " +
+                                       result);
+    }
+    public synchronized void uart() {
+
+        result = example.ftdi_set_bitmode(context, (short)0, (short)0x00);
+        if (result != 0)
+            throw new RuntimeException("ftdi_set_bitmode() returned " + result);
+
+        result = example.ftdi_setflowctrl(context, (1 << 8));
+        if (result != 0)
+            throw new RuntimeException("ftdi_setflowcontrol() returned " +
+                                       result);
+
+    }
+    public synchronized void dbangmode() {
+        int result = example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
+        if (result != 0)
+            throw new RuntimeException("ftdi_set_bitmode() returned " + result);
+    }
+
+    protected synchronized void cbangmode() {
+        int result = example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
+        if (result != 0)
+            throw new RuntimeException("ftdi_set_bitmode() returned " + result);
+    }
+
+    protected int dbits = 0;
+    protected synchronized void dbang(int bit, boolean val) {
+        dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
+        if (buffered) {
+            baos.write((byte)dbits);
+        } else {
+            dbang((byte)dbits);
+        }
+    }
+    int write = 0;
+    protected synchronized void dbang(byte by) {
+        byte[] b = new byte[1];
+        b[0] = by;
+        int result = example.ftdi_write_data(context, b, 1);
+        if (result != 1)
+            throw new RuntimeException("ftdi_write_data() returned " + result);
+    }
+    int queued = 0;
+    protected synchronized void dbang(byte[] b, int len) {
+        example.ftdi_write_data(context, b, len);
+    }
+}