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:
PrototypeSensor, UltrasonicSensor

public class I2CSensor
extends Sensor
implements SharedConstants

A sensor wrapper to allow easy access to I2C sensors, like the ultrasonic sensor. Currently uses the default I2C address of 0x02, but some sensors can be connected to same port lines and use different addresses using the Auto Detecting Parallel Architecture (ADPA). Currently unsure if there are commercial port expanders yet to use this function, or whether the Lego UltrasonicSensor sensor is ADPA compatible.
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 class ch.aplu.nxt.Part
robot
 
Fields inherited from interface ch.aplu.nxt.SharedConstants
ABOUT, ANGLE, ANGLESTEPSMODE, BOOLEANMODE, BOOT, BRAKE, CELSIUSMODE, CLOSE, CUSTOM, DEBUG_LEVEL_HIGH, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_OFF, DELETE, DELETE_USER_FLASH, DIRECT_COMMAND_NOREPLY, DIRECT_COMMAND_REPLY, FAHRENHEITMODE, FIND_FIRST, FIND_NEXT, GET_BATTERY_LEVEL, GET_CURRENT_PROGRAM_NAME, GET_DEVICE_INFO, GET_FIRMWARE_VERSION, GET_INPUT_VALUES, GET_OUTPUT_STATE, HIGH_SPEED_BUFFER, KEEP_ALIVE, LIGHT_ACTIVE, LIGHT_INACTIVE, LOWSPEED, LOWSPEED_9V, LS_GET_STATUS, LS_READ, LS_WRITE, MESSAGE_READ, MESSAGE_WRITE, MODEMASK, MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPDOWN, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTORON, NO_OF_SENSOR_TYPES, NO_SENSOR, NXJ_FIND_FIRST, NXJ_FIND_NEXT, 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, RAWMODE, READ, REFLECTION, REGULATED, REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC, REPLY_COMMAND, RESET_MOTOR_POSITION, RESET_SCALED_INPUT_VALUE, SET_BRICK_NAME, SET_INPUT_MODE, SET_OUTPUT_STATE, SLOPEMASK, SOUND_DB, SOUND_DBA, START_PROGRAM, STOP_PROGRAM, STOP_SOUND_PLAYBACK, SWITCH, SYSTEM_COMMAND_NOREPLY, SYSTEM_COMMAND_REPLY, TEMPERATURE, TITLE, TITLEMP, TRANSITIONCNTMODE, VERSION, WRITE
 
Constructor Summary
I2CSensor(SensorPort port, byte sensorType)
          Creates a sensor instance of given type connected to the given port.
 
Method Summary
protected  void cleanup()
           
protected  java.lang.String fetchString(byte constantEnumeration, int rxLength)
           
 byte[] getData(byte register, int length)
          Retrieves data from the sensor.
protected  void init()
           
 void sendData(byte register, byte value)
          Sets a single byte in the I2C sensor.
 void sendData(byte register, byte value1, byte value2)
          Sets two bytes in the I2C sensor.
 
Methods inherited from class ch.aplu.nxt.Sensor
getPort, getPortId, getPortLabel, LSRead, LSWrite, readBooleanValue, readNormalizedValue, readRawValue, readScaledValue, setTypeAndMode
 
Methods inherited from class ch.aplu.nxt.Part
setRobot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

I2CSensor

public I2CSensor(SensorPort port,
                 byte sensorType)
Creates a sensor instance of given type connected to the given port.

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

init

protected void init()
Specified by:
init in class Part

getData

public byte[] getData(byte register,
                      int length)
Retrieves data from 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 an unsigned byte but Java converts them into signed bytes.

Parameters:
register - the register used, e.g. FACTORY_SCALE_DIVISOR, BYTE0, etc....
length - the length of data to read (minimum 1, maximum 16)
Returns:
the data from the sensor

sendData

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

Parameters:
register - the data register in the I2C sensor, e.g. ACTUAL_ZERO
value - the data sent to the sensor

sendData

public void sendData(byte register,
                     byte value1,
                     byte value2)
Sets two bytes in the I2C sensor.

Parameters:
register - the data register in the I2C sensor, e.g. ACTUAL_ZERO
value1 - the first data value sent to the sensor
value2 - the second data value sent to the sensor

fetchString

protected java.lang.String fetchString(byte constantEnumeration,
                                       int rxLength)

cleanup

protected void cleanup()
Specified by:
cleanup in class Part