/** * Method for retrieving data values from the sensor. BYTE0 ( is usually the primary data value * for the sensor. Data is read from registers in the sensor, usually starting at 0x00 and ending * around 0x49. Just supply the register to start reading at, and the length of bytes to read (16 * maximum). NOTE: The NXT supplies UBYTE (unsigned byte) values but Java converts them into * signed bytes (probably more practical to return short/int?) * * @param register e.g. FACTORY_SCALE_DIVISOR, BYTE0, etc.... * @param length Length of data to read (minimum 1, maximum 16) * @return the status */ public int getData(int register, byte[] buf, int length) { byte[] txData = {address, (byte) register}; try { nxtCommand.LSWrite(port, txData, (byte) length); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } byte[] status = null; do { try { status = nxtCommand.LSGetStatus(port); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } } while (status[0] == ErrorMessages.PENDING_COMMUNICATION_TRANSACTION_IN_PROGRESS | status[0] == ErrorMessages.SPECIFIED_CHANNEL_CONNECTION_NOT_CONFIGURED_OR_BUSY); try { byte[] ret = nxtCommand.LSRead(port); if (ret != null) System.arraycopy(ret, 0, buf, 0, ret.length); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } return status[0]; }
/** @param s A sensor. e.g. Port.S1 */ public I2CSensor(I2CPort s, byte sensorType) { port = (byte) s.getId(); s.setTypeAndMode(sensorType, NXTProtocol.RAWMODE); // Flushes out any existing data try { nxtCommand.LSGetStatus(this.port); nxtCommand.LSRead(this.port); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } }
/** * Stops a sound file that has been playing/repeating. * * @return Error code. */ public static int stopSoundPlayback() { try { return nxtCommand.stopSoundPlayback(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } }
/** * Plays a sound file from the NXT. SoundSensor files use the .rso extension. The filename is not * case sensitive. Filenames on the NXT Bricks display do now show the filename extension. * * @param fileName e.g. "Woops.rso" * @param repeat true = repeat, false = play once. * @return If you receive a non-zero number, the filename is probably wrong or the file is not * uploaded to the NXT brick. */ public static byte playSoundFile(String fileName, boolean repeat) { try { return nxtCommand.playSoundFile(fileName, repeat); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } }
public static int playTone(int frequency, int duration) { try { return nxtCommand.playTone(frequency, duration); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } }
/** * Sets a single byte in the I2C sensor. * * @param register A data register in the I2C sensor. e.g. ACTUAL_ZERO * @param value The data value. */ public int sendData(int register, byte value) { byte[] txData = {address, (byte) register, value}; try { int ret = nxtCommand.LSWrite(this.port, txData, (byte) 0); return ret; } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } }
/** * Send data top the sensor * * @param register A data register in the I2C sensor. * @param data The byte to send. * @param length the number of bytes */ public int sendData(int register, byte[] data, int length) { byte[] txData = {address, (byte) register}; byte[] sendData = new byte[length + 2]; System.arraycopy(txData, 0, sendData, 0, 2); System.arraycopy(data, 0, sendData, 2, length); try { return nxtCommand.LSWrite(this.port, sendData, (byte) 0); } catch (IOException ioe) { System.out.println(ioe.getMessage()); return -1; } }