lejos.nxt
Class I2CSensor

java.lang.Object
  extended by lejos.nxt.I2CSensor
All Implemented Interfaces:
SensorConstants
Direct Known Subclasses:
AccelHTSensor, AccelMindSensor, AngleSensor, ColorHTSensor, CompassHTSensor, CompassMindSensor, CruizcoreGyro, DIMUAccel, DIMUGyro, GPSSensor, IRLink, IRSeeker, IRSeekerV2, LMotor, LSC, MSC, MServo, NXTCam, NXTe, NXTLineLeader, NXTMMX, OpticalDistanceSensor, PFLink, PFMate, PSPNXController, RCXLink, RCXMotorMultiplexer, RCXSensorMultiplexer, RFIDSensor, SensorMux, UltrasonicSensor

public class I2CSensor
extends java.lang.Object
implements SensorConstants

A sensor wrapper to allow easy access to I2C sensors, like the ultrasonic sensor. This version of this class supports remote execution of I2C.

Author:
Brian Bagnall and Lawrie Griffiths

Field Summary
protected  int address
           
protected static int DEFAULT_I2C_ADDRESS
           
protected  byte port
           
protected static byte REG_PRODUCT_ID
          Register number of sensor product ID, as defined by standard Lego I2C register layout.
protected static byte REG_VENDOR_ID
          Register number of sensor vendor ID, as defined by standard Lego I2C register layout.
protected static byte REG_VERSION
          Register number of sensor version string, as defined by standard Lego I2C register layout.
 
Fields inherited from interface lejos.nxt.SensorConstants
BLACK, BLANK_INDEX, BLUE, BLUE_INDEX, GREEN, GREEN_INDEX, MAX_AD_RAW, MAX_TYPE, MIN_TYPE, MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, RED, RED_INDEX, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE, WHITE, YELLOW
 
Constructor Summary
I2CSensor(I2CPort port)
           
I2CSensor(I2CPort port, int mode)
           
I2CSensor(I2CPort port, int address, int mode, int type)
           
 
Method Summary
protected  java.lang.String fetchString(byte reg, int len)
          Read a string from the device.
 int getAddress()
          Return the the I2C address of the sensor.
 int getData(int register)
          Helper method to return a single register byte.
 int getData(int register, byte[] buf, int length)
           
 int getData(int register, byte[] buf, int offset, int length)
          Method for retrieving data values from the sensor.
 int getId()
           
 java.lang.String getProductID()
          Read the sensor's product identifier.
 java.lang.String getVendorID()
          Read the sensor's vendor identifier.
 java.lang.String getVersion()
          Read the sensor's version string.
 int sendData(int register, byte value)
          Sets a single byte in the I2C sensor.
 int sendData(int register, byte[] data, int length)
           
 int sendData(int register, byte[] data, int offset, int length)
          Send data top the sensor
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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

DEFAULT_I2C_ADDRESS

protected static final int DEFAULT_I2C_ADDRESS
See Also:
Constant Field Values

port

protected byte port

address

protected int address
Constructor Detail

I2CSensor

public I2CSensor(I2CPort port)

I2CSensor

public I2CSensor(I2CPort port,
                 int mode)
Parameters:
port -
mode - will not work on PC side

I2CSensor

public I2CSensor(I2CPort port,
                 int address,
                 int mode,
                 int type)
Parameters:
port -
address -
mode - will not work on PC side
type -
Method Detail

getId

public int getId()

getData

public int getData(int register,
                   byte[] buf,
                   int length)

getData

public int getData(int register,
                   byte[] buf,
                   int offset,
                   int length)
Method for retrieving data values from the sensor. BYTE0 ( is usually the primary data value for the sensor. Data is read from registers in the sensor, usually starting at 0x00 and ending around 0x49. Just supply the register to start reading at, and the length of bytes to read (16 maximum). NOTE: The NXT supplies UBYTE (unsigned byte) values but Java converts them into signed bytes (probably more practical to return short/int?)

Parameters:
register - e.g. FACTORY_SCALE_DIVISOR, BYTE0, etc....
length - Length of data to read (minimum 1, maximum 16)
Returns:
the status

getData

public int getData(int register)
Helper method to return a single register byte.

Parameters:
register -
Returns:
the byte of data

sendData

public int sendData(int register,
                    byte value)
Sets a single byte in the I2C sensor.

Parameters:
register - A data register in the I2C sensor. e.g. ACTUAL_ZERO
value - The data value.

sendData

public int sendData(int register,
                    byte[] data,
                    int length)

sendData

public int sendData(int register,
                    byte[] data,
                    int offset,
                    int length)
Send data top the sensor

Parameters:
register - A data register in the I2C sensor.
data - The byte to send.
length - the number of bytes

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

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.