it works!
[slipway.git] / src / edu / berkeley / obits / device / atmel / AvrDrone.java
1 package edu.berkeley.obits.device.atmel;
2
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
5 import java.io.*;
6 import java.util.*;
7 import gnu.io.*;
8
9 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
10 public class AvrDrone extends AtmelDevice {
11
12     final DataInputStream in;
13
14     final DataOutputStream out;
15
16     final SerialPort sp;
17
18     public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
19         this.sp = sp;
20         sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
21         sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
22         //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
23         this.out = new DataOutputStream(sp.getOutputStream());
24         this.in = new DataInputStream(sp.getInputStream());
25         Log.debug(this, "consuming any leftover data on the serial port");
26         while(in.available() > 0) in.read();
27         reset();
28         Log.debug(this, "waiting for device to identify itself");
29         if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
30         if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
31         if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
32         if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
33         if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
34         if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
35         Log.info(this, "device correctly identified itself; ready for operation");
36     }
37
38     public void reset() throws DeviceException {
39         try {
40             Log.info(this, "resetting device");
41             sp.setDTR(true);
42             sp.setRTS(true);
43             Thread.sleep(500);
44             Log.info(this, "deasserting reset signal");
45             sp.setDTR(false);
46             sp.setRTS(false);
47             Thread.sleep(100);
48         } catch (InterruptedException e) { throw new DeviceException(e); }
49     }
50
51     public void mode4(int z, int y, int x, int d) throws DeviceException {
52         try {
53             Log.debug(this, "writing configuration frame [zyxd]: " +
54                       pad(2, Integer.toString(z, 16)) + " " +
55                       pad(2, Integer.toString(y, 16)) + " " +
56                       pad(2, Integer.toString(x, 16)) + " " +
57                       pad(2, Integer.toString(d, 16))
58                       );
59             out.writeByte(1);
60             out.writeByte(z);
61             out.writeByte(y);
62             out.writeByte(x);
63             out.writeByte(d);
64         } catch (IOException e) { throw new DeviceException(e); }
65     }
66
67     public void flush() throws DeviceException {
68         try {
69             out.flush();
70         } catch (IOException e) { throw new DeviceException(e); }
71     }
72
73     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
74
75 }