cleanup
[eltron.git] / src / com / megacz / eltron / Eltron.java
diff --git a/src/com/megacz/eltron/Eltron.java b/src/com/megacz/eltron/Eltron.java
new file mode 100644 (file)
index 0000000..a7881a8
--- /dev/null
@@ -0,0 +1,144 @@
+package com.megacz.eltron;
+
+import org.ibex.util.*;
+import org.ibex.graphics.*;
+import java.io.*;
+import java.util.*;
+import gnu.io.*;
+
+public class Eltron {
+
+    public static SerialPort detectObitsPort() throws Exception {
+        Enumeration e = CommPortIdentifier.getPortIdentifiers();
+        while(e.hasMoreElements()) {
+            CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
+            Log.info(Eltron.class, "detected " + cpi.getName());
+            if (cpi.getName().indexOf("usbserial")!=-1) {
+                SerialPort ret = new RXTXPort(cpi.getName());
+                Log.info(Eltron.class, "returning " + ret);
+                return ret;
+            }
+        }
+        SerialPort ret = new RXTXPort("/dev/ttyS0");
+        Log.info(Eltron.class, "returning " + ret);
+        return ret;
+    }
+
+    public static void main(String[] s) throws Exception {
+        SerialPort sp = detectObitsPort();
+        //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
+
+        sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+        //sp.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+        //sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+        OutputStream out = sp.getOutputStream();
+        InputStream in = sp.getInputStream();
+        int count = 0;
+        PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
+        pw.println();
+        pw.flush();
+
+       /*
+       System.out.println("rebooting device...");
+        pw.println("^@");
+        pw.println("^@");
+        pw.println("^@");
+        pw.flush();
+       System.out.println("rebooted.");
+        try { Thread.sleep(3000); } catch (Exception e) { }
+       System.out.println("setting comm parameters...");
+        pw.println();
+        pw.flush();
+        pw.println();
+        pw.println("Y38,N,8,1");
+        pw.flush();
+       System.out.println("set.");
+        try { Thread.sleep(2000); } catch (Exception e) { }
+       System.exit(0);
+       */
+
+        pw.println("^@");
+        pw.println("^@");
+        pw.println("^@");
+        pw.flush();
+        try { Thread.sleep(2000); } catch (Exception e) { }
+
+        pw.println("OD");
+        pw.println("N");
+        pw.println("D14");
+        pw.println("S1");
+        //pw.println("Q609,24");
+        pw.println("q780");
+        //pw.println("R0,0");
+
+        //int MAXWIDTH = 200 * 4 - 10;
+        //int MAXHEIGHT = 200 * 6 - 10;
+        int MAXWIDTH = 200 * 5 - 10;
+        int MAXHEIGHT = 200 * 7 - 10;
+        Picture p = new Picture(System.in);
+        int[] data = p.data;
+        int owidth = ((int)Math.floor(Math.min(MAXWIDTH, p.height)/8.0)) * 8;   // FIXME: should be ceil
+        int oheight = Math.min(MAXHEIGHT, p.width);
+        int[] data2 = new int[owidth * oheight];
+        for(int y=0; y<owidth; y++)
+            for(int x=0; x<oheight; x++)
+                data2[y+x*owidth] = data[x+(owidth-y-1)*p.width];
+        data = data2;
+
+        for(int y=0; y<oheight; y++) {
+            pw.println("GW0,"+y+","+owidth/8+",1");
+            pw.flush();
+            DataOutputStream dout = new DataOutputStream(out);
+            System.out.println("y="+y);
+            for(int x=0; x<owidth; x+=8) {
+                byte b = 0;
+                for(int i=0; i<Math.min(8, owidth-x); i++) {
+                    int dat = data[y*owidth+x+i];
+                    dat &= 0x00ffffff;
+                    if (dat==0) continue;
+                    b |= 1 << (7-i);
+                }
+                dout.writeByte(b);
+            }
+            dout.flush();
+            pw.println();
+        }
+        pw.println();
+        pw.println();
+        pw.println();
+        pw.flush();
+
+
+        //pw.println("GI");
+        //pw.flush();
+
+
+        pw.println("P1");
+        pw.flush();
+
+        /*
+        */
+        //Log.debug(this, "consuming any leftover data on the serial port");
+
+        /*
+        AvrDrone device = new AvrDrone(detectObitsPort());
+        int count = 0;
+        try {
+            long begin = System.currentTimeMillis();
+            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+            for(String str = br.readLine(); str != null; str = br.readLine()) {
+                long foo = Long.parseLong(str, 16);
+                device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
+                count++;
+                if (count % 100 == 0) Log.info(Eltron.class, "wrote " + count + " configuration octets");
+            }
+            device.flush();
+            long end = System.currentTimeMillis();
+            Log.info(Eltron.class, "finished in " + ((end-begin)/1000) + "s");
+            System.exit(0);
+        } catch (Exception e) { e.printStackTrace(); }
+        */
+        System.exit(0);
+    }
+
+}