lejos.nxt
Class I2CSensor

java.lang.Object
  extended by lejos.nxt.I2CSensor
All Implemented Interfaces:
SensorConstants
Direct Known Subclasses:
AccelHTSensor, AccelMindSensor, AngleSensor, ColorSensorHT, CompassSensor, CruizcoreGyro, 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
          Returns the product ID of the sensor.
protected static byte REG_SENSOR_TYPE
          Returns the sensor type.
protected static byte REG_VERSION
          Returns the version number of the sensor.
 
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 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()
          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)
           
 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 byte REG_VERSION
Returns the version number of the sensor. e.g. "V1.0" Reply length = 8.


REG_PRODUCT_ID

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


REG_SENSOR_TYPE

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


DEFAULT_I2C_ADDRESS

protected static int DEFAULT_I2C_ADDRESS

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()
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(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 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 - 0x02 to 0xfe