checkpoint
authoradam <adam@megacz.com>
Mon, 11 Sep 2006 11:43:20 +0000 (12:43 +0100)
committeradam <adam@megacz.com>
Mon, 11 Sep 2006 11:43:20 +0000 (12:43 +0100)
src/edu/berkeley/obits/device/atmel/AvrDrone.java
src/edu/berkeley/obits/device/atmel/Chip.java
src/edu/berkeley/obits/device/atmel/ChipImpl.java

index 31f1e62..6e1a37c 100644 (file)
@@ -23,7 +23,6 @@ public class AvrDrone extends AtmelDevice {
     public void reset() { board.reset(); }
 
     private void init() throws IOException {
-        //board.reset();
         Log.debug(this, "waiting for device to identify itself");
         if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
         if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
index 9a91137..bbb0870 100644 (file)
@@ -2,38 +2,26 @@ package edu.berkeley.obits.device.atmel;
 import com.ftdi.usb.*;
 import java.io.*;
 
-public abstract class Chip {
+public interface Chip {
     
-    public void doReset() {
-        flush();
-        buffered(false);
+    public void doReset();
 
 
-        reset(false);
-        //avrrst(false);
-        try { Thread.sleep(200); } catch (Exception e) { }
-        reset(true);
-        //avrrst(true);
-        try { Thread.sleep(200); } catch (Exception e) { }
-    }
+    public void reset(boolean on);
+    public void avrrst(boolean on);
 
-    public abstract void reset(boolean on);
-    public abstract void avrrst(boolean on);
-    public abstract void int3(boolean on);
+    public void config(boolean bit);
+    public void config(int data, int numbits);
 
-    public abstract void config(boolean bit);
-    public abstract void config(int data, int numbits);
+    public boolean initErr();
+    public void    porte(int pin, boolean b);
 
-    public abstract boolean initErr();
-    public abstract boolean porte(int pin);
-    public abstract void porte(int pin, boolean b);
-
-    public abstract void    con(boolean b);
-    public abstract boolean con();
+    public void    con(boolean b);
+    public boolean con();
 
     //remove
-    public abstract void buffered();
-    public abstract void buffered(boolean buf);
-    protected abstract void flush();
-    public abstract int readPins();
+    public void buffered();
+    public void buffered(boolean buf);
+    public void flush();
+    public int readPins();
 }
index d86f73f..45076e3 100644 (file)
@@ -2,19 +2,29 @@ package edu.berkeley.obits.device.atmel;
 import com.ftdi.usb.*;
 import java.io.*;
 
-public class ChipImpl extends Chip {
+public class ChipImpl implements Chip {
+
+    private SWIGTYPE_p_ftdi_context context;
+    private int bits = 0;
 
     public void doReset() {
         dbangmode();
         clk(false);
         data(false);
+
         con(false);
-        super.doReset();
+        flush();
+        buffered(false);
+        reset(false);
+        //avrrst(false);
+        try { Thread.sleep(200); } catch (Exception e) { }
+        reset(true);
+        //avrrst(true);
+        try { Thread.sleep(200); } catch (Exception e) { }
+
         dmask &= ~(1<<7);
         dbangmode();
     }
-    public void int3(boolean on) { throw new RuntimeException("not connected"); }
-    public boolean porte(int pin) { throw new RuntimeException("not connected"); }
 
     int porte = 0;
     public void porte(int pin, boolean b) {
@@ -25,11 +35,6 @@ public class ChipImpl extends Chip {
         }
     }
 
-    public void config(boolean bit) { config(bit?1:0, 1); }
-
-    private SWIGTYPE_p_ftdi_context context;
-    private int bits = 0;
-
     public ChipImpl() {
         context = example.new_ftdi_context();
 
@@ -42,9 +47,9 @@ public class ChipImpl extends Chip {
             throw new RuntimeException("ftdi_usb_open() returned " + result);
 
         result = example.ftdi_set_baudrate(context, 750 * 1000);
-        //result = example.ftdi_set_baudrate(context, 1000 * 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);
@@ -59,139 +64,13 @@ public class ChipImpl extends Chip {
             throw new RuntimeException("ftdi_read_pins() returned " + result);
         return b[0];
     }
-
-    private OutputStream os = new ChipOutputStream();
-    private InputStream  is = new ChipInputStream();
-
-    public OutputStream getOutputStream() {
-        return os;
-    }
-    public InputStream  getInputStream() {
-        //example.ftdi_read_data_set_chunksize(context, 32);
-        return is;
-    }
-
-    public class ChipInputStream extends InputStream {
-        public int available() throws IOException {
-            // FIXME
-            return 0;
-        }
-        public long skip(long l) throws IOException {
-            throw new RuntimeException("not supported");
-        }
-        public int read() throws IOException {
-            System.out.println("read()");
-            byte[] b = new byte[1];
-            int result = 0;
-            while(result==0)
-                result = read(b, 0, 1);
-            if (result==-1)
-                throw new IOException("ftdi_read_pins() returned " + result);
-            return b[0] & 0xff;
-        }
-        public int read(byte[] b, int off, int len) throws IOException {
-            // FIXME: blocking reads?
-            int result = 0;
-            while(true) {
-                if (len==0) return 0;
-                    byte[] b0 = new byte[len];
-                    synchronized(ChipImpl.this) {
-                        result = example.ftdi_read_data(context, b0, len);
-                    }
-                    if (result == -1)
-                        throw new IOException("ftdi_read_pins() returned " + result);
-                    if (result>0) {
-                        System.arraycopy(b0, 0, b, off, result);
-                        return result;
-                    }
-                try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
-            }
-        }
-    }
-
-    public class ChipOutputStream extends OutputStream {
-        public void write(int b) throws IOException {
-            byte[] d = new byte[1];
-            d[0] = (byte)b;
-            write(d, 0, 1);
-        }
-        public void write(byte[] b, int off, int len) throws IOException {
-            byte[] b2 = new byte[64];
-            while(len > 0) {
-                System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
-                synchronized(ChipImpl.this) {
-                    int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
-                    if (result < 0)
-                        throw new IOException("ftdi_write_data() returned " + result);
-                    off += result;
-                    len -= result;
-                }
-            }
-        }
-    }
-
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
-    protected void flush() {
+    public void flush() {
         byte[] bytes = baos.toByteArray();
         baos = new ByteArrayOutputStream();
         dbang(bytes, bytes.length);
     }
     public boolean buffered = false;
-    public void buffered() { buffered = true; }
-    public void buffered(boolean buf) { buffered = buf; }
-    public void config(int dat) { config(dat, 8); }
-    public void config(int dat, int numbits) {
-        for(int i=(numbits-1); i>=0; i--) {
-            boolean bit = (dat & (1<<i)) != 0;
-            data(bit);
-            clk(true);
-            clk(false);
-        }
-    }
-
-    public void reset(boolean on) {
-        bits = on ? (1<<1) : 0;
-        cbangmode();
-        //dbang(0, on);
-    }
-    public void avrrst(boolean on) {
-        dbang(7, on);
-    }
-    public boolean initErr() { return (readPins() & (1<<4))!=0; }
-    public void clk(boolean on) { dbang(6, on); }
-    public void data(boolean on) { dbang(5, on); }
-
-    public boolean con() {
-
-        /*
-        mask &= ~(1<<0);
-        cbangmode();
-        boolean ret = (readPins() & (1<<0)) != 0;
-        dbangmode();
-        return ret;
-        */
-
-
-
-        dmask &= ~(1<<0);
-        dbangmode();
-        return (readPins() & (1<<0)) != 0;
-
-    }
-    public void con(boolean on) {
-
-        /*
-        mask |= (1<<0);
-        bits = on ? (1<<0) : 0;
-        cbangmode();
-        */
-
-
-        dmask |= (1<<0);
-        dbangmode();
-        dbang(0, on);
-
-    }
 
     private static int mask =
         (1<<0) |
@@ -264,4 +143,130 @@ public class ChipImpl extends Chip {
     private synchronized void dbang(byte[] b, int len) {
         example.ftdi_write_data(context, b, len);
     }
+
+    //
+
+    public void buffered() { buffered = true; }
+    public void buffered(boolean buf) { buffered = buf; }
+    public void config(boolean bit) { config(bit?1:0, 1); }
+    public void config(int dat) { config(dat, 8); }
+    public void config(int dat, int numbits) {
+        for(int i=(numbits-1); i>=0; i--) {
+            boolean bit = (dat & (1<<i)) != 0;
+            data(bit);
+            clk(true);
+            clk(false);
+        }
+    }
+
+    public void reset(boolean on) {
+        bits = on ? (1<<1) : 0;
+        cbangmode();
+        //dbang(0, on);
+    }
+    public void avrrst(boolean on) { dbang(7, on); }
+    public boolean initErr()       { return (readPins() & (1<<4))!=0; }
+    public void clk(boolean on)    { dbang(6, on); }
+    public void data(boolean on)   { dbang(5, on); }
+
+    public boolean con() {
+
+        /*
+        mask &= ~(1<<0);
+        cbangmode();
+        boolean ret = (readPins() & (1<<0)) != 0;
+        dbangmode();
+        return ret;
+        */
+
+
+
+        dmask &= ~(1<<0);
+        dbangmode();
+        return (readPins() & (1<<0)) != 0;
+
+    }
+    public void con(boolean on) {
+
+        /*
+        mask |= (1<<0);
+        bits = on ? (1<<0) : 0;
+        cbangmode();
+        */
+
+
+        dmask |= (1<<0);
+        dbangmode();
+        dbang(0, on);
+
+    }
+
+
+    // UART comm pair //////////////////////////////////////////////////////////////////////////////
+
+
+    private OutputStream os = new ChipOutputStream();
+    private InputStream  is = new ChipInputStream();
+    public OutputStream getOutputStream() { return os; }
+    public InputStream  getInputStream() { return is; }
+    
+    public class ChipInputStream extends InputStream {
+        public int available() throws IOException {
+            // FIXME
+            return 0;
+        }
+        public long skip(long l) throws IOException {
+            throw new RuntimeException("not supported");
+        }
+        public int read() throws IOException {
+            System.out.println("read()");
+            byte[] b = new byte[1];
+            int result = 0;
+            while(result==0)
+                result = read(b, 0, 1);
+            if (result==-1)
+                throw new IOException("ftdi_read_pins() returned " + result);
+            return b[0] & 0xff;
+        }
+        public int read(byte[] b, int off, int len) throws IOException {
+            // FIXME: blocking reads?
+            int result = 0;
+            while(true) {
+                if (len==0) return 0;
+                    byte[] b0 = new byte[len];
+                    synchronized(ChipImpl.this) {
+                        result = example.ftdi_read_data(context, b0, len);
+                    }
+                    if (result == -1)
+                        throw new IOException("ftdi_read_pins() returned " + result);
+                    if (result>0) {
+                        System.arraycopy(b0, 0, b, off, result);
+                        return result;
+                    }
+                try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
+            }
+        }
+    }
+
+    public class ChipOutputStream extends OutputStream {
+        public void write(int b) throws IOException {
+            byte[] d = new byte[1];
+            d[0] = (byte)b;
+            write(d, 0, 1);
+        }
+        public void write(byte[] b, int off, int len) throws IOException {
+            byte[] b2 = new byte[64];
+            while(len > 0) {
+                System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
+                synchronized(ChipImpl.this) {
+                    int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
+                    if (result < 0)
+                        throw new IOException("ftdi_write_data() returned " + result);
+                    off += result;
+                    len -= result;
+                }
+            }
+        }
+    }
+
 }