1 package com.megacz.eltron;
3 import org.ibex.util.*;
4 import org.ibex.graphics.*;
11 If you're on a mac and you get the error below, you need to create
12 /var/lock and make it writable.
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)
24 /** code to control an Eltron */
27 public static void main(String[] s) throws Exception {
28 Picture p = new Picture(System.in);
29 print(p.data, p.width, p.height);
31 // RXTX spawns a thread that never dies
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);
46 SerialPort ret = new RXTXPort("/dev/ttyS0");
47 Log.info(Eltron.class, "returning " + ret);
51 public static void print(int[] data, int width, int height) throws Exception {
52 SerialPort sp = detectPort();
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);
58 sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
61 OutputStream out = sp.getOutputStream();
62 InputStream in = sp.getInputStream();
64 PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
68 // uncomment this code the first time you run it (at 9600bps)
70 System.out.println("rebooting device...");
75 System.out.println("rebooted.");
76 try { Thread.sleep(3000); } catch (Exception e) { }
77 System.out.println("setting comm parameters...");
81 pw.println("Y38,N,8,1");
83 System.out.println("set.");
84 try { Thread.sleep(2000); } catch (Exception e) { }
92 try { Thread.sleep(2000); } catch (Exception e) { }
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];
110 for(int y=0; y<oheight; y++) {
111 pw.println("GW0,"+y+","+owidth/8+",1");
113 DataOutputStream dout = new DataOutputStream(out);
114 System.out.println("y="+y);
115 for(int x=0; x<owidth; x+=8) {
117 for(int i=0; i<Math.min(8, owidth-x); i++) {
118 int dat = data[y*owidth+x+i];
120 if (dat==0) continue;