ch.aplu.nxt
Class I2CSensor

java.lang.Object
  extended by ch.aplu.nxt.Part
      extended by ch.aplu.nxt.Sensor
          extended by ch.aplu.nxt.I2CSensor
All Implemented Interfaces:
SharedConstants
Direct Known Subclasses:
AccelerometerSensor, CompassHTSensor, PrototypeSensor, SuperProSensor, UltrasonicSensor

public class I2CSensor
extends Sensor
implements SharedConstants

A sensor wrapper to allow easy access to I2C sensors, like the ultrasonic sensor. Most of the code and the documentation taken from the leJOS library (lejos.sourceforge.net, with thanks to the autor.


Field Summary
 
Fields inherited from interface ch.aplu.nxt.SharedConstants
ABOUT, ACCELEROMETERPOLLDELAY, ANGLE, ANGLESTEPSMODE, AXELENGTH, BOOLEANMODE, BOOT, BRAKE, BRAKEDELAY, CELSIUSMODE, CLOSE, COLORPOLLDELAY, COMPASSPOLLDELAY, CRAWLERROTATIONFACTOR, CRAWLERROTATIONSPEED, CRAWLERSPEED, CRAWLERSTEPFACTOR, CUSTOM, DEBUG_LEVEL_HIGH, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_OFF, DEBUGLEVEL, DELETE, DELETE_USER_FLASH, DIRECT_COMMAND_NOREPLY, DIRECT_COMMAND_REPLY, FAHRENHEITMODE, FIND_FIRST, FIND_NEXT, GEARSPEED, GET_BATTERY_LEVEL, GET_CURRENT_PROGRAM_NAME, GET_DEVICE_INFO, GET_FIRMWARE_VERSION, GET_INPUT_VALUES, GET_OUTPUT_STATE, GYROPOLLDELAY, HIGH_SPEED_BUFFER, INFRAREDSEEKERPOLLDELAY, KEEP_ALIVE, LIGHT_ACTIVE, LIGHT_INACTIVE, LIGHTPOLLDELAY, LOWSPEED, LOWSPEED_9V, LS_GET_STATUS, LS_READ, LS_WRITE, MESSAGE_READ, MESSAGE_WRITE, MODEMASK, MOTIONDETECTORPOLLDELAY, MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPDOWN, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTORON, MOTORSPEED, MOTORSPEEDFACTOR, MOTORSPEEDMULTIPLIER, NO_OF_SENSOR_TYPES, NO_SENSOR, OPEN_APPEND_DATA, OPEN_READ, OPEN_READ_LINEAR, OPEN_WRITE, OPEN_WRITE_DATA, OPEN_WRITE_LINEAR, PCTFULLSCALEMODE, PERIODCOUNTERMODE, PLAY_SOUND_FILE, PLAY_TONE, POLL, POLL_BUFFER, POLL_LENGTH, PROTOTYPEPOLLDELAY, RAWMODE, READ, REFLECTION, REGULATED, REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC, REPLY_COMMAND, RESET_MOTOR_POSITION, RESET_SCALED_INPUT_VALUE, RFIDPOLLDELAY, SENSOREVENTDELAY, SET_BRICK_NAME, SET_INPUT_MODE, SET_OUTPUT_STATE, SLOPEMASK, SOUND_DB, SOUND_DBA, SOUNDPOLLDELAY, START_PROGRAM, STOP_PROGRAM, STOP_SOUND_PLAYBACK, SWITCH, SYSTEM_COMMAND_NOREPLY, SYSTEM_COMMAND_REPLY, TEMPERATURE, TITLE, TITLEMP, TOUCHPOLLDELAY, TRANSITIONCNTMODE, TURTLEROTATIONFACTOR, TURTLEROTATIONSPEED, TURTLESPEED, TURTLESTEPFACTOR, ULTRASONICPOLLDELAY, VERSION, WRITE
 
Constructor Summary
I2CSensor(SensorPort port, byte sensorType)
          Creates a sensor instance of given type connected to the given port with default device address 0x02.
I2CSensor(SensorPort port, byte sensorType, int deviceAddress)
          Creates a sensor instance of given type connected to the given port using the given I2C device address (default address is 0x02).
 
Method Summary
 int getData(int register, byte[] buf, int len)
          Retrieves data from the sensor.
 lejos.nxt.I2CSensor getLejosI2CSensor()
          Returns the reference of the the underlying lejos.nxt.I2CSensor.
 lejos.nxt.SensorPort getLejosPort()
          Returns the reference of the the underlying lejos.nxt.SensorPort.
 java.lang.String getProductID()
          Returns the product identifier (if available).
 java.lang.String getVersion()
          Returns the sensor version number (if available).
 int sendData(int register, byte value)
          Sets a single byte in the I2C sensor.
 int sendData(int register, byte[] buf, int len)
          Send multiple values with a I2C write transaction.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

I2CSensor

public I2CSensor(SensorPort port,
                 byte sensorType,
                 int deviceAddress)
Creates a sensor instance of given type connected to the given port using the given I2C device address (default address is 0x02).

Parameters:
port - the port where the sensor is plugged-in
sensorType - the type of the sensor
deviceAddress - in standard Lego/NXT format (range 0x02-0xFE). The low bit must always be zero. Some data sheets (and older versions of leJOS) may use i2c 7 bit format (0x01-0x7F) in which case this address must be shifted left one bit.

I2CSensor

public I2CSensor(SensorPort port,
                 byte sensorType)
Creates a sensor instance of given type connected to the given port with default device address 0x02.

Parameters:
port - the port where the sensor is plugged-in
sensorType - the type of the sensor
Method Detail

getData

public int getData(int register,
                   byte[] buf,
                   int len)
Retrieves data from the sensor. Data is read from registers in the sensor, usually starting at 0x00. Just supply the register to start reading at, and the length of bytes to read (16 maximum).

Parameters:
register - the starting register used
buf - the buffer where data are returned
len - the length of data to read (minimum 1, maximum 16)
Returns:
status zero=success, non-zero=failure

sendData

public int sendData(int register,
                    byte value)
Sets a single byte in the I2C sensor.

Parameters:
register - the data register in the I2C sensor
value - the data sent to the sensor
Returns:
status zero=success, non-zero=failure

sendData

public int sendData(int register,
                    byte[] buf,
                    int len)
Send multiple values with a I2C write transaction.

Parameters:
register - the starting register in the I2C sensor
buf - the buffer where data are supplied
len - the length the buffer (minimum 1, maximum 16)
Returns:
status zero=success, non-zero=failure

getProductID

public java.lang.String getProductID()
Returns the product identifier (if available).

Returns:
the product identifier (PID)

getVersion

public java.lang.String getVersion()
Returns the sensor version number (if available).

Returns:
the sensor version number

getLejosI2CSensor

public lejos.nxt.I2CSensor getLejosI2CSensor()
Returns the reference of the the underlying lejos.nxt.I2CSensor.

Returns:
the reference of the I2CSensor

getLejosPort

public lejos.nxt.SensorPort getLejosPort()
Returns the reference of the the underlying lejos.nxt.SensorPort.

Returns:
the reference of the SensorPort