X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fobits%2Fdevice%2Fatmel%2FAvrDrone.java;h=6776b8b4125be20c932555e7c686c4e26e8a9c1a;hb=423974291c90fbba82ed586499e01c774ee96ae0;hp=35581da255690726ee1ea0c075c573af2932e781;hpb=69ab82574d3ae842369e0c7e0cb837e48f4b4660;p=slipway.git diff --git a/src/edu/berkeley/obits/device/atmel/AvrDrone.java b/src/edu/berkeley/obits/device/atmel/AvrDrone.java index 35581da..6776b8b 100644 --- a/src/edu/berkeley/obits/device/atmel/AvrDrone.java +++ b/src/edu/berkeley/obits/device/atmel/AvrDrone.java @@ -1,5 +1,6 @@ package edu.berkeley.obits.device.atmel; +import edu.berkeley.slipway.*; import edu.berkeley.obits.*; import org.ibex.util.Log; import java.io.*; @@ -9,30 +10,42 @@ import gnu.io.*; /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */ public class AvrDrone extends AtmelDevice { - final DataInputStream in; + private final DataInputStream in; + private final DataOutputStream out; + private final Board board; - final DataOutputStream out; + public AvrDrone(Board b) throws IOException { + this.board = b; + this.out = new DataOutputStream(b.getOutputStream()); + this.in = new DataInputStream(b.getInputStream()); + init(); + } - final SerialPort sp; + public void reset() throws DeviceException { + try { + board.reset(); + } catch (IOException e) { + throw new DeviceException(e); + } + } - 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.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(); + private void init() throws IOException { 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"); + */ + out.write(0); + byte[] bytes = new byte[6]; + for(int i=0; i<6; i++) { + bytes[i] = in.readByte(); + System.out.println("got " + (i+1) + " header bytes: " + (bytes[i] & 0xff) + " '" + ((char)bytes[i]) + "'"); + // FIXME + } Log.info(this, "device correctly identified itself; ready for operation"); } @@ -46,6 +59,33 @@ public class AvrDrone extends AtmelDevice { } } catch (IOException e) { throw new DeviceException(e); } } + // fixme! + public static int retval = 0; + public synchronized int readCount() throws DeviceException { + try { + if (reader != null) { + reader.start(); + reader = null; + } + ByteCallback bc = new ByteCallback() { + public synchronized void call(byte b) throws Exception { + retval = + ((b & 0xff) << 24) | + ((in.read() & 0xff) << 16) | + ((in.read() & 0xff) << 8) | + ((in.read() & 0xff) << 0); + this.notify(); + } + }; + synchronized(bc) { + callbacks.add(bc); + out.writeByte(6); + out.flush(); + bc.wait(); + } + return retval; + } catch (Exception e) { throw new DeviceException(e); } + } public static interface ByteCallback { public void call(byte b) throws Exception; @@ -55,6 +95,7 @@ public class AvrDrone extends AtmelDevice { private Thread reader = new Thread() { public void run() { + System.out.println("*** reader thread begun"); while(true) { try { byte b = in.readByte(); @@ -69,7 +110,6 @@ public class AvrDrone extends AtmelDevice { public synchronized void readBus(ByteCallback bc) throws DeviceException { try { - System.out.println("capacity: " + callbacks.size()); callbacks.add(bc); out.writeByte(2); out.flush(); @@ -80,26 +120,31 @@ public class AvrDrone extends AtmelDevice { } catch (IOException e) { throw new DeviceException(e); } } - public synchronized void reset() throws DeviceException { + public synchronized void readInterrupts(ByteCallback bc) throws DeviceException { try { - Log.info(this, "resetting device"); - sp.setDTR(true); - sp.setRTS(true); - Thread.sleep(500); - Log.info(this, "deasserting reset signal"); - sp.setDTR(false); - sp.setRTS(false); - Thread.sleep(100); - } catch (InterruptedException e) { throw new DeviceException(e); } + callbacks.add(bc); + out.writeByte(6); + out.flush(); + if (reader != null) { + reader.start(); + reader = null; + } + } catch (IOException e) { throw new DeviceException(e); } } private byte[][][] cache = new byte[24][][]; - public synchronized byte mode4(int z, int y, int x) throws DeviceException { + public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException { if (cache[x]==null) return 0; if (cache[x][y]==null) return 0; return cache[x][y][z]; } - public synchronized void mode4(int z, int y, int x, int d) throws DeviceException { + + int lastz = 0; + int lastx = 0; + int lasty = 0; + public static int save = 0; + public static int saveof = 0; + public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException { try { /* Log.info(this, "writing configuration frame [zyxd]: " + @@ -109,18 +154,33 @@ public class AvrDrone extends AtmelDevice { pad(1, Integer.toString(d&0xff, 16)) ); */ + boolean zchange = z!=lastz; + boolean ychange = y!=lasty; + boolean xchange = x!=lastx; + boolean zinc = z==lastz+1; + boolean yinc = y==lasty+1; + boolean xinc = x==lastx+1; + boolean zdec = z==lastz-1; + boolean ydec = y==lasty-1; + boolean xdec = x==lastx-1; + out.writeByte(1); out.writeByte(z); out.writeByte(y); out.writeByte(x); + saveof++; + lastz = z; + lastx = x; + lasty = y; out.writeByte(d); + if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][]; if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256]; cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff); } catch (IOException e) { throw new DeviceException(e); } } - public synchronized void flush() throws DeviceException { + public /*synchronized*/ void flush() throws DeviceException { try { out.flush(); } catch (IOException e) { throw new DeviceException(e); }