icommand.nxtcomm
Class NXTCommand

java.lang.Object
  extended by icommand.nxtcomm.NXTCommand
All Implemented Interfaces:
NXTProtocol

public class NXTCommand
extends java.lang.Object
implements NXTProtocol

NXTCommand contains easily accessible commands for the Lego NXT.

Version:
0.3 23-August-2006
Author:
Brian Bagnall

Field Summary
 
Fields inherited from interface icommand.nxtcomm.NXTProtocol
ANGLE, ANGLESTEPSMODE, BOOLEANMODE, BOOT, BRAKE, CELSIUSMODE, CLOSE, CUSTOM, DELETE, DELETE_USER_FLASH, DIRECT_COMMAND_NOREPLY, DIRECT_COMMAND_REPLY, FAHRENHEITMODE, FIND_FIRST, FIND_NEXT, GET_BATTERY_LEVEL, GET_CURRENT_PROGRAM_NAME, GET_DEVICE_INFO, GET_FIRMWARE_VERSION, GET_INPUT_VALUES, GET_OUTPUT_STATE, HIGH_SPEED_BUFFER, KEEP_ALIVE, LIGHT_ACTIVE, LIGHT_INACTIVE, LOWSPEED, LOWSPEED_9V, LS_GET_STATUS, LS_READ, LS_WRITE, MESSAGE_READ, MESSAGE_WRITE, MODEMASK, MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPDOWN, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTORON, NO_OF_SENSOR_TYPES, NO_SENSOR, OPEN_APPEND_DATA, OPEN_READ, OPEN_READ_LINEAR, OPEN_WRITE, OPEN_WRITE_DATA, OPEN_WRITE_LINEAR, PCTFULLSCALEMODE, PERIODCOUNTERMODE, PLAY_SOUND_FILE, PLAY_TONE, POLL, POLL_BUFFER, POLL_LENGTH, RAWMODE, READ, REFLECTION, REGULATED, REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC, REPLY_COMMAND, RESET_MOTOR_POSITION, RESET_SCALED_INPUT_VALUE, SET_BRICK_NAME, SET_INPUT_MODE, SET_OUTPUT_STATE, SLOPEMASK, SOUND_DB, SOUND_DBA, START_PROGRAM, STOP_PROGRAM, STOP_SOUND_PLAYBACK, SWITCH, SYSTEM_COMMAND_NOREPLY, SYSTEM_COMMAND_REPLY, TEMPERATURE, TRANSITIONCNTMODE, WRITE
 
Method Summary
static void close()
          Call the close() command when your program ends, otherwise you will have to turn the NXT brick off/on before you run another program using iCommand.
static byte closeFile(byte handle)
          Closes an open file.
static byte delete(java.lang.String fileName)
           
static byte deleteUserFlash()
          Deletes user flash memory (not including system modules).
static FileInfo findFirst(java.lang.String wildCard)
          When no files exist within the system, an error message is returned in the package saying "File not found".
static FileInfo findNext(byte handle)
          When no files exist within the system, an error message is returned in the package saying "File not found".
static int getBatteryLevel()
           
static java.lang.String getCurrentProgramName()
          Name of current running program.
static DeviceInfo getDeviceInfo()
           
static FirmwareInfo getFirmwareVersion()
           
static InputValues getInputValues(int port)
           
static OutputState getOutputState(int port)
          Retrieves the current output state for a port.
static boolean isVerify()
           
static long keepAlive()
          Keeps the NXT from shutting off.
static byte[] LSGetStatus(byte port)
          Returns the status for an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port.
static byte[] LSRead(byte port)
          Reads data from an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port.
static byte LSWrite(byte port, byte[] txData, byte rxDataLength)
          Used to request data from an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port.
static java.lang.String messageRead(byte remoteInbox, byte localInbox, boolean remove)
          UNTESTED
static byte messageWrite(java.lang.String message, byte inbox)
          Sends a message to an inbox on the NXT for storage(?) For future reference, message size must be capped at 59 for USB.
static FileInfo openRead(java.lang.String fileName)
          Opens a file on the NXT for reading.
static byte openWrite(java.lang.String fileName, int size)
          Opens a file on the NXT for writing.
static byte playSoundFile(java.lang.String fileName, boolean repeat)
           
static byte playTone(int frequency, int duration)
          Plays a tone on NXT speaker.
static byte[] poll(byte bufferNumber, byte commandLength)
          Reads bytes from the low-speed or high-speed buffer.
static byte pollLength(byte bufferNumber)
          Returns the number of bytes for a command in the low-speed buffer or the high-speed buffer (0 = no command is ready).
static byte[] readFile(byte handle, int length)
          Returns requested number of bytes from a file.
static byte resetMotorPosition(int port, boolean relative)
          Resets the tachometer?
static byte resetScaledInputValue(int port)
          UNTESTED
static byte setBrickName(java.lang.String name)
           
static byte setInputMode(int port, int sensorType, int sensorMode)
          Tells the NXT what type of sensor you are using and the mode to operate in.
static byte setOutputState(int port, byte power, int mode, int regulationMode, int turnRatio, int runState, int tachoLimit)
           
static void setVerify(boolean verify)
           
static byte startProgram(java.lang.String fileName)
          Starts a program already on the NXT.
static byte stopProgram()
          Forces the currently executing program to stop.
static byte stopSoundPlayback()
          Stops sound file playing.
static byte writeFile(byte handle, byte[] data)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

startProgram

public static byte startProgram(java.lang.String fileName)
Starts a program already on the NXT. UNTESTED

Parameters:
fileName -
Returns:

stopProgram

public static byte stopProgram()
Forces the currently executing program to stop. UNTESTED

Returns:
Error value

getCurrentProgramName

public static java.lang.String getCurrentProgramName()
Name of current running program. !! Leaves null character at end. UNTESTED

Returns:

openRead

public static FileInfo openRead(java.lang.String fileName)
Opens a file on the NXT for reading. Returns a handle number and file size, enclosed in a FileInfo object.

Parameters:
fileName - e.g. "Woops.rso"
Returns:

openWrite

public static byte openWrite(java.lang.String fileName,
                             int size)
Opens a file on the NXT for writing. UNFINISHED UNTESTED

Parameters:
fileName - e.g. "Woops.rso"
Returns:
File Handle number

readFile

public static byte[] readFile(byte handle,
                              int length)
Returns requested number of bytes from a file. File must first be opened using the openRead() command.

Parameters:
handle - File handle number (from openRead method)
length - Number of bytes to read.
Returns:

writeFile

public static byte writeFile(byte handle,
                             byte[] data)

closeFile

public static byte closeFile(byte handle)
Closes an open file.

Parameters:
handle - File handle number.
Returns:
Error code 0 = success

delete

public static byte delete(java.lang.String fileName)

getFirmwareVersion

public static FirmwareInfo getFirmwareVersion()

setBrickName

public static byte setBrickName(java.lang.String name)

getDeviceInfo

public static DeviceInfo getDeviceInfo()

deleteUserFlash

public static byte deleteUserFlash()
Deletes user flash memory (not including system modules). UNTESTED

Returns:

pollLength

public static byte pollLength(byte bufferNumber)
Returns the number of bytes for a command in the low-speed buffer or the high-speed buffer (0 = no command is ready). UNTESTED

Parameters:
bufferNumber - 0 = poll buffer (low-speed) 1 = high-speed buffer
Returns:

poll

public static byte[] poll(byte bufferNumber,
                          byte commandLength)
Reads bytes from the low-speed or high-speed buffer. UNTESTED

Parameters:
bufferNumber - 0 = poll buffer (low-speed) 1 = high-speed buffer
commandLength - Number of bytes obtained from pollLength()
Returns:

findFirst

public static FileInfo findFirst(java.lang.String wildCard)
When no files exist within the system, an error message is returned in the package saying "File not found". When this command returns a success, a close command is required for "closing the handle" within the brick when handle is not needed anymore. If an error is returned, the firmware will close the handle automatically.

Parameters:
wildCard - [filename].[extension], *.[extension], [filename].*, *.*
Returns:

findNext

public static FileInfo findNext(byte handle)
When no files exist within the system, an error message is returned in the package saying "File not found". When this command returns a success, a close command is required for "closing the handle" within the brick when handle is not needed anymore. If an error is returned, the firmware will close the handle automatically.

Parameters:
handle - Handle number from the previous found file or fromthe Find First command.
Returns:

playSoundFile

public static byte playSoundFile(java.lang.String fileName,
                                 boolean repeat)

stopSoundPlayback

public static byte stopSoundPlayback()
Stops sound file playing.

Returns:

setOutputState

public static byte setOutputState(int port,
                                  byte power,
                                  int mode,
                                  int regulationMode,
                                  int turnRatio,
                                  int runState,
                                  int tachoLimit)
Parameters:
port - - Output port (0 - 2 or 0xFF for all three)
power - - Setpoint for power. (-100 to 100)
mode - - Setting the modes MOTORON, BRAKE, and/or REGULATED. This parameter is a bitfield, so to put it in brake mode and regulated, use BRAKEMODE + REGULATED
regulationMode - - see NXTProtocol for enumerations
turnRatio - - Need two motors? (-100 to 100)
runState - - see NXTProtocol for enumerations
tachoLimit - - Number of degrees(?) to rotate before stopping.

setInputMode

public static byte setInputMode(int port,
                                int sensorType,
                                int sensorMode)
Tells the NXT what type of sensor you are using and the mode to operate in.

Parameters:
port - - 0 to 3
sensorType - - Enumeration for sensor type (see NXTProtocol)
sensorMode - - Enumeration for sensor mode (see NXTProtocol)

getOutputState

public static OutputState getOutputState(int port)
Retrieves the current output state for a port.

Parameters:
port - - 0 to 3
Returns:
OutputState - returns a container object for output state variables.

getInputValues

public static InputValues getInputValues(int port)

resetScaledInputValue

public static byte resetScaledInputValue(int port)
UNTESTED

Parameters:
port -
Returns:

messageWrite

public static byte messageWrite(java.lang.String message,
                                byte inbox)
Sends a message to an inbox on the NXT for storage(?) For future reference, message size must be capped at 59 for USB. UNTESTED

Parameters:
message - String to send. A null termination is automatically appended.
inbox - Inbox Number 0 - 9
Returns:

messageRead

public static java.lang.String messageRead(byte remoteInbox,
                                           byte localInbox,
                                           boolean remove)
UNTESTED

Parameters:
remoteInbox - 0-9
localInbox - 0-9
remove - True clears the message from the remote inbox.
Returns:

resetMotorPosition

public static byte resetMotorPosition(int port,
                                      boolean relative)
Resets the tachometer?

Parameters:
port - Output port (0-2)
relative - TRUE: position relative to last movement, FALSE: absolute position

playTone

public static byte playTone(int frequency,
                            int duration)
Plays a tone on NXT speaker. If a new tone is sent while the previous tone is playing, the new tone command will stop the old tone command.

Parameters:
frequency - - 100 to 2000?
duration - - In milliseconds.
verifyCommand - - A boolean value to indicate you want to know if the command worked.
Returns:
- Returns true if command worked, false if it failed.

getBatteryLevel

public static int getBatteryLevel()

keepAlive

public static long keepAlive()
Keeps the NXT from shutting off. NOTE: Normal Bluetooth commands do not keep the NXT alive. It will power off even if you have been regularly sending commands. Must use keepAlive() UNTESTED

Returns:
The current sleep time limit, in milliseconds.

LSGetStatus

public static byte[] LSGetStatus(byte port)
Returns the status for an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port. The port must first be configured to type LOWSPEED or LOWSPEED_9V.

Parameters:
port - 0-3
Returns:
byte[0] = status, byte[1] = Bytes Ready (count of available bytes to read)

LSWrite

public static byte LSWrite(byte port,
                           byte[] txData,
                           byte rxDataLength)
Used to request data from an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port. The port must first be configured to type LOWSPEED or LOWSPEED_9V. Data lengths are limited to 16 bytes per command. Rx (receive) Data Length MUST be specified in the write command since reading from the device is done on a master-slave basis.

Parameters:
txData - Transmitted data.
rxDataLength - Receive data length.
port - 0-3
Returns:

LSRead

public static byte[] LSRead(byte port)
Reads data from an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port. The port must first be configured to type LOWSPEED or LOWSPEED_9V. Data lengths are limited to 16 bytes per command. The response will also contain 16 bytes, with invalid data padded with zeros.

Parameters:
port -
Returns:

setVerify

public static void setVerify(boolean verify)

isVerify

public static boolean isVerify()

close

public static void close()
Call the close() command when your program ends, otherwise you will have to turn the NXT brick off/on before you run another program using iCommand.