checkpoint
[eltron.git] / src / edu / berkeley / cs / obits / AtmelSerial.java
1 package edu.berkeley.cs.obits;
2
3 import edu.berkeley.cs.obits.device.atmel.*;
4 import org.ibex.util.*;
5 import java.io.*;
6 import java.util.*;
7 import gnu.io.*;
8
9 public class AtmelSerial {
10
11     public static SerialPort detectObitsPort() throws Exception {
12         Enumeration e = CommPortIdentifier.getPortIdentifiers();
13         while(e.hasMoreElements()) {
14             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
15             Log.info(AtmelSerial.class, "trying " + cpi.getName());
16         }
17         SerialPort ret = new RXTXPort("/dev/cu.usbserial-FTBUODP4");
18         Log.info(AtmelSerial.class, "returning " + ret);
19         return ret;
20     }
21
22     public static void main(String[] s) throws Exception {
23         SerialPort sp = detectObitsPort();
24         sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
25         //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
26         OutputStream out = sp.getOutputStream();
27         InputStream in = sp.getInputStream();
28         int count = 0;
29         PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
30         /*
31         pw.println("Y38,N,8,1");
32         pw.flush();
33         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
34
35         pw.println("GI");
36         pw.flush();
37         */
38
39         pw.println();
40         pw.flush();
41         pw.println("^@");
42         pw.println("^@");
43         pw.println("^@");
44         pw.flush();
45         try { Thread.sleep(3000); } catch (Exception e) { }
46
47         pw.println("GK\"IMG\"");
48         pw.println("GK\"IMG\"");
49         pw.println();
50         pw.flush();
51         try { Thread.sleep(1000); } catch (Exception e) { }
52         /*
53         pw.println("GI");
54         pw.flush();
55         */
56         int[] data = new int[104 * 104];
57         for(int i=0; i<104*104; i++) data[i] = 1;
58         for(int i=0; i<104; i++) data[i*104+i] = 0;
59         for(int i=0; i<104; i++) data[i*104+(104-i)] = 0;
60         ByteArrayOutputStream baos = new ByteArrayOutputStream();
61         PCX.dump(104, 104, data, new DataOutputStream(baos));
62         byte[] outb = baos.toByteArray();
63         int len = outb.length;
64         pw.println("GM\"IMG\""+len);
65         pw.flush();
66         DataOutputStream dout = new DataOutputStream(out);
67         for(int i=0; i<len; i++) {
68             System.out.println("wrote " + i + "/"+outb.length);
69             dout.writeByte(outb[i]);
70             dout.flush();
71         }
72         dout.flush();
73
74         pw.println();
75         pw.println("GI");
76         pw.flush();
77
78         try { Thread.sleep(2000); } catch (Exception e) { }
79
80         pw.println();
81         pw.println("OD");
82         pw.println("N");
83         pw.println("D14");
84         pw.println("S1");
85         pw.println("Q609,24");
86         pw.println("q754");
87         //pw.println("R0,0");
88         pw.println("A170,5,0,1,5,5,N,\"WORLDWIDE\"");
89         pw.println("LO5,230,765,10");
90         pw.println("A10,265,0,1,3,3,R,\"MODEL:\"");
91         pw.println("A280,265,0,1,3,3,N,\"Bar Code Printer\"");
92         pw.println("A10,340,0,1,3,3,R,\"  CODE: \"");
93         pw.println("B280,340,0,3C,2,6,120,B,\"BCP-1234\"");
94         pw.println("LO5,520,765,10");
95         pw.println("A100,550,0,1,2,2,N,\"ISO9000     Made In USA\"");
96         pw.println("GG0,0,\"IMG2\"");
97         pw.println("P1");
98         pw.flush();
99
100         /*
101         */
102         //Log.debug(this, "consuming any leftover data on the serial port");
103
104         /*
105         AvrDrone device = new AvrDrone(detectObitsPort());
106         int count = 0;
107         try {
108             long begin = System.currentTimeMillis();
109             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
110             for(String str = br.readLine(); str != null; str = br.readLine()) {
111                 long foo = Long.parseLong(str, 16);
112                 device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
113                 count++;
114                 if (count % 100 == 0) Log.info(AtmelSerial.class, "wrote " + count + " configuration octets");
115             }
116             device.flush();
117             long end = System.currentTimeMillis();
118             Log.info(AtmelSerial.class, "finished in " + ((end-begin)/1000) + "s");
119             System.exit(0);
120         } catch (Exception e) { e.printStackTrace(); }
121         */
122         System.exit(0);
123     }
124
125 }