a497c5a4126acdceeed51a790742c58a26cf3fc0
[eltron.git] / src / edu / berkeley / cs / obits / AtmelSerial.java
1 package edu.berkeley.cs.obits;
2
3 import java.io.*;
4 import java.util.*;
5 import gnu.io.*;
6
7 public class AtmelSerial {
8
9     public static SerialPort detectObitsPort() throws Exception {
10         Enumeration e = CommPortIdentifier.getPortIdentifiers();
11         while(e.hasMoreElements()) {
12             CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement();
13             System.err.println("trying " + cpi.getName());
14         }
15         return new RXTXPort("/dev/cu.usbserial-FTBUODP4");
16     }
17     public static void main(String[] s) throws Exception {
18         final SerialPort sp = detectObitsPort();
19         sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
20         sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
21         final OutputStream out = sp.getOutputStream();
22         final InputStream in = sp.getInputStream();
23         while(in.available() > 0) in.read();
24         sp.setDTR(true);
25         Thread.sleep(500);
26         sp.setDTR(false);
27         Thread.sleep(3000);
28         DataInputStream dis = new DataInputStream(in);
29         System.err.println("waiting...");
30         if (dis.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
31         if (dis.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
32         if (dis.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
33         if (dis.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
34         if (dis.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
35         if (dis.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
36         System.err.println("ready.");
37         int count = 0;
38         new Thread() {
39             public void run() {
40                 try {
41                     while(true) {
42                         System.err.println(sp.isDTR() + " " + sp.isDSR() + " " + sp.isRTS() + " " + sp.isCTS());
43                         Thread.sleep(250);
44                     }
45                 } catch (Exception e) {
46                     e.printStackTrace();
47                 }
48             } }.start();
49         new Thread() {
50             public void run() {
51                 try {
52                     while(true) {
53                         int i2 = in.read();
54                         if (i2==-1) { System.err.println("input closed"); System.exit(-1); }
55                         System.out.print((char)i2);
56                         System.out.flush();
57                     }
58                 } catch (Exception e) {
59                     e.printStackTrace();
60                 }
61             }
62         }.start();
63         try {
64             Thread.sleep(1000);
65             long begin = System.currentTimeMillis();
66             DataOutputStream dos = new DataOutputStream(out);
67             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
68             for(String str = br.readLine(); str != null; str = br.readLine()) {
69                 long foo = Long.parseLong(str, 16);
70                 dos.writeByte(1);
71                 dos.writeByte((int)(foo >> 24));
72                 dos.writeByte((int)(foo >> 16));
73                 dos.writeByte((int)(foo >>  8));
74                 dos.writeByte((int)(foo >>  0));
75                 count++;
76                 if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets");
77             }
78             dos.flush();
79             long end = System.currentTimeMillis();
80             System.err.println("finished in " + ((end-begin)/1000) + "s");
81             System.exit(0);
82         } catch (Exception e) { e.printStackTrace(); }
83     }
84
85 }