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