lejos.nxt.remote
Class NXTCommand

java.lang.Object
  extended by lejos.nxt.remote.NXTCommand
All Implemented Interfaces:
NXTProtocol

public class NXTCommand
extends java.lang.Object
implements NXTProtocol

Sends LCP requests to the NXT and receives replies. Uses an object that implements the NXTComm interface for low-level communication.


Field Summary
static int MAX_FILENAMELENGTH
           
 
Fields inherited from interface lejos.nxt.remote.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, NXJ_DEFRAG, NXJ_DISCONNECT, NXJ_GET_AUTO_RUN, NXJ_GET_DEFAULT_PROGRAM, NXJ_GET_KEY_CLICK_VOLUME, NXJ_GET_SLEEP_TIME, NXJ_GET_VERSION, NXJ_GET_VOLUME, NXJ_PACKET_MODE, NXJ_SET_AUTO_RUN, NXJ_SET_DEFAULT_PROGRAM, NXJ_SET_KEY_CLICK_VOLUME, NXJ_SET_SLEEP_TIME, NXJ_SET_VOLUME, 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
 
Constructor Summary
NXTCommand(NXTCommRequest nxtComm)
          Create a NXTCommand object.
 
Method Summary
 void boot()
          Put the NXT into SAMBA mode, ready to update the firmware.
 void close()
          Deprecated. call disconnect, then close the underlying NXTComm
 byte closeFile(byte handle)
          Closes an open file.
 byte defrag()
          A NXJ extension to defrag the file system
 byte delete(java.lang.String fileName)
          Delete a file on the NXT
 byte deleteUserFlash()
          Deletes user flash memory.
 void disconnect()
          Tell the NXT that the connection is aborted.
 FileInfo findFirst(java.lang.String wildCard)
          Find the first file on the NXT.
 FileInfo findNext(byte handle)
          Find the next file on the NXT
 boolean getAutoRun()
          leJOS-specific command to get the auto run setring
 int getBatteryLevel()
          Get the battery reading
 java.lang.String getCurrentProgramName()
          Name of current running program.
 java.lang.String getDefaultProgram()
          leJOS-specific command to get the default program name
 DeviceInfo getDeviceInfo()
          Gets device information
 FirmwareInfo getFirmwareVersion()
          Get the fimrware version.
 java.lang.String getFriendlyName()
          Get the friendly name of the NXT
 InputValues getInputValues(int port)
          Get input values for a specific NXT sensor port
 int getKeyClickVolume()
          leJOS-specific command to get the master volume level
 java.lang.String getLocalAddress()
          Get the local address of the NXT.
 NXJFirmwareInfo getNXJFirmwareInfo()
          leJOS-specific command to get the NXJ firmware and menu information
 java.lang.String getNXJFirmwareVersion()
          leJOS-specific command to get the NXJ firmware version
 java.lang.String getNXJMenuVersion()
          leJOS-specific command to get the NXJ start-up menu version
 OutputState getOutputState(int port)
          Retrieves the current output state for a port.
 int getSleepTime()
          leJOS-specific method to get the menu sleep time
 int getTachoCount(int port)
          Retrieves tacho count.
 int getVolume()
          leJOS-specific command to get the master volume level
 boolean isOpen()
          Test is connection is open
 byte[] LSGetStatus(byte port)
          Returns the status for an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port.
 byte[] LSRead(byte port)
          Reads data from an Inter-Integrated Circuit (I2C) sensor (the ultrasound sensor) via the Low Speed (LS) data port.
 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.
 byte[] messageRead(byte remoteInbox, byte localInbox, boolean remove)
           
 byte messageWrite(byte[] 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.
 FileInfo openRead(java.lang.String fileName)
          Opens a file on the NXT for reading.
 byte openWrite(java.lang.String fileName, int size)
          Opens a file on the NXT for writing.
 byte playSoundFile(java.lang.String fileName, boolean repeat)
           
 byte playTone(int frequency, int duration)
          Plays a tone on NXT speaker.
 int readFile(byte handle, byte[] data, int offset, int length)
          Returns requested number of bytes from a file.
 byte resetMotorPosition(int port, boolean relative)
          Resets either RotationCount or BlockTacho
 byte setAutoRun(boolean on)
          leJOS-specific command to set auto-run on or off
 byte setDefaultProgram(java.lang.String name)
          leJOS-specific command to set the default program
 byte setFriendlyName(java.lang.String name)
          Set the friendly name of the NXT
 byte setInputMode(int port, int sensorType, int sensorMode)
          Tells the NXT what type of sensor you are using and the mode to operate in.
 byte setKeyClickVolume(byte volume)
          leJOS-specific command to set the key click volume level
 byte setOutputState(int port, byte power, int mode, int regulationMode, int turnRatio, int runState, int tachoLimit)
           
 byte setSleepTime(byte seconds)
          leJOS-specific command to the the sleep time for the menu
 void setVerify(boolean verify)
          Toggle the verify flag.
 byte setVolume(byte volume)
          leJOS-specific command to set the master volume level
 byte startProgram(java.lang.String fileName)
          Starts a program already on the NXT.
 byte stopProgram()
          Forces the currently executing program to stop.
 byte stopSoundPlayback()
          Stops sound file playing.
 java.lang.String uploadFile(java.io.File file, java.lang.String nxtFileName)
          Upload a file to the NXT
 byte writeFile(byte handle, byte[] data, int offset, int length)
          Write data to the file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_FILENAMELENGTH

public static final int MAX_FILENAMELENGTH
See Also:
Constant Field Values
Constructor Detail

NXTCommand

public NXTCommand(NXTCommRequest nxtComm)
Create a NXTCommand object.

Method Detail

setVerify

public void setVerify(boolean verify)
Toggle the verify flag.

Parameters:
verify - true causes all commands to return a response.

startProgram

public byte startProgram(java.lang.String fileName)
                  throws java.io.IOException
Starts a program already on the NXT.

Parameters:
fileName - the file name
Returns:
the status
Throws:
java.io.IOException

stopProgram

public byte stopProgram()
                 throws java.io.IOException
Forces the currently executing program to stop. Not implemented by leJOS NXJ.

Returns:
Error value
Throws:
java.io.IOException

getCurrentProgramName

public java.lang.String getCurrentProgramName()
                                       throws java.io.IOException
Name of current running program. Does not work with leJOS NXJ.

Returns:
the program name
Throws:
java.io.IOException

openRead

public FileInfo openRead(java.lang.String fileName)
                  throws java.io.IOException
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.wav"
Returns:
fileInfo object giving details of the file
Throws:
java.io.IOException

openWrite

public byte openWrite(java.lang.String fileName,
                      int size)
               throws java.io.IOException
Opens a file on the NXT for writing.

Parameters:
fileName - e.g. "Woops.wav"
Returns:
File Handle number
Throws:
java.io.IOException

closeFile

public byte closeFile(byte handle)
               throws java.io.IOException
Closes an open file.

Parameters:
handle - File handle number.
Returns:
Error code 0 = success
Throws:
java.io.IOException

delete

public byte delete(java.lang.String fileName)
            throws java.io.IOException
Delete a file on the NXT

Parameters:
fileName - the name of the file
Returns:
the error code 0 = success
Throws:
java.io.IOException

findFirst

public FileInfo findFirst(java.lang.String wildCard)
                   throws java.io.IOException
Find the first file on the NXT.

Parameters:
wildCard - [filename].[extension], *.[extension], [filename].*, *.*
Returns:
fileInfo object giving details of the file
Throws:
java.io.IOException

findNext

public FileInfo findNext(byte handle)
                  throws java.io.IOException
Find the next file on the NXT

Parameters:
handle - Handle number from the previous found file or from the Find First command.
Returns:
fileInfo object giving details of the file
Throws:
java.io.IOException

getBatteryLevel

public int getBatteryLevel()
                    throws java.io.IOException
Get the battery reading

Returns:
the battery level in millivolts
Throws:
java.io.IOException

close

@Deprecated
public void close()
           throws java.io.IOException
Deprecated. call disconnect, then close the underlying NXTComm

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

Throws:
java.io.IOException

disconnect

public void disconnect()
                throws java.io.IOException
Tell the NXT that the connection is aborted.

Throws:
java.io.IOException

boot

public void boot()
          throws java.io.IOException
Put the NXT into SAMBA mode, ready to update the firmware. Marks this NXTCommand object as closed. Does never never wait for a reply from the NXT.

Throws:
java.io.IOException

writeFile

public byte writeFile(byte handle,
                      byte[] data,
                      int offset,
                      int length)
               throws java.io.IOException
Write data to the file

Parameters:
handle - the file handle
data - the data to write
Returns:
the status value
Throws:
java.io.IOException

uploadFile

public java.lang.String uploadFile(java.io.File file,
                                   java.lang.String nxtFileName)
                            throws java.io.IOException
Upload a file to the NXT

Parameters:
file - the file to upload
nxtFileName - the name of the file on the NXT
Returns:
a message saying how long it took to upload the file
Throws:
java.io.IOException

readFile

public int readFile(byte handle,
                    byte[] data,
                    int offset,
                    int length)
             throws java.io.IOException
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)
data - Buffer to which data is written
offset - Index of first byte to be overwritten
length - Number of bytes to read
Returns:
number of bytes read
Throws:
java.io.IOException

defrag

public byte defrag()
            throws java.io.IOException
A NXJ extension to defrag the file system

Returns:
the status byte
Throws:
java.io.IOException

getFriendlyName

public java.lang.String getFriendlyName()
                                 throws java.io.IOException
Get the friendly name of the NXT

Returns:
the friendly name
Throws:
java.io.IOException

setFriendlyName

public byte setFriendlyName(java.lang.String name)
                     throws java.io.IOException
Set the friendly name of the NXT

Parameters:
name - the friendly name
Returns:
the status byte
Throws:
java.io.IOException

getLocalAddress

public java.lang.String getLocalAddress()
                                 throws java.io.IOException
Get the local address of the NXT.

Returns:
the address (used by USB and Bluetooth)
Throws:
java.io.IOException

getInputValues

public InputValues getInputValues(int port)
                           throws java.io.IOException
Get input values for a specific NXT sensor port

Parameters:
port - the port number
Returns:
the InputValues structure
Throws:
java.io.IOException

getOutputState

public OutputState getOutputState(int port)
                           throws java.io.IOException
Retrieves the current output state for a port.

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

getTachoCount

public int getTachoCount(int port)
                  throws java.io.IOException
Retrieves tacho count.

Parameters:
port - - 0 to 3
Returns:
tacho count
Throws:
java.io.IOException

setInputMode

public byte setInputMode(int port,
                         int sensorType,
                         int sensorMode)
                  throws java.io.IOException
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)
Throws:
java.io.IOException

LSGetStatus

public byte[] LSGetStatus(byte port)
                   throws java.io.IOException
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)
Throws:
java.io.IOException

LSRead

public byte[] LSRead(byte port)
              throws java.io.IOException
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:
the response
Throws:
java.io.IOException

LSWrite

public byte LSWrite(byte port,
                    byte[] txData,
                    byte rxDataLength)
             throws java.io.IOException
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:
the status (0 = success)
Throws:
java.io.IOException

messageRead

public byte[] messageRead(byte remoteInbox,
                          byte localInbox,
                          boolean remove)
                   throws java.io.IOException
Parameters:
remoteInbox - 0-9
localInbox - 0-9
remove - True clears the message from the remote inbox.
Returns:
the message as an array of bytes
Throws:
java.io.IOException

messageWrite

public byte messageWrite(byte[] message,
                         byte inbox)
                  throws java.io.IOException
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:
the status (0 = success)
Throws:
java.io.IOException

playTone

public byte playTone(int frequency,
                     int duration)
              throws java.io.IOException
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.
Returns:
- Returns true if command worked, false if it failed.
Throws:
java.io.IOException

playSoundFile

public byte playSoundFile(java.lang.String fileName,
                          boolean repeat)
                   throws java.io.IOException
Throws:
java.io.IOException

stopSoundPlayback

public byte stopSoundPlayback()
                       throws java.io.IOException
Stops sound file playing.

Returns:
the status (0 = success)
Throws:
java.io.IOException

resetMotorPosition

public byte resetMotorPosition(int port,
                               boolean relative)
                        throws java.io.IOException
Resets either RotationCount or BlockTacho

Parameters:
port - Output port (0-2)
relative - TRUE: BlockTacho, FALSE: RotationCount
Returns:
the status (0 = success)
Throws:
java.io.IOException

setOutputState

public byte setOutputState(int port,
                           byte power,
                           int mode,
                           int regulationMode,
                           int turnRatio,
                           int runState,
                           int tachoLimit)
                    throws java.io.IOException
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.
Returns:
the status (0 = success)
Throws:
java.io.IOException

getDeviceInfo

public DeviceInfo getDeviceInfo()
                         throws java.io.IOException
Gets device information

Returns:
a DeviceInfo structure
Throws:
java.io.IOException

getFirmwareVersion

public FirmwareInfo getFirmwareVersion()
                                throws java.io.IOException
Get the fimrware version. leJOS NXJ returns the version of the LEGO firmware that it emulates, not its own version number.

Returns:
a FirmwareInfo structure.
Throws:
java.io.IOException

deleteUserFlash

public byte deleteUserFlash()
                     throws java.io.IOException
Deletes user flash memory. Not implemented by leJOS NXJ.

Returns:
the status (0 = success)
Throws:
java.io.IOException

setDefaultProgram

public byte setDefaultProgram(java.lang.String name)
                       throws java.io.IOException
leJOS-specific command to set the default program

Parameters:
name - the default program name
Returns:
the status (0 is success)
Throws:
java.io.IOException

setVolume

public byte setVolume(byte volume)
               throws java.io.IOException
leJOS-specific command to set the master volume level

Parameters:
volume - the master volume level
Returns:
the status (0 = success)
Throws:
java.io.IOException

setKeyClickVolume

public byte setKeyClickVolume(byte volume)
                       throws java.io.IOException
leJOS-specific command to set the key click volume level

Parameters:
volume - the key click volume level
Returns:
the status (0 = success)
Throws:
java.io.IOException

setAutoRun

public byte setAutoRun(boolean on)
                throws java.io.IOException
leJOS-specific command to set auto-run on or off

Parameters:
on - true = on, false = off
Returns:
the status (0 = success)
Throws:
java.io.IOException

getVolume

public int getVolume()
              throws java.io.IOException
leJOS-specific command to get the master volume level

Returns:
the master volume level
Throws:
java.io.IOException

getKeyClickVolume

public int getKeyClickVolume()
                      throws java.io.IOException
leJOS-specific command to get the master volume level

Returns:
the master volume level
Throws:
java.io.IOException

getAutoRun

public boolean getAutoRun()
                   throws java.io.IOException
leJOS-specific command to get the auto run setring

Returns:
the auto run setting
Throws:
java.io.IOException

getNXJFirmwareVersion

public java.lang.String getNXJFirmwareVersion()
                                       throws java.io.IOException
leJOS-specific command to get the NXJ firmware version

Returns:
a string with major version, minor version, and patch level and revision
Throws:
java.io.IOException

getNXJMenuVersion

public java.lang.String getNXJMenuVersion()
                                   throws java.io.IOException
leJOS-specific command to get the NXJ start-up menu version

Returns:
a string with major version, minor version, patch level and revision
Throws:
java.io.IOException

getNXJFirmwareInfo

public NXJFirmwareInfo getNXJFirmwareInfo()
                                   throws java.io.IOException
leJOS-specific command to get the NXJ firmware and menu information

Returns:
a NXJFirmwareInfo object containing all the version numbers
Throws:
java.io.IOException

getSleepTime

public int getSleepTime()
                 throws java.io.IOException
leJOS-specific method to get the menu sleep time

Returns:
the sleep time in seconds
Throws:
java.io.IOException

getDefaultProgram

public java.lang.String getDefaultProgram()
                                   throws java.io.IOException
leJOS-specific command to get the default program name

Returns:
the default program name
Throws:
java.io.IOException

setSleepTime

public byte setSleepTime(byte seconds)
                  throws java.io.IOException
leJOS-specific command to the the sleep time for the menu

Parameters:
seconds - the number of seconds before shutdown
Returns:
the status (0 = success)
Throws:
java.io.IOException

isOpen

public boolean isOpen()
Test is connection is open

Returns:
true iff the connection is open