lejos.hardware.sensor.NXTLightSensor Maven / Gradle / Ivy
Show all versions of lejos-ev3-api Show documentation
package lejos.hardware.sensor;
import lejos.hardware.port.AnalogPort;
import lejos.hardware.port.Port;
import lejos.robotics.Color;
import lejos.robotics.LampController;
/**
* LEGO NXT light Sensor
* The NXT light sensor measures light levels of reflected or ambient light.
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Red
* Measures the light value when illuminated with a red light source.
* N/A, normalized
* {@link #getRedMode() }
*
*
* Ambient
* Measures the light value of ambient light.
* N/A, normalized
* {@link #getAmbientMode() }
*
*
*
*
* See Mindstorms NXT HDK/SDK
*
*/
public class NXTLightSensor extends AnalogSensor implements LampController, SensorConstants
{
protected static final long SWITCH_DELAY = 10;
private boolean floodlight = false;
private class AmbientMode implements SensorMode
{
@Override
public int sampleSize()
{
return 1;
}
@Override
public void fetchSample(float[] sample, int offset)
{
setFloodlight(false);
sample[offset] = 1.0f - normalize(port.getPin1());
}
@Override
public String getName()
{
return "Ambient";
}
}
protected void init()
{
setModes(new SensorMode[]{ new RedMode(), new AmbientMode() });
setFloodlight(true);
}
/**
* Create a light sensor object attached to the specified port.
* The sensor will be set to floodlight mode, i.e. the LED will be turned on.
* @param port port, e.g. Port.S1
*/
public NXTLightSensor(AnalogPort port)
{
super(port);
init();
}
/**
* Create a light sensor object attached to the specified port.
* The sensor will be set to floodlight mode, i.e. the LED will be turned on.
* @param port port, e.g. Port.S1
*/
public NXTLightSensor(Port port)
{
super(port);
init();
}
public void setFloodlight(boolean floodlight)
{
switchType(floodlight ? TYPE_LIGHT_ACTIVE : TYPE_LIGHT_INACTIVE, SWITCH_DELAY);
this.floodlight = floodlight;
}
public boolean setFloodlight(int color) {
if(color == Color.RED) {
setFloodlight(true);
return true;
} else if (color == Color.NONE) {
setFloodlight(false);
return true;
} else return false;
}
public int getFloodlight() {
if(this.floodlight == true)
return Color.RED;
else
return Color.NONE;
}
public boolean isFloodlightOn() {
return this.floodlight;
}
/**
* get a sample provider the returns the light value when illuminated with a
* Red light source.
* @return the sample provider
*/
public SensorMode getRedMode()
{
return getMode(0);
}
/**
* get a sample provider the returns the light value when illuminated without a
* light source.
* @return the sample provider
*/
public SensorMode getAmbientMode()
{
return getMode(1);
}
private class RedMode implements SensorMode {
@Override
public int sampleSize()
{
return 1;
}
@Override
public void fetchSample(float[] sample, int offset)
{
setFloodlight(true);
sample[offset] = 1.0f - normalize(port.getPin1());
}
@Override
public String getName()
{
return "Red";
}
}
}