lejos.nxt
Class ColorSensor

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

public class ColorSensor
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

Nested Class Summary
static class ColorSensor.Color
          Extended color class, that includes the background reading at the time that the other readings were made.
 
Field Summary
protected static int[] colorMap
           
protected  SensorPort port
           
protected  int type
           
 
Fields inherited from interface lejos.nxt.SensorConstants
BLACK, BLANK_INDEX, BLUE, BLUE_INDEX, GREEN, GREEN_INDEX, MAX_AD_RAW, MAX_TYPE, MIN_TYPE, MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, RED, RED_INDEX, 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, WHITE, YELLOW
 
Constructor Summary
ColorSensor(SensorPort port)
          Create a new Color Sensor instance and bind it to a port.
ColorSensor(SensorPort port, int color)
          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
 ColorSensor.Color getColor()
          Return a Color Object that contains the calibrated color readings.
 int getColorID()
          Read the current color and return an enumeration constant.
 int getFloodlight()
          Returns the color of the floodlight, including Color.NONE.
 int getHigh()
          return the normalized value corresponding to readValue() = 100;
 int getLightValue()
          Return the calibrated light reading.
 int getLow()
          return the normalized value corresponding to readValue() = 0
 int getNormalizedLightValue()
          Return the normalized light level.
 ColorSensor.Color getRawColor()
          Return a Color Object that contains the raw color readings.
 int getRawLightValue()
          Return the Raw light reading.
 boolean isFloodlightOn()
          Checks if the floodlight is currently on.
 void setFloodlight(boolean floodlight)
          Turns the default LED light on or off.
 boolean setFloodlight(int 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
protected  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 static int[] colorMap

port

protected SensorPort port

type

protected int type
Constructor Detail

ColorSensor

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

Parameters:
port - Port to use for the sensor.

ColorSensor

public ColorSensor(SensorPort port,
                   int color)
Create a new Color Sensor instance and bind it to a port. Set the floodlight to the specified color.

Parameters:
port - Port to use for the sensor.
color - The floodlight color.
Method Detail

setType

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

Parameters:
type - new sensor type.

getLightValue

public int getLightValue()
Return the calibrated light reading.

Specified by:
getLightValue in interface LightDetector
Returns:
Calibrated value as a percentage

getNormalizedLightValue

public int getNormalizedLightValue()
Return the normalized light level.

Specified by:
getNormalizedLightValue in interface LightDetector
Returns:
The normalized light value.

getRawLightValue

public int getRawLightValue()
Return the Raw light reading.

Returns:
Raw light reading 0-1023

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).

getColor

public ColorSensor.Color getColor()
Return a Color Object that contains the calibrated color readings.

Specified by:
getColor in interface ColorDetector
Returns:
Color data

getRawColor

public ColorSensor.Color getRawColor()
Return a Color Object that contains the raw color readings.

Returns:
Raw Color data (Note the color Id is always Color.NONE)

getFloodlight

public int 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(int 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. You can turn the floodlight off by using Color.NONE.

Specified by:
setFloodlight in interface LampLightDetector
Parameters:
color - Use 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:
the low value

getHigh

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

Specified by:
getHigh in interface LightDetector
Returns:
the high value

getColorID

public int getColorID()
Read the current color and return an enumeration constant. This is usually only accurate at a distance of about 1 cm.

Specified by:
getColorID in interface ColorDetector
Returns:
The color id under the sensor.