08b4e488474bc8474ae7cf66f0213337ee908dfa
[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
18     public static class AvrDrone {
19         final DataInputStream in;
20         final DataOutputStream out;
21         final SerialPort sp;
22         public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException {
23             this.sp = sp;
24             sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
25             sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
26             this.out = new DataOutputStream(sp.getOutputStream());
27             this.in = new DataInputStream(sp.getInputStream());
28             while(in.available() > 0) in.read();
29             reset();
30             System.err.println("waiting...");
31             if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
32             if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
33             if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
34             if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
35             if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
36             if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
37             System.err.println("ready.");
38         }
39         public void reset() throws InterruptedException {
40             sp.setDTR(true);
41             Thread.sleep(500);
42             sp.setDTR(false);
43             Thread.sleep(3000);
44         }
45         public void mode4(int z, int y, int x, int d) throws IOException {
46             out.writeByte(1);
47             out.writeByte(z);
48             out.writeByte(y);
49             out.writeByte(x);
50             out.writeByte(d);
51         }
52         public void flush() throws IOException {
53             out.flush();
54         }
55     }
56
57
58     public static void main(String[] s) throws Exception {
59         AvrDrone device = new AvrDrone(detectObitsPort());
60         int count = 0;
61         try {
62             long begin = System.currentTimeMillis();
63             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
64             for(String str = br.readLine(); str != null; str = br.readLine()) {
65                 long foo = Long.parseLong(str, 16);
66                 device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
67                 count++;
68                 if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets");
69             }
70             device.flush();
71             long end = System.currentTimeMillis();
72             System.err.println("finished in " + ((end-begin)/1000) + "s");
73             System.exit(0);
74         } catch (Exception e) { e.printStackTrace(); }
75     }
76
77 }