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, RealTimeClock, RFIDSensor, SensorMux, TemperatureSensor, TetrixMotorController, TetrixServoController, TouchPanel, UltrasonicSensor

public class I2CSensor
extends Object
implements SensorConstants

Class that implements common methods for all I2C sensors. Extend this class to implement new I2C sensors.

Author:
Lawrie Griffiths (lawrie.griffiths@ntlworld.com).

Field Summary
protected  int address
           
protected static int DEFAULT_I2C_ADDRESS
           
protected  I2CPort 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  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, byte[] buf, int len)
          Executes an I2C read transaction and waits for the result.
 int getData(int register, byte[] buf, int offset, int len)
          Executes an I2C read transaction and waits for the result.
 I2CPort getPort()
          Get the port that the sensor is attached to
 String getProductID()
          Read the sensor's product identifier.
 String getVendorID()
          Read the sensor's vendor identifier.
 String getVersion()
          Read the sensor's version string.
 int sendData(int register, byte value)
          Executes an I2C write transaction.
 int sendData(int register, byte[] buf, int len)
          Executes an I2C write transaction.
 int sendData(int register, byte[] buf, int offset, int len)
          Executes an I2C write transaction.
 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 I2CPort port

address

protected int address
Constructor Detail

I2CSensor

public I2CSensor(I2CPort port)

I2CSensor

public I2CSensor(I2CPort port,
                 int mode)

I2CSensor

public I2CSensor(I2CPort port,
                 int address,
                 int mode,
                 int type)
Method Detail

getData

public int 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
Returns:
negative value on error, 0 otherwise

getData

public int 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
Returns:
negative value on error, 0 otherwise

sendData

public int 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
Returns:
negative value on error, 0 otherwise

sendData

public int 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
Returns:
negative value on error, 0 otherwise

sendData

public int sendData(int register,
                    byte value)
Executes an I2C write transaction.

Parameters:
register - I2C register, e.g 0x42
value - single byte to send
Returns:
negative value on error, 0 otherwise

getVersion

public 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 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 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 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