checkpoint
[eltron.git] / src / edu / berkeley / cs / obits / AtmelSerial.java
index a497c5a..b43d0f9 100644 (file)
@@ -1,5 +1,7 @@
 package edu.berkeley.cs.obits;
 
+import edu.berkeley.cs.obits.device.atmel.*;
+import org.ibex.util.*;
 import java.io.*;
 import java.util.*;
 import gnu.io.*;
@@ -10,76 +12,114 @@ public class AtmelSerial {
         Enumeration e = CommPortIdentifier.getPortIdentifiers();
         while(e.hasMoreElements()) {
             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
-            System.err.println("trying " + cpi.getName());
+            Log.info(AtmelSerial.class, "trying " + cpi.getName());
         }
-        return new RXTXPort("/dev/cu.usbserial-FTBUODP4");
+        SerialPort ret = new RXTXPort("/dev/cu.usbserial-FTBUODP4");
+        Log.info(AtmelSerial.class, "returning " + ret);
+        return ret;
     }
+
     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.");
+        SerialPort sp = detectObitsPort();
+        sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+        //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
+        OutputStream out = sp.getOutputStream();
+        InputStream in = sp.getInputStream();
+        int count = 0;
+        PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
+        /*
+        pw.println("Y38,N,8,1");
+        pw.flush();
+        sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+
+        pw.println("GI");
+        pw.flush();
+        */
+
+        pw.println();
+        pw.flush();
+        pw.println("^@");
+        pw.println("^@");
+        pw.println("^@");
+        pw.flush();
+        try { Thread.sleep(3000); } catch (Exception e) { }
+
+        pw.println("GK\"IMG\"");
+        pw.println("GK\"IMG\"");
+        pw.println();
+        pw.flush();
+        try { Thread.sleep(1000); } catch (Exception e) { }
+        /*
+        pw.println("GI");
+        pw.flush();
+        */
+        int[] data = new int[104 * 104];
+        for(int i=0; i<104*104; i++) data[i] = 1;
+        for(int i=0; i<104; i++) data[i*104+i] = 0;
+        for(int i=0; i<104; i++) data[i*104+(104-i)] = 0;
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        PCX.dump(104, 104, data, new DataOutputStream(baos));
+        byte[] outb = baos.toByteArray();
+        int len = outb.length;
+        pw.println("GM\"IMG\""+len);
+        pw.flush();
+        DataOutputStream dout = new DataOutputStream(out);
+        for(int i=0; i<len; i++) {
+            System.out.println("wrote " + i + "/"+outb.length);
+            dout.writeByte(outb[i]);
+            dout.flush();
+        }
+        dout.flush();
+
+        pw.println();
+        pw.println("GI");
+        pw.flush();
+
+        try { Thread.sleep(2000); } catch (Exception e) { }
+
+        pw.println();
+        pw.println("OD");
+        pw.println("N");
+        pw.println("D14");
+        pw.println("S1");
+        pw.println("Q609,24");
+        pw.println("q754");
+        //pw.println("R0,0");
+        pw.println("A170,5,0,1,5,5,N,\"WORLDWIDE\"");
+        pw.println("LO5,230,765,10");
+        pw.println("A10,265,0,1,3,3,R,\"MODEL:\"");
+        pw.println("A280,265,0,1,3,3,N,\"Bar Code Printer\"");
+        pw.println("A10,340,0,1,3,3,R,\"  CODE: \"");
+        pw.println("B280,340,0,3C,2,6,120,B,\"BCP-1234\"");
+        pw.println("LO5,520,765,10");
+        pw.println("A100,550,0,1,2,2,N,\"ISO9000     Made In USA\"");
+        pw.println("GG0,0,\"IMG2\"");
+        pw.println("P1");
+        pw.flush();
+
+        /*
+        */
+        //Log.debug(this, "consuming any leftover data on the serial port");
+
+        /*
+        AvrDrone device = new AvrDrone(detectObitsPort());
         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 {
-            Thread.sleep(1000);
             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);
-                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");
+                if (count % 100 == 0) Log.info(AtmelSerial.class, "wrote " + count + " configuration octets");
             }
-            dos.flush();
+            device.flush();
             long end = System.currentTimeMillis();
-            System.err.println("finished in " + ((end-begin)/1000) + "s");
+            Log.info(AtmelSerial.class, "finished in " + ((end-begin)/1000) + "s");
             System.exit(0);
         } catch (Exception e) { e.printStackTrace(); }
+        */
+        System.exit(0);
     }
 
 }