1 package com.megacz.eltron;
3 import org.ibex.util.*;
4 import org.ibex.graphics.*;
9 /** code to control an Eltron */
12 public static void main(String[] s) throws Exception {
13 Picture p = new Picture(System.in);
14 print(p.data, p.width, p.height);
16 // RXTX spawns a thread that never dies
20 public static SerialPort detectPort() throws Exception {
21 if (System.getProperty("os.name").toLowerCase().indexOf("mac")==-1)
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");
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);
44 SerialPort ret = new RXTXPort("/dev/ttyUSB0");
45 Log.info(Eltron.class, "returning " + ret);
49 public static void print(int[] data, int width, int height) throws Exception {
50 SerialPort sp = detectPort();
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);
55 sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
57 OutputStream out = sp.getOutputStream();
58 InputStream in = sp.getInputStream();
60 PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
64 // uncomment this code the first time you run it (at 9600bps)
66 System.out.println("rebooting device...");
71 System.out.println("rebooted.");
72 try { Thread.sleep(3000); } catch (Exception e) { }
73 System.out.println("setting comm parameters...");
77 pw.println("Y38,N,8,1");
79 System.out.println("set.");
80 try { Thread.sleep(2000); } catch (Exception e) { }
88 try { Thread.sleep(2000); } catch (Exception e) { }
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];
106 for(int y=0; y<oheight; y++) {
107 pw.println("GW0,"+y+","+owidth/8+",1");
109 DataOutputStream dout = new DataOutputStream(out);
110 System.out.println("y="+y);
111 for(int x=0; x<owidth; x+=8) {
113 for(int i=0; i<Math.min(8, owidth-x); i++) {
114 int dat = data[y*owidth+x+i];
116 if (dat==0) continue;
131 System.out.println("done printing.");