lejos.nxt
Class I2CSensor

java.lang.Object
  extended by lejos.nxt.I2CSensor
All Implemented Interfaces:
SensorConstants
Direct Known Subclasses:
ColorSensor, CompassSensor, IRLink, IRSeeker, LMotor, LSC, MSC, MServo, NXTCam, NXTe, NXTLineLeader, OpticalDistanceSensor, PFLink, PFMate, PSPNXController, RCXLink, RCXMotorMultiplexer, RCXSensorMultiplexer, RFIDSensor, TiltSensor, UltrasonicSensor

public class I2CSensor
extends Object
implements SensorConstants

Abstract 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 static byte PRODUCT_ID
          Returns the product ID of the sensor.
protected static byte SENSOR_TYPE
          Returns the sensor type.
protected static byte VERSION
          Returns the version number of the sensor.
 
Fields inherited from interface lejos.nxt.SensorConstants
MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, 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
 
Constructor Summary
I2CSensor(I2CPort port)
           
I2CSensor(I2CPort port, int mode)
           
 
Method Summary
protected  String fetchString(int register, int len)
          Internal helper function, read a string from the device
 int getData(int register, byte[] buf, 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()
          Return the sensor product identifier.
 String getSensorType()
          Return the sensor type.
 String getVersion()
          Return the sensor version number.
 int sendData(int register, byte value)
          Executes an I2C write transaction.
 int sendData(int register, byte[] buf, int len)
          Executes an I2C write transaction.
 void setAddress(int addr)
          Set the address of the port Note that addresses are from 0x01 to 0x7F not even numbers from 0x02 to 0xFE as given in some I2C device specifications.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

protected static byte VERSION
Returns the version number of the sensor. e.g. "V1.0" Reply length = 8.


PRODUCT_ID

protected static byte PRODUCT_ID
Returns the product ID of the sensor. e.g. "LEGO" Reply length = 8.


SENSOR_TYPE

protected static byte SENSOR_TYPE
Returns the sensor type. e.g. "Sonar" Reply length = 8.

Constructor Detail

I2CSensor

public I2CSensor(I2CPort port,
                 int mode)

I2CSensor

public I2CSensor(I2CPort port)
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:
status == 0 success, != 0 failure

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:
status zero=success, non-zero=failure

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:
status zero=success, non-zero=failure

getVersion

public String getVersion()
Return the sensor version number.

Returns:
8-byte string

getProductID

public String getProductID()
Return the sensor product identifier.

Returns:
8-byte string

getSensorType

public String getSensorType()
Return the sensor type.

Returns:
8-byte string

fetchString

protected String fetchString(int register,
                             int len)
Internal helper function, read a string from the device

Parameters:
register -
len - The length of the space padded reply.
Returns:
the requested string

setAddress

public void setAddress(int addr)
Set the address of the port Note that addresses are from 0x01 to 0x7F not even numbers from 0x02 to 0xFE as given in some I2C device specifications. They are 7-bit addresses not 8-bit addresses.

Parameters:
addr - 1 to 0x7F

getPort

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

Returns:
the I2CPort