lejos.pc.comm
Class NXTCommand

java.lang.Object
  extended by lejos.pc.comm.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
 
Fields inherited from interface lejos.pc.comm.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()
           
 
Method Summary
 void addLogListener(NXTCommLogListener listener)
          register log listener
 void boot()
           
 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()
           
 byte delete(java.lang.String fileName)
          Delete a file on the NXT
 FileInfo findFirst(java.lang.String wildCard)
           
 FileInfo findNext(byte handle)
           
 int getBatteryLevel()
           
 java.lang.String getFriendlyName()
           
 InputValues getInputValues(int port)
           
 java.lang.String getLocalAddress()
           
 OutputState getOutputState(int port)
          Retrieves the current output state for a port.
static NXTCommand getSingleton()
           
 byte[] messageRead(byte remoteInbox, byte localInbox, boolean remove)
           
 boolean open(NXTInfo nxt)
          Open a connection to the NXT
 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[] readFile(byte handle, int length)
          Returns requested number of bytes from a file.
 void removeLogListener(NXTCommLogListener listener)
          unregister log listener
 NXTInfo[] search(java.lang.String name, int protocol)
          Search for NXTs
 byte setFriendlyName(java.lang.String name)
           
 void setNXTCommBlueTooth()
          Set the protocol to Bluetooth.
 void setVerify(boolean verify)
           
 byte startProgram(java.lang.String fileName)
          Starts a program already on the NXT.
 byte writeFile(byte handle, byte[] data)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NXTCommand

public NXTCommand()
Method Detail

search

public NXTInfo[] search(java.lang.String name,
                        int protocol)
                 throws NXTCommException
Search for NXTs

Parameters:
name - the name of the NXT or null, if any
protocol - USB, Bluetooth or either (uses NXTCommFactory constants)
Returns:
an array of NXTInfo objects representing all the NXTs found
Throws:
NXTCommException - if comms drivers cannot be loaded

setNXTCommBlueTooth

public void setNXTCommBlueTooth()
                         throws NXTCommException
Set the protocol to Bluetooth. Should now be unnecessary.

Throws:
NXTCommException - if the comms driver fails to load

open

public boolean open(NXTInfo nxt)
             throws NXTCommException
Open a connection to the NXT

Parameters:
nxt - the NXTInfo object returned by search or constructed
Returns:
true if the open succeeded, else false
Throws:
NXTCommException - if a comms driver could not be loaded

setVerify

public void setVerify(boolean verify)

startProgram

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

Parameters:
fileName -
Returns:
the status
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.rso"
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
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
Parameters:
handle - Handle number from the previous found file or fromthe Find First command.
Returns:
fileInfo object giving details of the file
Throws:
java.io.IOException

getBatteryLevel

public int getBatteryLevel()
                    throws java.io.IOException
Throws:
java.io.IOException

close

public void close()
           throws java.io.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:
java.io.IOException

boot

public void boot()
          throws java.io.IOException
Throws:
java.io.IOException

writeFile

public byte writeFile(byte handle,
                      byte[] data)
               throws java.io.IOException
Throws:
java.io.IOException

readFile

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

defrag

public byte defrag()
            throws java.io.IOException
Throws:
java.io.IOException

getFriendlyName

public java.lang.String getFriendlyName()
                                 throws java.io.IOException
Throws:
java.io.IOException

setFriendlyName

public byte setFriendlyName(java.lang.String name)
                     throws java.io.IOException
Throws:
java.io.IOException

getLocalAddress

public java.lang.String getLocalAddress()
                                 throws java.io.IOException
Throws:
java.io.IOException

getInputValues

public InputValues getInputValues(int port)
                           throws java.io.IOException
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

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

getSingleton

public static NXTCommand getSingleton()

addLogListener

public void addLogListener(NXTCommLogListener listener)
register log listener

Parameters:
listener -

removeLogListener

public void removeLogListener(NXTCommLogListener listener)
unregister log listener

Parameters:
listener -