Package lejos.remote.nxt
Class NXTCommand
- java.lang.Object
-
- lejos.remote.nxt.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 Modifier and Type Field Description static intMAX_FILENAMELENGTH-
Fields inherited from interface lejos.remote.nxt.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
Constructors Constructor Description NXTCommand(NXTCommRequest nxtComm)Create a NXTCommand object.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidboot()Put the NXT into SAMBA mode, ready to update the firmware.voidclose()Deprecated.call disconnect, then close the underlying NXTCommbytecloseFile(byte handle)Closes an open file.bytedefrag()A NXJ extension to defrag the file systembytedelete(java.lang.String fileName)Delete a file on the NXTbytedeleteUserFlash()Deletes user flash memory.voiddisconnect()Tell the NXT that the connection is aborted.FileInfofindFirst(java.lang.String wildCard)Find the first file on the NXT.FileInfofindNext(byte handle)Find the next file on the NXTbooleangetAutoRun()leJOS-specific command to get the auto run setringintgetBatteryLevel()Get the battery readingjava.lang.StringgetCurrentProgramName()Name of current running program.java.lang.StringgetDefaultProgram()leJOS-specific command to get the default program nameDeviceInfogetDeviceInfo()Gets device informationFirmwareInfogetFirmwareVersion()Get the fimrware version.java.lang.StringgetFriendlyName()Get the friendly name of the NXTInputValuesgetInputValues(int port)Get input values for a specific NXT sensor portintgetKeyClickVolume()leJOS-specific command to get the master volume leveljava.lang.StringgetLocalAddress()Get the local address of the NXT.NXJFirmwareInfogetNXJFirmwareInfo()leJOS-specific command to get the NXJ firmware and menu informationjava.lang.StringgetNXJFirmwareVersion()leJOS-specific command to get the NXJ firmware versionjava.lang.StringgetNXJMenuVersion()leJOS-specific command to get the NXJ start-up menu versionOutputStategetOutputState(int port)Retrieves the current output state for a port.intgetSleepTime()leJOS-specific method to get the menu sleep timeintgetTachoCount(int port)Retrieves tacho count.intgetVolume()leJOS-specific command to get the master volume levelbooleanisOpen()Test is connection is openbyte[]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.byteLSWrite(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)Read message.bytemessageWrite(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.FileInfoopenRead(java.lang.String fileName)Opens a file on the NXT for reading.byteopenWrite(java.lang.String fileName, int size)Opens a file on the NXT for writing.byteplaySoundFile(java.lang.String fileName, boolean repeat)byteplayTone(int frequency, int duration)Plays a tone on NXT speaker.intreadFile(byte handle, byte[] data, int offset, int length)Returns requested number of bytes from a file.byteresetMotorPosition(int port, boolean relative)Resets either RotationCount or BlockTachobytesetAutoRun(boolean on)leJOS-specific command to set auto-run on or offbytesetDefaultProgram(java.lang.String name)leJOS-specific command to set the default programbytesetFriendlyName(java.lang.String name)Set the friendly name of the NXTbytesetInputMode(int port, int sensorType, int sensorMode)Tells the NXT what type of sensor you are using and the mode to operate in.bytesetKeyClickVolume(byte volume)leJOS-specific command to set the key click volume levelbytesetOutputState(int port, byte power, int mode, int regulationMode, int turnRatio, int runState, int tachoLimit)bytesetSleepTime(byte seconds)leJOS-specific command to the the sleep time for the menuvoidsetVerify(boolean verify)Toggle the verify flag.bytesetVolume(byte volume)leJOS-specific command to set the master volume levelbytestartProgram(java.lang.String fileName)Starts a program already on the NXT.bytestopProgram()Forces the currently executing program to stop.bytestopSoundPlayback()Stops sound file playing.java.lang.StringuploadFile(java.io.File file, java.lang.String nxtFileName)Upload a file to the NXTbytewriteFile(byte handle, byte[] data, int offset, int length)Write data to the file
-
-
-
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.IOExceptionStarts 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.IOExceptionForces 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.IOExceptionName 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.IOExceptionOpens 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.IOExceptionCloses 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.IOExceptionDelete 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.IOExceptionGet the battery reading- Returns:
- the battery level in millivolts
- Throws:
java.io.IOException
-
close
@Deprecated public void close() throws java.io.IOExceptionDeprecated.call disconnect, then close the underlying NXTCommCall 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.IOExceptionTell the NXT that the connection is aborted.- Throws:
java.io.IOException
-
boot
public void boot() throws java.io.IOExceptionPut 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.IOExceptionWrite data to the file- Parameters:
handle- the file handledata- 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.IOExceptionUpload a file to the NXT- Parameters:
file- the file to uploadnxtFileName- 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.IOExceptionReturns 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 writtenoffset- Index of first byte to be overwrittenlength- Number of bytes to read- Returns:
- number of bytes read
- Throws:
java.io.IOException
-
defrag
public byte defrag() throws java.io.IOExceptionA NXJ extension to defrag the file system- Returns:
- the status byte
- Throws:
java.io.IOException
-
getFriendlyName
public java.lang.String getFriendlyName() throws java.io.IOExceptionGet 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.IOExceptionSet 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.IOExceptionGet 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.IOExceptionRetrieves 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.IOExceptionTells the NXT what type of sensor you are using and the mode to operate in.- Parameters:
port- - 0 to 3sensorType- - 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.IOExceptionReturns 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.IOExceptionReads 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.IOExceptionUsed 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.IOExceptionRead message.- Parameters:
remoteInbox- 0-9localInbox- 0-9remove- True clears the message from the remote inbox.- Returns:
- the message as an array of bytes, excluding the trailing null-terminator or null when queue is empty
- Throws:
java.io.IOException
-
messageWrite
public byte messageWrite(byte[] message, byte inbox) throws java.io.IOExceptionSends a message to an inbox on the NXT for storage(?) For future reference, message size must be capped at 59 for USB. A null terminator is automatically appended and should not be included in the message. UNTESTED- Parameters:
message- String to send.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.IOExceptionPlays 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.IOExceptionStops sound file playing.- Returns:
- the status (0 = success)
- Throws:
java.io.IOException
-
resetMotorPosition
public byte resetMotorPosition(int port, boolean relative) throws java.io.IOExceptionResets 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 + REGULATEDregulationMode- - see NXTProtocol for enumerationsturnRatio- - Need two motors? (-100 to 100)runState- - see NXTProtocol for enumerationstachoLimit- - 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.IOExceptionDeletes 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.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-specific command to get the master volume level- Returns:
- the master volume level
- Throws:
java.io.IOException
-
getKeyClickVolume
public int getKeyClickVolume() throws java.io.IOExceptionleJOS-specific command to get the master volume level- Returns:
- the master volume level
- Throws:
java.io.IOException
-
getAutoRun
public boolean getAutoRun() throws java.io.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-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.IOExceptionleJOS-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
-
-