lejos.nxt.remote
Class NXTCommand

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

public class NXTCommand
extends 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(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(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
 String getCurrentProgramName()
          Name of current running program.
 String getDefaultProgram()
          leJOS-specific command to get the default program name
 DeviceInfo getDeviceInfo()
          Gets device information
 FirmwareInfo getFirmwareVersion()
          Get the fimrware version.
 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
 String getLocalAddress()
          Get the local address of the NXT.
 NXJFirmwareInfo getNXJFirmwareInfo()
          leJOS-specific command to get the NXJ firmware and menu information
 String getNXJFirmwareVersion()
          leJOS-specific command to get the NXJ firmware version
 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(String fileName)
          Opens a file on the NXT for reading.
 byte openWrite(String fileName, int size)
          Opens a file on the NXT for writing.
 byte playSoundFile(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(String name)
          leJOS-specific command to set the default program
 byte setFriendlyName(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(String fileName)
          Starts a program already on the NXT.
 byte stopProgram()
          Forces the currently executing program to stop.
 byte stopSoundPlayback()
          Stops sound file playing.
 String uploadFile(File file, 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(String fileName)
                  throws IOException
Starts a program already on the NXT.

Parameters:
fileName - the file name
Returns:
the status
Throws:
IOException

stopProgram

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

Returns:
Error value
Throws:
IOException

getCurrentProgramName

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

Returns:
the program name
Throws:
IOException

openRead

public FileInfo openRead(String fileName)
                  throws 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:
IOException

openWrite

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

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

closeFile

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

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

delete

public byte delete(String fileName)
            throws IOException
Delete a file on the NXT

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

findFirst

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

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

findNext

public FileInfo findNext(byte handle)
                  throws 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:
IOException

getBatteryLevel

public int getBatteryLevel()
                    throws IOException
Get the battery reading

Returns:
the battery level in millivolts
Throws:
IOException

close

@Deprecated
public void close()
           throws 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:
IOException

disconnect

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

Throws:
IOException

boot

public void boot()
          throws 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:
IOException

writeFile

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

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

uploadFile

public String uploadFile(File file,
                         String nxtFileName)
                  throws 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:
IOException

readFile

public int readFile(byte handle,
                    byte[] data,
                    int offset,
                    int length)
             throws 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:
IOException

defrag

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

Returns:
the status byte
Throws:
IOException

getFriendlyName

public String getFriendlyName()
                       throws IOException
Get the friendly name of the NXT

Returns:
the friendly name
Throws:
IOException

setFriendlyName

public byte setFriendlyName(String name)
                     throws IOException
Set the friendly name of the NXT

Parameters:
name - the friendly name
Returns:
the status byte
Throws:
IOException

getLocalAddress

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

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

getInputValues

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

Parameters:
port - the port number
Returns:
the InputValues structure
Throws:
IOException

getOutputState

public OutputState getOutputState(int port)
                           throws 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:
IOException

getTachoCount

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

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

setInputMode

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

LSGetStatus

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

LSRead

public byte[] LSRead(byte port)
              throws 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:
IOException

LSWrite

public byte LSWrite(byte port,
                    byte[] txData,
                    byte rxDataLength)
             throws 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:
IOException

messageRead

public byte[] messageRead(byte remoteInbox,
                          byte localInbox,
                          boolean remove)
                   throws 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:
IOException

messageWrite

public byte messageWrite(byte[] message,
                         byte inbox)
                  throws 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:
IOException

playTone

public byte playTone(int frequency,
                     int duration)
              throws 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:
IOException

playSoundFile

public byte playSoundFile(String fileName,
                          boolean repeat)
                   throws IOException
Throws:
IOException

stopSoundPlayback

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

Returns:
the status (0 = success)
Throws:
IOException

resetMotorPosition

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

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

setOutputState

public byte setOutputState(int port,
                           byte power,
                           int mode,
                           int regulationMode,
                           int turnRatio,
                           int runState,
                           int tachoLimit)
                    throws 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:
IOException

getDeviceInfo

public DeviceInfo getDeviceInfo()
                         throws IOException
Gets device information

Returns:
a DeviceInfo structure
Throws:
IOException

getFirmwareVersion

public FirmwareInfo getFirmwareVersion()
                                throws 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:
IOException

deleteUserFlash

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

Returns:
the status (0 = success)
Throws:
IOException

setDefaultProgram

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

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

setVolume

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

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

setKeyClickVolume

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

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

setAutoRun

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

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

getVolume

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

Returns:
the master volume level
Throws:
IOException

getKeyClickVolume

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

Returns:
the master volume level
Throws:
IOException

getAutoRun

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

Returns:
the auto run setting
Throws:
IOException

getNXJFirmwareVersion

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

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

getNXJMenuVersion

public String getNXJMenuVersion()
                         throws 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:
IOException

getNXJFirmwareInfo

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

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

getSleepTime

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

Returns:
the sleep time in seconds
Throws:
IOException

getDefaultProgram

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

Returns:
the default program name
Throws:
IOException

setSleepTime

public byte setSleepTime(byte seconds)
                  throws 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:
IOException

isOpen

public boolean isOpen()
Test is connection is open

Returns:
true iff the connection is open