lejos.nxt
Class SensorPort.ColorSensorReader

java.lang.Object
  extended by lejos.nxt.SensorPort.SensorReader
      extended by lejos.nxt.SensorPort.ColorSensorReader
Enclosing class:
SensorPort

protected class SensorPort.ColorSensorReader
extends SensorPort.SensorReader

Lego Color Sensor driver. This driver provides access to the Lego Color sensor. It allows the reading raw and processed color values. The sensor has a tri-color led and this can be set to output red/green/blue or off. It also has a full mode in which four samples are read (off/red/green/blue) very quickly. These samples can then be combined using the calibration data provided by the device to determine the "Lego" color currently being viewed.

Author:
andy

Field Summary
protected static int ADMAX
           
protected static int ADVOLTS
           
protected  int[][] calData
           
protected  int[] calLimits
           
protected static int CLOCK
           
protected  Colors.Color[] colorMap
           
protected static int DATA
           
protected  boolean initialized
           
protected static int MINBLANKVAL
           
protected  int[] rawValues
           
static int RGB_BLANK
           
static int RGB_BLUE
           
static int RGB_GREEN
           
static int RGB_RED
          Indexes into the output arrays for specific color values.
protected static int SENSORMAX
           
protected  int type
           
protected  int[] values
           
 
Constructor Summary
SensorPort.ColorSensorReader()
          Create a new Color Sensor instance and bind it to a port.
 
Method Summary
protected  int calcCRC(int crc, int val)
          Incrementally calculate the CRC value of the read data.
protected  void calibrate(int[] vals)
          This method accepts a set of raw values (in full color mode) and processes them using the calibration data to return standard RGB values between 0 and 255
protected  boolean checkInitialized()
          Check to see if a sensor is attached and working, Read the standard sensor analogue pin to see if a the sensor is present.
protected  boolean checkPresent()
          Check to see if a sensor is attached.
protected  boolean checkSensor()
          Check the state of an initialized sensor.
protected  boolean getData()
          get the current digital value from the data pin.
protected  boolean initSensor(int mode)
          Initialize the sensor and set the operating mode.
protected  void initValues()
          initialize the raw and processed RGB values
protected  int readByte()
          Read a data byte from the sensor.
protected  boolean readCalibration()
          Read the calibration data from the sensor.
protected  int readData()
          Read the current analogue value from the data pin
protected  int readFullColorValue(int newClock)
          Read a value from the sensor when in fill color mode.
 int readRawValue()
          Return a single raw value from the device.
 int readRawValues(int[] vals)
          When in full color mode this returns all four raw color values from the device by doing four very quick reads and flashing all colors.
protected  boolean readSensor()
          Read the device
 int readValue()
          Return a single processed value.
 int readValues(int[] vals)
          Return a set of calibrated data.
 void reset()
          Reset the sensor.
protected  void resetSensor()
          perform a reset of the device.
protected  void sendMode(int mode)
          Send the new operating mode to the sensor.
protected  void setClock(int val)
          Set the clock pin to the specified value
protected  void setData(int val)
          Set the data pin to the specified value
 void setType(int type)
          Change the type of the sensor
 
Methods inherited from class lejos.nxt.SensorPort.SensorReader
setMode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RGB_RED

public static final int RGB_RED
Indexes into the output arrays for specific color values.

See Also:
Constant Field Values

RGB_GREEN

public static final int RGB_GREEN
See Also:
Constant Field Values

RGB_BLUE

public static final int RGB_BLUE
See Also:
Constant Field Values

RGB_BLANK

public static final int RGB_BLANK
See Also:
Constant Field Values

colorMap

protected Colors.Color[] colorMap

CLOCK

protected static final int CLOCK
See Also:
Constant Field Values

DATA

protected static final int DATA
See Also:
Constant Field Values

initialized

protected boolean initialized

type

protected int type

ADVOLTS

protected static final int ADVOLTS
See Also:
Constant Field Values

ADMAX

protected static final int ADMAX
See Also:
Constant Field Values

MINBLANKVAL

protected static final int MINBLANKVAL
See Also:
Constant Field Values

SENSORMAX

protected static final int SENSORMAX
See Also:
Constant Field Values

calData

protected int[][] calData

calLimits

protected int[] calLimits

rawValues

protected int[] rawValues

values

protected int[] values
Constructor Detail

SensorPort.ColorSensorReader

public SensorPort.ColorSensorReader()
Create a new Color Sensor instance and bind it to a port.

Method Detail

initValues

protected void initValues()
initialize the raw and processed RGB values


setType

public void setType(int type)
Change the type of the sensor

Overrides:
setType in class SensorPort.SensorReader
Parameters:
type - new sensor type.

reset

public void reset()
Reset the sensor.

Overrides:
reset in class SensorPort.SensorReader

setClock

protected void setClock(int val)
Set the clock pin to the specified value

Parameters:
val - the new value(0/1) for the pin.

setData

protected void setData(int val)
Set the data pin to the specified value

Parameters:
val - new value(0/1) for the pin.

getData

protected boolean getData()
get the current digital value from the data pin.

Returns:
current pin value

readData

protected int readData()
Read the current analogue value from the data pin

Returns:
current value of the pin.

resetSensor

protected void resetSensor()
perform a reset of the device.


sendMode

protected void sendMode(int mode)
Send the new operating mode to the sensor. The value is sent to the sensor by using the clock pin to clock a series of 8 bits out to the device.

Parameters:
mode -

readByte

protected int readByte()
Read a data byte from the sensor. The data is read by reading the digital value of the data pin while using the clock pin to request each of the 8 bits.

Returns:
The read byte.

calcCRC

protected int calcCRC(int crc,
                      int val)
Incrementally calculate the CRC value of the read data.

Parameters:
crc - current crc
val - new value
Returns:
new crc

readCalibration

protected boolean readCalibration()
Read the calibration data from the sensor. This consists of two tables. The first contians 3 rows of data with each row having 4 columns. The data is sent one row at a time. Each row contains a calibration constant for red/green/blue/blank readings. The second table contains 2 threshold values that are used (based on the background light reading) to select the row to use from the first table. Finally there is a CRC value which is used to ensure correct reading of the data.

Returns:
true if ok false if error

initSensor

protected boolean initSensor(int mode)
Initialize the sensor and set the operating mode.

Parameters:
mode - Operating mode.
Returns:
true if ok false if error.

checkPresent

protected boolean checkPresent()
Check to see if a sensor is attached. Read the standard sensor analogue pin to see if a the sensor is present. If it is it will pull this pin down.

Returns:
true if sensor is connected false otherwise.

checkInitialized

protected boolean checkInitialized()
Check to see if a sensor is attached and working, Read the standard sensor analogue pin to see if a the sensor is present. If it is it will pull this pin down. If the sensor is detected but it has not been initialized then initialize it.

Returns:
true if sensor is connected and working false otherwise.

checkSensor

protected boolean checkSensor()
Check the state of an initialized sensor. Once initialized this method will check that the sensor is not reporting an error state. The sensor can do this by pulling the clock pin high

Returns:
true if ok false if error.

readFullColorValue

protected int readFullColorValue(int newClock)
Read a value from the sensor when in fill color mode. When in full color mode the readings are taken by toggling the clock line to move from one reading to the next. This method performs this operation. It also samples the analogue value twice and returns the average reading.

Parameters:
newClock - New value for the clock pin
Returns:
the new reading

readSensor

protected boolean readSensor()
Read the device

Returns:
true if ok false if error

readRawValue

public int readRawValue()
Return a single raw value from the device. When in single color mode this returns the raw sensor reading. Values range from 0 to 1023 but usually don't get over 600.

Overrides:
readRawValue in class SensorPort.SensorReader
Returns:
the raw value or < 0 if there is an error.

readRawValues

public int readRawValues(int[] vals)
When in full color mode this returns all four raw color values from the device by doing four very quick reads and flashing all colors. The raw values theoretically range from 0 to 1023 but in practice they usually do not go higher than 600. You can access the index of each color using RGB_RED, RGB_GREEN, RGB_BLUE and RGB_BLANK. e.g. to retrieve the Blue value: vals[ColorLightSensor.RGB_BLUE]

Overrides:
readRawValues in class SensorPort.SensorReader
Parameters:
vals - array of four color values.
Returns:
< 0 if there is an error the number of values if ok

calibrate

protected void calibrate(int[] vals)
This method accepts a set of raw values (in full color mode) and processes them using the calibration data to return standard RGB values between 0 and 255

Parameters:
vals - array to return the newly calibrated data.

readValues

public int readValues(int[] vals)
Return a set of calibrated data. If in single color mode the returned data is a simple percentage. If in full color mode the data is a set of calibrated red/blue/green/blank readings that range from 0 to 255. You can access the index of each color using RGB_RED, RGB_GREEN, RGB_BLUE and RGB_BLANK. e.g. to retrieve the Blue value: vals[ColorLightSensor.RGB_BLUE]

Overrides:
readValues in class SensorPort.SensorReader
Parameters:
vals - 4 element array for the results
Returns:
< 0 of error, the number of values if ok

readValue

public int readValue()
Return a single processed value. If in single color mode this returns a single reading as a percentage. If in full color mode it returns a Lego color value that identifies the color of the object in view.

Overrides:
readValue in class SensorPort.SensorReader
Returns:
processed color value.