Class I2CSensor

    • Field Detail

      • REG_VERSION

        protected static final byte REG_VERSION
        Register number of sensor version string, as defined by standard Lego I2C register layout.
        See Also:
        getVersion(), Constant Field Values
      • REG_VENDOR_ID

        protected static final byte REG_VENDOR_ID
        Register number of sensor vendor ID, as defined by standard Lego I2C register layout.
        See Also:
        getVendorID(), Constant Field Values
      • REG_PRODUCT_ID

        protected static final byte REG_PRODUCT_ID
        Register number of sensor product ID, as defined by standard Lego I2C register layout.
        See Also:
        getProductID(), Constant Field Values
      • address

        protected int address
      • retryCount

        protected int retryCount
    • Constructor Detail

      • I2CSensor

        public I2CSensor​(I2CPort port,
                         int address)
        Create the sensor using an already open sensor port. Do not configure the hardware
        Parameters:
        port - the open port
        address - I2C address
      • I2CSensor

        public I2CSensor​(I2CPort port)
      • I2CSensor

        public I2CSensor​(Port port,
                         int address,
                         int type)
        Create the sensor using the specified port. Configure the hardware as required.
        Parameters:
        port - port the sensor is attached to
        address - I2C address
        type - type of I2C sensor
      • I2CSensor

        public I2CSensor​(Port port)
      • I2CSensor

        public I2CSensor​(Port port,
                         int address)
    • Method Detail

      • setRetryCount

        public void setRetryCount​(int newCount)
        Set the number of times that a get/send data request should be retried.
        Parameters:
        newCount - number of times to try the request
      • getRetryCount

        public int getRetryCount()
        Return the current get/send retry count value
        Returns:
        current retry count
      • getData

        public void getData​(int register,
                            byte[] buf,
                            int len)
        Executes an I2C read transaction and waits for the result.
        Parameters:
        register - I2C register, e.g 0x41
        buf - Buffer to return data
        len - Length of the return data
      • getData

        public void getData​(int register,
                            byte[] buf,
                            int offset,
                            int len)
        Executes an I2C read transaction and waits for the result.
        Parameters:
        register - I2C register, e.g 0x41
        buf - Buffer to return data
        offset - Offset of the start of the data
        len - Length of the return data
      • sendData

        public void sendData​(int register,
                             byte[] buf,
                             int len)
        Executes an I2C write transaction.
        Parameters:
        register - I2C register, e.g 0x42
        buf - Buffer containing data to send
        len - Length of data to send
      • sendData

        public void sendData​(int register,
                             byte[] buf,
                             int offset,
                             int len)
        Executes an I2C write transaction.
        Parameters:
        register - I2C register, e.g 0x42
        buf - Buffer containing data to send
        offset - Offset of the start of the data
        len - Length of data to send
      • sendData

        public void sendData​(int register,
                             byte value)
        Executes an I2C write transaction.
        Parameters:
        register - I2C register, e.g 0x42
        value - single byte to send
      • getVersion

        public java.lang.String getVersion()
        Read the sensor's version string. This method reads up to 8 bytes and returns the characters before the zero termination byte. Examples: "V1.0", ...
        Returns:
        version number
      • getVendorID

        public java.lang.String getVendorID()
        Read the sensor's vendor identifier. This method reads up to 8 bytes and returns the characters before the zero termination byte. Examples: "LEGO", "HiTechnc", ...
        Returns:
        vendor identifier
      • getProductID

        public java.lang.String getProductID()
        Read the sensor's product identifier. This method reads up to 8 bytes and returns the characters before the zero termination byte. Examples: "Sonar", ...
        Returns:
        product identifier
      • fetchString

        protected java.lang.String fetchString​(byte reg,
                                               int len)
        Read a string from the device. This functions reads the specified number of bytes and returns the characters before the zero termination byte.
        Parameters:
        reg -
        len - maximum length of the string, including the zero termination byte
        Returns:
        the string containing the characters before the zero termination byte
      • setAddress

        @Deprecated
        public void setAddress​(int addr)
        Deprecated.
        If the device has a changeable address, then constructor of the class should have an address parameter. If not, please report a bug.
        Set the address of the port Addresses use the standard Lego/NXT format and are in the range 0x2-0xfe. The low bit must always be zero. Some data sheets (and older versions of leJOS) may use i2c 7 bit format (0x1-0x7f) in which case this address must be shifted left one place to be used with this function.
        Parameters:
        addr - 0x02 to 0xfe
      • getAddress

        public int getAddress()
        Return the the I2C address of the sensor. The sensor uses the address for writing/reading.
        Returns:
        the I2C address.
      • getPort

        public I2CPort getPort()
        Get the port that the sensor is attached to
        Returns:
        the I2CPort