ff030f4cc368cbe094fdcd6e9c4a0c3d49805f68
[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 /*
10
11 If you're on a mac and you get the error below, you need to create
12 /var/lock and make it writable.
13
14 Exception in thread "main" gnu.io.PortInUseException: No such file or directory in open
15 at gnu.io.RXTXPort.open(Native Method)
16 at gnu.io.RXTXPort.<init>(RXTXPort.java:84)
17 at com.megacz.eltron.Eltron.detectPort(Eltron.java:26)
18 at com.megacz.eltron.Eltron.print(Eltron.java:37)
19 at com.megacz.eltron.Eltron.main(Eltron.java:14)
20
21
22  */
23
24 /** code to control an Eltron */
25 public class Eltron {
26
27     public static void main(String[] s) throws Exception {
28         Picture p = new Picture(System.in);
29         print(p.data, p.width, p.height);
30     }
31
32     public static SerialPort detectPort() throws Exception {
33         Enumeration e = CommPortIdentifier.getPortIdentifiers();
34         while(e.hasMoreElements()) {
35             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
36             Log.info(Eltron.class, "detected " + cpi.getName());
37             if (cpi.getName().indexOf("usbserial")!=-1) {
38                 SerialPort ret = new RXTXPort(cpi.getName());
39                 Log.info(Eltron.class, "returning " + ret);
40                 return ret;
41             }
42         }
43         SerialPort ret = new RXTXPort("/dev/ttyS0");
44         Log.info(Eltron.class, "returning " + ret);
45         return ret;
46     }
47
48     public static void print(int[] data, int width, int height) throws Exception {
49         SerialPort sp = detectPort();
50
51         // you'll need to run this once at 9600 to tell the built-in eeprom to switch to 38,400kbps
52         //sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
53         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
54
55         OutputStream out = sp.getOutputStream();
56         InputStream in = sp.getInputStream();
57         int count = 0;
58         PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
59         pw.println();
60         pw.flush();
61
62         // uncomment this code the first time you run it (at 9600bps)
63         /*
64         System.out.println("rebooting device...");
65         pw.println("^@");
66         pw.println("^@");
67         pw.println("^@");
68         pw.flush();
69         System.out.println("rebooted.");
70         try { Thread.sleep(3000); } catch (Exception e) { }
71         System.out.println("setting comm parameters...");
72         pw.println();
73         pw.flush();
74         pw.println();
75         pw.println("Y38,N,8,1");
76         pw.flush();
77         System.out.println("set.");
78         try { Thread.sleep(2000); } catch (Exception e) { }
79         System.exit(0);
80         */
81
82         pw.println("^@");
83         pw.println("^@");
84         pw.println("^@");
85         pw.flush();
86         try { Thread.sleep(2000); } catch (Exception e) { }
87
88         pw.println("OD");
89         pw.println("N");
90         pw.println("D14");
91         pw.println("S1");
92         pw.println("q780");
93
94         int MAXWIDTH = 200 * 5 - 10;
95         int MAXHEIGHT = 200 * 7 - 10;
96         int owidth = ((int)Math.floor(Math.min(MAXWIDTH, height)/8.0)) * 8;   // FIXME: should be ceil
97         int oheight = Math.min(MAXHEIGHT, width);
98         int[] data2 = new int[owidth * oheight];
99         for(int y=0; y<owidth; y++)
100             for(int x=0; x<oheight; x++)
101                 data2[y+x*owidth] = data[x+(owidth-y-1)*width];
102         data = data2;
103
104         for(int y=0; y<oheight; y++) {
105             pw.println("GW0,"+y+","+owidth/8+",1");
106             pw.flush();
107             DataOutputStream dout = new DataOutputStream(out);
108             System.out.println("y="+y);
109             for(int x=0; x<owidth; x+=8) {
110                 byte b = 0;
111                 for(int i=0; i<Math.min(8, owidth-x); i++) {
112                     int dat = data[y*owidth+x+i];
113                     dat &= 0x00ffffff;
114                     if (dat==0) continue;
115                     b |= 1 << (7-i);
116                 }
117                 dout.writeByte(b);
118             }
119             dout.flush();
120             pw.println();
121         }
122         pw.println();
123         pw.println();
124         pw.println();
125         pw.flush();
126
127         pw.println("P1");
128         pw.flush();
129     }
130
131 }