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
 
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_FIND_FIRST, NXJ_FIND_NEXT, NXJ_PACKET_MODE, 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()
          Create a NXTCommand object.
 
Method Summary
 void boot()
          Put the NXT into SAMBA mode, ready to update the firmware
 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.
 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.
 FileInfo findFirst(String wildCard)
          Find the first file on the NXT.
 FileInfo findFirstNXJ(String wildCard)
          Find the first file on the NXT.
 FileInfo findNext(byte handle)
          Find the next file on the NXT
 FileInfo findNextNXJ(byte handle)
          Find the next file on the NXT.
 int getBatteryLevel()
          Get the battery reading
 String getCurrentProgramName()
          Name of current running program.
 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
 String getLocalAddress()
          Get the local address of the NXT.
 OutputState getOutputState(int port)
          Retrieves the current output state for a port.
static NXTCommand getSingleton()
          Get the singleton NXTCommand object.
 int getTachoCount(int port)
          Retrieves tacho count.
 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.
 byte[] readFile(byte handle, int length)
          Returns requested number of bytes from a file.
 byte resetMotorPosition(int port, boolean relative)
          Resets either RotationCount or BlockTacho
 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.
 void setNXTComm(NXTCommRequest nxtComm)
          Set the NXTComm used to communicate with the NXT.
 byte setOutputState(int port, byte power, int mode, int regulationMode, int turnRatio, int runState, int tachoLimit)
           
 void setVerify(boolean verify)
          Toggle the verify flag.
 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)
          Write data to the file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NXTCommand

public NXTCommand()
Create a NXTCommand object.

Method Detail

setNXTComm

public void setNXTComm(NXTCommRequest nxtComm)
Set the NXTComm used to communicate with the NXT.

Parameters:
nxtComm - a nxtComm instance which must be connected to a NXT

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

findFirstNXJ

public FileInfo findFirstNXJ(String wildCard)
                      throws IOException
Find the first file on the NXT. This is a NXJ-specific version that returns the start page number as well as the other FileInfo data

Parameters:
wildCard - [filename].[extension], *.[extension], [filename].*, *.*
Returns:
fileInfo object giving details of the file
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

findNextNXJ

public FileInfo findNextNXJ(byte handle)
                     throws IOException
Find the next file on the NXT. This is a NXJ-specific version that returns the start page number as well as the other FileInfo data

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

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

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

boot

public void boot()
          throws IOException
Put the NXT into SAMBA mode, ready to update the firmware

Throws:
IOException

writeFile

public byte writeFile(byte handle,
                      byte[] data)
               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 byte[] readFile(byte handle,
                       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)
length - Number of bytes to read.
Returns:
the bytes requested
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

getSingleton

public static NXTCommand getSingleton()
Get the singleton NXTCommand object. Use of this is optional.

Returns:
the singleton NXTCommand instance

isOpen

public boolean isOpen()
Test is connection is open

Returns:
true iff the connection is open