- final DataInputStream in;
-
- final DataOutputStream out;
-
- final SerialPort sp;
-
- public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
- this.sp = sp;
- //sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
- sp.setInputBufferSize(1024);
- //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
- this.out = new DataOutputStream(sp.getOutputStream());
- this.in = new DataInputStream(sp.getInputStream());
- Log.debug(this, "consuming any leftover data on the serial port");
- while(in.available() > 0) in.read();
- reset();
- Log.debug(this, "waiting for device to identify itself");
- if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
- if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
- if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
- if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
- if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
- if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
- Log.info(this, "device correctly identified itself; ready for operation");
+ private final DataInputStream in;
+ private final DataOutputStream out;
+ private final Board board;
+
+ public AvrDrone(Board b) throws IOException {
+ this.board = b;
+ this.out = new DataOutputStream(b.getOutputStream());
+ this.in = new DataInputStream(b.getInputStream());
+ init();
+ }
+
+ public void reset() throws DeviceException {
+ try {
+ board.reset();
+ } catch (IOException e) {
+ throw new DeviceException(e);
+ }
+ }
+
+ private void init() throws IOException {
+ byte[] bytes = new byte[6];
+ int i=0;
+
+ out.write(0);
+ out.flush();
+
+ // read any crap that might be left in the buffer
+ while(true) {
+ System.arraycopy(bytes, 1, bytes, 0, 5);
+ bytes[5] = in.readByte();
+ i++;
+ System.out.print("\rsignature: read \"" + new String(bytes) + "\" ");
+ if (bytes[0] == (byte)'O' &&
+ bytes[1] == (byte)'B' &&
+ bytes[2] == (byte)'I' &&
+ bytes[3] == (byte)'T' &&
+ bytes[4] == (byte)'S') {
+ System.out.println("\rsignature: got proper signature ");
+ break;
+ }
+ }
+