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 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  byte address
           
protected static java.lang.String BLANK
           
protected static byte PRODUCT_ID
          Returns the product ID of the sensor.
protected static byte SENSOR_TYPE
          Returns the sensor type.
protected static byte STOP
           
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 s)
           
I2CSensor(I2CPort s, byte sensorType)
           
 
Method Summary
protected  java.lang.String fetchString(int constantEnumeration, int rxLength)
          Helper method for retrieving string constants using I2C protocol.
 int getData(int register)
          Helper method to return a single register byte.
 int getData(int register, byte[] buf, int length)
          Method for retrieving data values from the sensor.
 int getId()
           
 java.lang.String getProductID()
          Returns the Product ID as a string.
 java.lang.String getSensorType()
          Returns the type of sensor as a string.
 java.lang.String getVersion()
          Returns the version number of the sensor hardware.
 int sendData(int register, byte value)
          Sets a single byte in the I2C sensor.
 int sendData(int register, byte[] data, int length)
          Send data top the sensor
 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

address

protected byte address

STOP

protected static byte STOP

BLANK

protected static java.lang.String BLANK

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 s,
                 byte sensorType)
Parameters:
s - A sensor. e.g. Port.S1

I2CSensor

public I2CSensor(I2CPort s)
Method Detail

getId

public int getId()

getData

public int getData(int register,
                   byte[] buf,
                   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)
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()
Returns the version number of the sensor hardware. NOTE: A little unreliable at the moment due to a bug in firmware. Keep trying if it doesn't get it the first time.

Returns:
The version number. e.g. "V1.0"

getProductID

public java.lang.String getProductID()
Returns the Product ID as a string. NOTE: A little unreliable at the moment due to a bug in firmware. Keep trying if it doesn't get it the first time.

Returns:
The product ID. e.g. "LEGO"

getSensorType

public java.lang.String getSensorType()
Returns the type of sensor as a string. NOTE: A little unreliable at the moment due to a bug in firmware. Keep trying if it doesn't get it the first time.

Returns:
The sensor type. e.g. "Sonar"

fetchString

protected java.lang.String fetchString(int constantEnumeration,
                                       int rxLength)
Helper method for retrieving string constants using I2C protocol.

Parameters:
constantEnumeration - e.g. I2CProtocol.VERSION
rxLength -
Returns:
the 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