package acroname; import java.io.*; public class Brainstem { /** The input stream */ public NonBlockingInputStream in; /** The output stream */ public OutputStream out; byte[] buf = new byte[32]; public static final int FAIL = -1024; public boolean time = false; long l; /** Make a Brainstem object. */ public Brainstem() { try { in = new NonBlockingInputStream(new FileInputStream("/dev/brainstem")); out = new FileOutputStream("/dev/brainstem"); } catch (IOException e) { throw new RuntimeException("couldn't open /dev/brainstem"); } } public int setServo(int brainstemid, int servo, int position) { try { if (time) l = System.currentTimeMillis(); byte[] packet = new byte[] { (byte)brainstemid, 3, 33, (byte)servo, (byte) position }; out.write(packet); if (time) System.out.println("Time: " + (System.currentTimeMillis() - l)); return 1; } catch (IOException e) { return FAIL; } } public int getA2D(int brainstemid, int sensor) { try { if (time) l = System.currentTimeMillis(); byte[] packet = new byte[] { (byte)brainstemid, 2, 25, (byte)(sensor+128) }; out.write(packet); int size =in.readNonBlocking(buf,1000); if (time) System.out.println("Time: " + (System.currentTimeMillis() - l)); if (size <= 0) return FAIL; else return 256 * (buf[size-2] < 0 ? buf[size-2] + 256 : buf[size-2] ) + (buf[size-1] < 0 ? buf[size-1] + 256 : buf[size-1] ); } catch (IOException e) { return FAIL; } } public int getDigital(int brainstemid, int sensor) { try { if (time) l = System.currentTimeMillis(); byte[] packet = new byte[] { (byte)brainstemid, 2, 27, (byte)(sensor+128) }; out.write(packet); int size =in.readNonBlocking(buf,1000); if (time) System.out.println("Time: " + (System.currentTimeMillis() - l)); if (size <= 0) return FAIL; else return buf[size-1]; } catch (IOException e) { return FAIL; } } public int getCharScratchpad(int brainstemid, int pad) { try { if (time) l = System.currentTimeMillis(); // if (pad > 31) throw an error! byte[] packet = new byte[] { (byte)brainstemid, 2, 51, (byte)pad }; out.write(packet); int size =in.readNonBlocking(buf,1000); if (time) System.out.println("Time: " + (System.currentTimeMillis() - l)); if (size <= 0) return FAIL; else return buf[size-1]; } catch (IOException e) { return FAIL; } } public int getIntScratchpad(int brainstemid, int pad) { int val0 = getCharScratchpad(brainstemid, pad); if (val0 == FAIL) return FAIL; int val1 = getCharScratchpad(brainstemid, pad+1); if (val1 == FAIL) return FAIL; byte b0 = (byte)val0; byte b1 = (byte)val1; return (short)((b0 << 8) + b1); } public int setCharScratchpad(int brainstemid, int pad, int value) { try { if (time) l = System.currentTimeMillis(); byte[] packet = new byte[] { (byte)brainstemid, 3, 51, (byte)pad, (byte)value}; // if (pad > 31) throw an error! out.write(packet); if (time) System.out.println("Time: " + (System.currentTimeMillis() - l)); return 1; } catch (IOException e) { return FAIL; } } public int setIntScratchpad(int brainstemid, int pad, int value) { short sval = (short)value; if (setCharScratchpad(brainstemid, pad, sval & 0xFF00) == FAIL) return FAIL; if (setCharScratchpad(brainstemid, pad+1, sval & 0x00FF) == FAIL) return FAIL; return 1; } }