All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.hardware.sensor.RCXLightSensor Maven / Gradle / Ivy

Go to download

leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.

The newest version!
package lejos.hardware.sensor;

import lejos.hardware.port.Port;
import lejos.robotics.*;

/**
 * LEGO RCX light Sensor
* The RCX light sensor measures light levels of reflected or ambient light. * *

*

* * * * * * * * * * * * * * * * * * * * * *
Supported modes
Mode nameDescriptionunit(s)Getter
RedMeasures the light value when illuminated with a red light source.N/A, normalized {@link #getRedMode() }
AmbientMeasures the light value of ambient light.N/A, normalized {@link #getAmbientMode() }
* *

* */ public class RCXLightSensor extends AnalogSensor implements SensorConstants, LampController { private static final int SWITCH_DELAY = 10; private boolean floodlight = false; /** * Create an RCX light sensor object attached to the specified port. The * sensor will be activated, i.e. the LED will be turned on. * * @param port * port, e.g. Port.S1 */ public RCXLightSensor(Port port) { super(port); init(); } protected void init() { setModes(new SensorMode[] { new RedMode(), new AmbientMode() }); port.setTypeAndMode(TYPE_REFLECTION, MODE_RAW); setFloodlight(true); } public int getFloodlight() { if (this.floodlight == true) return Color.RED; else return Color.NONE; } public boolean isFloodlightOn() { return floodlight; } public void setFloodlight(boolean floodlight) { this.floodlight = floodlight; if (floodlight == true) switchType(TYPE_REFLECTION, SWITCH_DELAY); else switchType(TYPE_CUSTOM, SWITCH_DELAY); } 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; } /** * 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"; } } 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"; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy