lejos.nxt
Class ColorLightSensor

java.lang.Object
  extended by lejos.nxt.ColorLightSensor
All Implemented Interfaces:
SensorConstants, ColorDetector, LampLightDetector, LightDetector

public class ColorLightSensor
extends Object
implements LampLightDetector, ColorDetector, SensorConstants

LEGO Color Sensor driver. This driver provides access to the LEGO Color sensor. It allows the reading of 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  Colors.Color[] colorMap
           
protected  SensorPort port
           
protected  int type
           
 
Fields inherited from interface lejos.nxt.SensorConstants
MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, 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
 
Constructor Summary
ColorLightSensor(SensorPort port, int type)
          Create a new Color Sensor instance and bind it to a port.
 
Method Summary
 void calibrateHigh()
          call this method when the light sensor is reading the high value - used by readValue
 void calibrateLow()
          call this method when the light sensor is reading the low value - used by readValue
 int getBlueComponent()
           
 int[] getColor()
          Return the Red, Green and Blue values together in one array.
 Colors.Color getFloodlight()
          Returns the color of the floodlight, including Color.NONE.
 int getGreenComponent()
           
 int getHigh()
          return the normalized value corresponding to readValue() = 100;
 int getLightValue()
          Returns the calibrated and normalized brightness of the white light detected.
 int getLow()
          return the normalized value corresponding to readValue() = 0
 int getNormalizedLightValue()
          Returns the normalized value of the brightness of the white light detected, such that the lowest value is darkness and the highest value is intense bright light.
 int getRedComponent()
           
 boolean isFloodlightOn()
          Checks if the floodlight is currently on.
 Colors.Color readColor()
          Read the current color and return an enum value.
 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.
 int readValue()
          Return a single processed value.
 int readValues(int[] vals)
          Return a set of calibrated data.
 void setFloodlight(boolean floodlight)
          Turns the default LED light on or off.
 boolean setFloodlight(Colors.Color color)
          Used to turn on or off the floodlight by color.
 void setHigh(int high)
          set the normalized value corresponding to readValue() = 100;
 void setLow(int low)
          set the normalized value corresponding to readValue() = 0
 void setType(int type)
          Change the type of the sensor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

colorMap

protected Colors.Color[] colorMap

port

protected SensorPort port

type

protected int type
Constructor Detail

ColorLightSensor

public ColorLightSensor(SensorPort port,
                        int type)
Create a new Color Sensor instance and bind it to a port.

Parameters:
port - Port to use for the sensor.
type - Initial operating mode.
Method Detail

setType

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

Parameters:
type - new sensor type.

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.

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]

Parameters:
vals - array of four color values.
Returns:
< 0 if there is an error number of values returned if ok.

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]

Parameters:
vals - 4 element array for the results
Returns:
< 0 if error. 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.

Returns:
processed color value.

readColor

public Colors.Color readColor()
Read the current color and return an enum value. This is usually only accurate at a distance of about 1 cm.It is not very good at detecting yellow.

Returns:
The color enumeration under the sensor.

getLightValue

public int getLightValue()
Description copied from interface: LightDetector
Returns the calibrated and normalized brightness of the white light detected.

Specified by:
getLightValue in interface LightDetector
Returns:
A brightness value between 0 and 100%, with 0 = darkness and 100 = intense sunlight

getNormalizedLightValue

public int getNormalizedLightValue()
Description copied from interface: LightDetector
Returns the normalized value of the brightness of the white light detected, such that the lowest value is darkness and the highest value is intense bright light.

Specified by:
getNormalizedLightValue in interface LightDetector
Returns:
A raw value, between getLow() and getHigh(). Usually between 0 and 1023 but can be anything depending on hardware.

setFloodlight

public void setFloodlight(boolean floodlight)
Description copied from interface: LampLightDetector
Turns the default LED light on or off. If the sensor has more than one lamp color, this will control the red LED.

Specified by:
setFloodlight in interface LampLightDetector
Parameters:
floodlight - true to turn on lamp, false for off (ambient light only).

getBlueComponent

public int getBlueComponent()
Specified by:
getBlueComponent in interface ColorDetector
Returns:
The calibrated/normalized blue value (0-255)

getGreenComponent

public int getGreenComponent()
Specified by:
getGreenComponent in interface ColorDetector
Returns:
The calibrated/normalized green value (0-255)

getColor

public int[] getColor()
Description copied from interface: ColorDetector
Return the Red, Green and Blue values together in one array. All values are 0-255.

Specified by:
getColor in interface ColorDetector
Returns:
All three color values. The array index for Red is 0, Green is 1, Blue is 2.

getRedComponent

public int getRedComponent()
Specified by:
getRedComponent in interface ColorDetector
Returns:
The calibrated/normalized red value (0-255)

getFloodlight

public Colors.Color getFloodlight()
Description copied from interface: LampLightDetector
Returns the color of the floodlight, including Color.NONE.

Specified by:
getFloodlight in interface LampLightDetector
Returns:
An enumeration of the current color.

isFloodlightOn

public boolean isFloodlightOn()
Description copied from interface: LampLightDetector
Checks if the floodlight is currently on.

Specified by:
isFloodlightOn in interface LampLightDetector
Returns:
true if on, false if off.

setFloodlight

public boolean setFloodlight(Colors.Color color)
Description copied from interface: LampLightDetector
Used to turn on or off the floodlight by color. If the sensor has multiple light colors, you can control which color is turned on or off. If the color does not exist, it does nothing and returns false.

Specified by:
setFloodlight in interface LampLightDetector
Parameters:
color - Use lejos.robotics.Color enumeration constant to control lamp colors.
Returns:
True if lamp changed, false if lamp color doesn't exist for this sensor.

calibrateLow

public void calibrateLow()
call this method when the light sensor is reading the low value - used by readValue


calibrateHigh

public void calibrateHigh()
call this method when the light sensor is reading the high value - used by readValue


setLow

public void setLow(int low)
set the normalized value corresponding to readValue() = 0

Parameters:
low - the low value

setHigh

public void setHigh(int high)
set the normalized value corresponding to readValue() = 100;

Parameters:
high - the high value

getLow

public int getLow()
return the normalized value corresponding to readValue() = 0

Specified by:
getLow in interface LightDetector
Returns:

getHigh

public int getHigh()
return the normalized value corresponding to readValue() = 100;

Specified by:
getHigh in interface LightDetector
Returns: