checkpoint
authoradam <adam@megacz.com>
Tue, 6 Sep 2005 05:03:45 +0000 (22:03 -0700)
committeradam <adam@megacz.com>
Tue, 6 Sep 2005 05:03:45 +0000 (22:03 -0700)
darcs-hash:20050906050345-5007d-006407cca7aee561097d1c07b0b89e1b8014edfc.gz

src/edu/berkeley/cs/obits/Atmel.java
src/edu/berkeley/cs/obits/AtmelSerial.java

index a01369b..1f01adb 100644 (file)
@@ -16,6 +16,7 @@ public class Atmel {
         Log.level = Log.DEBUG;
         new Bits().read(System.in);
     }
         Log.level = Log.DEBUG;
         new Bits().read(System.in);
     }
+
     private static class Bits {
         private byte[] bits    = new byte[0];
         //public int  get(int whichByte, int whichBit, int len) { }
     private static class Bits {
         private byte[] bits    = new byte[0];
         //public int  get(int whichByte, int whichBit, int len) { }
index a497c5a..08b4e48 100644 (file)
@@ -14,68 +14,60 @@ public class AtmelSerial {
         }
         return new RXTXPort("/dev/cu.usbserial-FTBUODP4");
     }
         }
         return new RXTXPort("/dev/cu.usbserial-FTBUODP4");
     }
+
+    public static class AvrDrone {
+        final DataInputStream in;
+        final DataOutputStream out;
+        final SerialPort sp;
+        public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException {
+            this.sp = sp;
+            sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+            sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
+            this.out = new DataOutputStream(sp.getOutputStream());
+            this.in = new DataInputStream(sp.getInputStream());
+            while(in.available() > 0) in.read();
+            reset();
+            System.err.println("waiting...");
+            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");
+            if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
+            if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
+            if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
+            if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
+            System.err.println("ready.");
+        }
+        public void reset() throws InterruptedException {
+            sp.setDTR(true);
+            Thread.sleep(500);
+            sp.setDTR(false);
+            Thread.sleep(3000);
+        }
+        public void mode4(int z, int y, int x, int d) throws IOException {
+            out.writeByte(1);
+            out.writeByte(z);
+            out.writeByte(y);
+            out.writeByte(x);
+            out.writeByte(d);
+        }
+        public void flush() throws IOException {
+            out.flush();
+        }
+    }
+
+
     public static void main(String[] s) throws Exception {
     public static void main(String[] s) throws Exception {
-        final SerialPort sp = detectObitsPort();
-        sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
-        sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
-        final OutputStream out = sp.getOutputStream();
-        final InputStream in = sp.getInputStream();
-        while(in.available() > 0) in.read();
-        sp.setDTR(true);
-        Thread.sleep(500);
-        sp.setDTR(false);
-        Thread.sleep(3000);
-        DataInputStream dis = new DataInputStream(in);
-        System.err.println("waiting...");
-        if (dis.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
-        if (dis.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
-        if (dis.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
-        if (dis.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
-        if (dis.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
-        if (dis.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
-        System.err.println("ready.");
+        AvrDrone device = new AvrDrone(detectObitsPort());
         int count = 0;
         int count = 0;
-        new Thread() {
-            public void run() {
-                try {
-                    while(true) {
-                        System.err.println(sp.isDTR() + " " + sp.isDSR() + " " + sp.isRTS() + " " + sp.isCTS());
-                        Thread.sleep(250);
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            } }.start();
-        new Thread() {
-            public void run() {
-                try {
-                    while(true) {
-                        int i2 = in.read();
-                        if (i2==-1) { System.err.println("input closed"); System.exit(-1); }
-                        System.out.print((char)i2);
-                        System.out.flush();
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }.start();
         try {
         try {
-            Thread.sleep(1000);
             long begin = System.currentTimeMillis();
             long begin = System.currentTimeMillis();
-            DataOutputStream dos = new DataOutputStream(out);
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             for(String str = br.readLine(); str != null; str = br.readLine()) {
                 long foo = Long.parseLong(str, 16);
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             for(String str = br.readLine(); str != null; str = br.readLine()) {
                 long foo = Long.parseLong(str, 16);
-                dos.writeByte(1);
-                dos.writeByte((int)(foo >> 24));
-                dos.writeByte((int)(foo >> 16));
-                dos.writeByte((int)(foo >>  8));
-                dos.writeByte((int)(foo >>  0));
+                device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
                 count++;
                 if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets");
             }
                 count++;
                 if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets");
             }
-            dos.flush();
+            device.flush();
             long end = System.currentTimeMillis();
             System.err.println("finished in " + ((end-begin)/1000) + "s");
             System.exit(0);
             long end = System.currentTimeMillis();
             System.err.println("finished in " + ((end-begin)/1000) + "s");
             System.exit(0);