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