1 package edu.berkeley.cs.obits;
7 public class AtmelSerial {
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());
15 return new RXTXPort("/dev/cu.usbserial-FTBUODP4");
18 public static class AvrDrone {
19 final DataInputStream in;
20 final DataOutputStream out;
22 public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException {
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();
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.");
39 public void reset() throws InterruptedException {
45 public void mode4(int z, int y, int x, int d) throws IOException {
52 public void flush() throws IOException {
58 public static void main(String[] s) throws Exception {
59 AvrDrone device = new AvrDrone(detectObitsPort());
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));
68 if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets");
71 long end = System.currentTimeMillis();
72 System.err.println("finished in " + ((end-begin)/1000) + "s");
74 } catch (Exception e) { e.printStackTrace(); }