a7881a81072961f4618c3af87df0ac8809fb65c0
[eltron.git] / src / com / megacz / eltron / Eltron.java
1 package com.megacz.eltron;
2
3 import org.ibex.util.*;
4 import org.ibex.graphics.*;
5 import java.io.*;
6 import java.util.*;
7 import gnu.io.*;
8
9 public class Eltron {
10
11     public static SerialPort detectObitsPort() throws Exception {
12         Enumeration e = CommPortIdentifier.getPortIdentifiers();
13         while(e.hasMoreElements()) {
14             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
15             Log.info(Eltron.class, "detected " + cpi.getName());
16             if (cpi.getName().indexOf("usbserial")!=-1) {
17                 SerialPort ret = new RXTXPort(cpi.getName());
18                 Log.info(Eltron.class, "returning " + ret);
19                 return ret;
20             }
21         }
22         SerialPort ret = new RXTXPort("/dev/ttyS0");
23         Log.info(Eltron.class, "returning " + ret);
24         return ret;
25     }
26
27     public static void main(String[] s) throws Exception {
28         SerialPort sp = detectObitsPort();
29         //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
30
31         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
32         //sp.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
33         //sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
34         OutputStream out = sp.getOutputStream();
35         InputStream in = sp.getInputStream();
36         int count = 0;
37         PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
38         pw.println();
39         pw.flush();
40
41         /*
42         System.out.println("rebooting device...");
43         pw.println("^@");
44         pw.println("^@");
45         pw.println("^@");
46         pw.flush();
47         System.out.println("rebooted.");
48         try { Thread.sleep(3000); } catch (Exception e) { }
49         System.out.println("setting comm parameters...");
50         pw.println();
51         pw.flush();
52         pw.println();
53         pw.println("Y38,N,8,1");
54         pw.flush();
55         System.out.println("set.");
56         try { Thread.sleep(2000); } catch (Exception e) { }
57         System.exit(0);
58         */
59
60         pw.println("^@");
61         pw.println("^@");
62         pw.println("^@");
63         pw.flush();
64         try { Thread.sleep(2000); } catch (Exception e) { }
65
66         pw.println("OD");
67         pw.println("N");
68         pw.println("D14");
69         pw.println("S1");
70         //pw.println("Q609,24");
71         pw.println("q780");
72         //pw.println("R0,0");
73
74         //int MAXWIDTH = 200 * 4 - 10;
75         //int MAXHEIGHT = 200 * 6 - 10;
76         int MAXWIDTH = 200 * 5 - 10;
77         int MAXHEIGHT = 200 * 7 - 10;
78         Picture p = new Picture(System.in);
79         int[] data = p.data;
80         int owidth = ((int)Math.floor(Math.min(MAXWIDTH, p.height)/8.0)) * 8;   // FIXME: should be ceil
81         int oheight = Math.min(MAXHEIGHT, p.width);
82         int[] data2 = new int[owidth * oheight];
83         for(int y=0; y<owidth; y++)
84             for(int x=0; x<oheight; x++)
85                 data2[y+x*owidth] = data[x+(owidth-y-1)*p.width];
86         data = data2;
87
88         for(int y=0; y<oheight; y++) {
89             pw.println("GW0,"+y+","+owidth/8+",1");
90             pw.flush();
91             DataOutputStream dout = new DataOutputStream(out);
92             System.out.println("y="+y);
93             for(int x=0; x<owidth; x+=8) {
94                 byte b = 0;
95                 for(int i=0; i<Math.min(8, owidth-x); i++) {
96                     int dat = data[y*owidth+x+i];
97                     dat &= 0x00ffffff;
98                     if (dat==0) continue;
99                     b |= 1 << (7-i);
100                 }
101                 dout.writeByte(b);
102             }
103             dout.flush();
104             pw.println();
105         }
106         pw.println();
107         pw.println();
108         pw.println();
109         pw.flush();
110
111
112         //pw.println("GI");
113         //pw.flush();
114
115
116         pw.println("P1");
117         pw.flush();
118
119         /*
120         */
121         //Log.debug(this, "consuming any leftover data on the serial port");
122
123         /*
124         AvrDrone device = new AvrDrone(detectObitsPort());
125         int count = 0;
126         try {
127             long begin = System.currentTimeMillis();
128             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
129             for(String str = br.readLine(); str != null; str = br.readLine()) {
130                 long foo = Long.parseLong(str, 16);
131                 device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
132                 count++;
133                 if (count % 100 == 0) Log.info(Eltron.class, "wrote " + count + " configuration octets");
134             }
135             device.flush();
136             long end = System.currentTimeMillis();
137             Log.info(Eltron.class, "finished in " + ((end-begin)/1000) + "s");
138             System.exit(0);
139         } catch (Exception e) { e.printStackTrace(); }
140         */
141         System.exit(0);
142     }
143
144 }