d59e73c6725bcb2187d250924d5328b0c497d639
[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         // RXTX spawns a thread that never dies
32         System.exit(0);
33     }
34
35     public static SerialPort detectPort() throws Exception {
36         Enumeration e = CommPortIdentifier.getPortIdentifiers();
37         while(e.hasMoreElements()) {
38             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
39             Log.info(Eltron.class, "detected " + cpi.getName());
40             if (cpi.getName().indexOf("usbserial")!=-1) {
41                 SerialPort ret = new RXTXPort(cpi.getName());
42                 Log.info(Eltron.class, "returning " + ret);
43                 return ret;
44             }
45         }
46         SerialPort ret = new RXTXPort("/dev/ttyS0");
47         Log.info(Eltron.class, "returning " + ret);
48         return ret;
49     }
50
51     public static void print(int[] data, int width, int height) throws Exception {
52         SerialPort sp = detectPort();
53
54         // you'll need to run this once at 9600 to tell the built-in eeprom to switch to 38,400kbps
55         //sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
56
57
58         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
59
60
61         OutputStream out = sp.getOutputStream();
62         InputStream in = sp.getInputStream();
63         int count = 0;
64         PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
65         pw.println();
66         pw.flush();
67
68         // uncomment this code the first time you run it (at 9600bps)
69         /*
70         System.out.println("rebooting device...");
71         pw.println("^@");
72         pw.println("^@");
73         pw.println("^@");
74         pw.flush();
75         System.out.println("rebooted.");
76         try { Thread.sleep(3000); } catch (Exception e) { }
77         System.out.println("setting comm parameters...");
78         pw.println();
79         pw.flush();
80         pw.println();
81         pw.println("Y38,N,8,1");
82         pw.flush();
83         System.out.println("set.");
84         try { Thread.sleep(2000); } catch (Exception e) { }
85         System.exit(0);
86         */
87
88         pw.println("^@");
89         pw.println("^@");
90         pw.println("^@");
91         pw.flush();
92         try { Thread.sleep(2000); } catch (Exception e) { }
93
94         pw.println("OD");
95         pw.println("N");
96         pw.println("D14");
97         pw.println("S1");
98         pw.println("q780");
99
100         int MAXWIDTH = 200 * 5 - 10;
101         int MAXHEIGHT = 200 * 7 - 10;
102         int owidth = ((int)Math.floor(Math.min(MAXWIDTH, height)/8.0)) * 8;   // FIXME: should be ceil
103         int oheight = Math.min(MAXHEIGHT, width);
104         int[] data2 = new int[owidth * oheight];
105         for(int y=0; y<owidth; y++)
106             for(int x=0; x<oheight; x++)
107                 data2[y+x*owidth] = data[x+(owidth-y-1)*width];
108         data = data2;
109
110         for(int y=0; y<oheight; y++) {
111             pw.println("GW0,"+y+","+owidth/8+",1");
112             pw.flush();
113             DataOutputStream dout = new DataOutputStream(out);
114             System.out.println("y="+y);
115             for(int x=0; x<owidth; x+=8) {
116                 byte b = 0;
117                 for(int i=0; i<Math.min(8, owidth-x); i++) {
118                     int dat = data[y*owidth+x+i];
119                     dat &= 0x00ffffff;
120                     if (dat==0) continue;
121                     b |= 1 << (7-i);
122                 }
123                 dout.writeByte(b);
124             }
125             dout.flush();
126             pw.println();
127         }
128         pw.println();
129         pw.println();
130         pw.println();
131         pw.flush();
132
133         pw.println("P1");
134         pw.flush();
135     }
136
137 }