/** * Sets the pin mode to use on the pin. * * @param pin The ping to set. * @param pinMode The mode to set. 1 is OUTPUT, 0 is INPUT * @return A status code. * @throws IOException */ public int setPinMode(int pin, int pinMode) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } // if (pin == 10 || pin == 15 || pin == 0 || pin == 1) return writeI2c(Commands.PIN_MODE, pin, pinMode, Commands.UNUSED); }
/** * Executes a digital read on the pin. * * @param pin The pin to use for the reading. * @return The read bytes. * @throws IOException */ public int digitalRead(int pin) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } writeI2c(Commands.DIGITAL_READ, pin, Commands.UNUSED, Commands.UNUSED); return device.read() & 0xff; }
/** * Reads the number of bytes from the I2C device. * * @param numberOfBytes The length of the buffer to read. * @return The buffer. * @throws IOException */ public byte[] readI2c(int numberOfBytes) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } byte[] buffer = new byte[numberOfBytes]; device.read(1, buffer, 0, buffer.length); return buffer; }
/** * Executes a digital write on the pin. * * @param pin The ping to use for the writing. * @param value The value to write. * @return A status code. * @throws IOException */ public int digitalWrite(int pin, int value) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } // if (pin == 10 || pin == 0 || pin == 1 || pin == 5 || pin == 16 || pin == 17) if (value == 0 || value == 1) { return writeI2c(Commands.DIGITAL_WRITE, pin, value, Commands.UNUSED); } else { return Statuses.ERROR; } }
/** * Executes an analog write on the pin. * * @param pin The ping to use for the writing. * @param value The value to write. * @return A status code. * @throws IOException */ public int analogWrite(int pin, int value) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } if (pin == 10) { writeI2c(Commands.ANALOG_WRITE, pin, value, Commands.UNUSED); sleep(5); return Statuses.OK; } else { return -2; // TODO: Which kind of status is "-2"? } }
/** * Writes the bytes to the I2C device. * * @param bytes The buffer to write. * @return A status code. * @throws IOException */ public int writeI2c(int... bytes) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } // Convert array: int[] to byte[] final ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length); for (int i = 0, len = bytes.length; i < len; i++) { byteBuffer.put((byte) bytes[i]); } sleep( 100); // TODO: Is correct to have this here or we let the external calls to handle the // sleep? device.write(0xfe, byteBuffer.array(), 0, byteBuffer.limit()); return Statuses.OK; }
/** * Executes an analog read on the pin. * * @param pin The ping to use for the reading. * @return The read value. * @throws IOException */ public int analogRead(int pin) throws IOException { if (Gopigo.getInstance().isHalt()) { Gopigo.getInstance().onHalt(); } // if (pin == 1) { writeI2c(Commands.ANALOG_READ, pin, Commands.UNUSED, Commands.UNUSED); sleep(50); byte[] b1 = readI2c(1); byte[] b2 = readI2c(1); // Note: before was 2 int val1 = (int) b1[0] & 0xFF; int val2 = (int) b2[0] & 0xFF; return val1 * 256 + val2; /*} else { return -2; }*/ }
/** Initializes the board executing a writing test catching the error. */ public void init() { try { device.write(0xfe, (byte) 0x04); } catch (IOException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String exceptionDetails = sw.toString(); debug.log(Debug.SEVERE, exceptionDetails); Gopigo.getInstance().halt(); } }