Package lejos.hardware.sensor
Class I2CSensor
- java.lang.Object
-
- lejos.hardware.Device
-
- lejos.hardware.sensor.BaseSensor
-
- lejos.hardware.sensor.I2CSensor
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,SensorConstants,SensorMode,SensorModes,SampleProvider
- Direct Known Subclasses:
CruizcoreGyro,DexterCompassSensor,DexterGPSSensor,DexterIMUSensor.DexterIMUAccelerationSensor,DexterThermalIRSensor,HiTechnicAccelerometer,HiTechnicAngleSensor,HiTechnicBarometer,HiTechnicColorSensor,HiTechnicCompass,HiTechnicIRSeeker,HiTechnicIRSeekerV2,IRLink,LMotor,LSC,MindsensorsAbsoluteIMU,MindsensorsAccelerometer,MindsensorsCompass,MindsensorsDistanceSensorV2,MindsensorsLightSensorArray,MindsensorsLineLeader,MindSensorsNumPad,MindSensorsPressureSensor,MSC,MServo,NXTCam,NXTe,NXTMMX,NXTUltrasonicSensor,PFLink,PFMate,PSPNXController,RCXLink,RCXMotorMultiplexer,RCXSensorMultiplexer,RFIDSensor,SensorMux,TetrixMotorController,TetrixServoController
public class I2CSensor extends BaseSensor implements SensorConstants
Class that implements common methods for all I2C sensors. Extend this class to implement new I2C sensors. TODO: We should probably change this class to use the Product ID etc. obtained by the Lego kernel module during initialisation.- Author:
- Lawrie Griffiths (lawrie.griffiths@ntlworld.com) and Andy Shaw.
-
-
Field Summary
Fields Modifier and Type Field Description protected intaddressprotected static intDEFAULT_I2C_ADDRESSprotected I2CPortportprotected static byteREG_PRODUCT_IDRegister number of sensor product ID, as defined by standard Lego I2C register layout.protected static byteREG_VENDOR_IDRegister number of sensor vendor ID, as defined by standard Lego I2C register layout.protected static byteREG_VERSIONRegister number of sensor version string, as defined by standard Lego I2C register layout.protected intretryCount-
Fields inherited from class lejos.hardware.sensor.BaseSensor
currentMode, modes
-
Fields inherited from interface lejos.hardware.sensor.SensorConstants
BLACK, BLANK_INDEX, BLUE, BLUE_INDEX, BROWN, GREEN, GREEN_INDEX, MAX_TYPE, MIN_TYPE, MODE_RAW, NXT_ADC_RES, RED, RED_INDEX, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HIGHSPEED, TYPE_HIGHSPEED_9V, 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
Constructors Constructor Description I2CSensor(I2CPort port)I2CSensor(I2CPort port, int address)Create the sensor using an already open sensor port.I2CSensor(Port port)I2CSensor(Port port, int address)I2CSensor(Port port, int address, int type)Create the sensor using the specified port.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected java.lang.StringfetchString(byte reg, int len)Read a string from the device.intgetAddress()Return the the I2C address of the sensor.voidgetData(int register, byte[] buf, int len)Executes an I2C read transaction and waits for the result.voidgetData(int register, byte[] buf, int offset, int len)Executes an I2C read transaction and waits for the result.I2CPortgetPort()Get the port that the sensor is attached tojava.lang.StringgetProductID()Read the sensor's product identifier.intgetRetryCount()Return the current get/send retry count valuejava.lang.StringgetVendorID()Read the sensor's vendor identifier.java.lang.StringgetVersion()Read the sensor's version string.voidsendData(int register, byte value)Executes an I2C write transaction.voidsendData(int register, byte[] buf, int len)Executes an I2C write transaction.voidsendData(int register, byte[] buf, int offset, int len)Executes an I2C write transaction.voidsetAddress(int addr)Deprecated.If the device has a changeable address, then constructor of the class should have an address parameter.voidsetRetryCount(int newCount)Set the number of times that a get/send data request should be retried.-
Methods inherited from class lejos.hardware.sensor.BaseSensor
fetchSample, getAvailableModes, getCurrentMode, getMode, getMode, getModeCount, getName, sampleSize, setCurrentMode, setCurrentMode, setModes
-
Methods inherited from class lejos.hardware.Device
close, releaseOnClose
-
-
-
-
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
-
retryCount
protected int retryCount
-
-
Constructor Detail
-
I2CSensor
public I2CSensor(I2CPort port, int address)
Create the sensor using an already open sensor port. Do not configure the hardware- Parameters:
port- the open portaddress- I2C address
-
I2CSensor
public I2CSensor(I2CPort port)
-
I2CSensor
public I2CSensor(Port port, int address, int type)
Create the sensor using the specified port. Configure the hardware as required.- Parameters:
port- port the sensor is attached toaddress- I2C addresstype- type of I2C sensor
-
I2CSensor
public I2CSensor(Port port)
-
I2CSensor
public I2CSensor(Port port, int address)
-
-
Method Detail
-
setRetryCount
public void setRetryCount(int newCount)
Set the number of times that a get/send data request should be retried.- Parameters:
newCount- number of times to try the request
-
getRetryCount
public int getRetryCount()
Return the current get/send retry count value- Returns:
- current retry count
-
getData
public void getData(int register, byte[] buf, int len)Executes an I2C read transaction and waits for the result.- Parameters:
register- I2C register, e.g 0x41buf- Buffer to return datalen- Length of the return data
-
getData
public void 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 0x41buf- Buffer to return dataoffset- Offset of the start of the datalen- Length of the return data
-
sendData
public void sendData(int register, byte[] buf, int len)Executes an I2C write transaction.- Parameters:
register- I2C register, e.g 0x42buf- Buffer containing data to sendlen- Length of data to send
-
sendData
public void sendData(int register, byte[] buf, int offset, int len)Executes an I2C write transaction.- Parameters:
register- I2C register, e.g 0x42buf- Buffer containing data to sendoffset- Offset of the start of the datalen- Length of data to send
-
sendData
public void sendData(int register, byte value)Executes an I2C write transaction.- Parameters:
register- I2C register, e.g 0x42value- single byte to send
-
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
@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
-
-