Class 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.
    • 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
        Read message.
        Parameters:
        remoteInbox - 0-9
        localInbox - 0-9
        remove - 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.IOException
        Sends 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.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