lejos.hardware.sensor.NXTTouchSensor 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;
/**
* NXT Touch sensor
* A sensor that can be pressed like a button. Also works with RCX touch sensors.
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Touch
* Detects a press of the button
* boolean
* {@link #getTouchMode() }
*
*
*
*
*
* See Mindstorms NXT HDK/SDK
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*/
public class NXTTouchSensor extends AnalogSensor implements SensorConstants
{
/**
* Create a touch sensor object attached to the specified open port. Note this
* port will not be configured. Any configuration od the sensor port must take
* place externally.
* @param port an open Analog port
*/
public NXTTouchSensor(AnalogPort port)
{
super(port);
port.setTypeAndMode(TYPE_SWITCH, MODE_RAW);
init();
}
/**
* Create an NXT touch sensor object attached to the specified port.
* @param port the port that has the sensor attached
*/
public NXTTouchSensor(Port port)
{
super(port);
this.port.setTypeAndMode(TYPE_SWITCH, MODE_RAW);
init();
}
protected void init() {
setModes(new SensorMode[]{new TouchMode()});
}
/**
* get a sample provider that returns an indication of the button being up(0) or down(1)
* @return the sample provider
*/
public SensorMode getTouchMode()
{
return getMode(0);
}
private class TouchMode implements SensorMode {
@Override
public int sampleSize()
{
return 1;
}
@Override
public void fetchSample(float[] sample, int offset)
{
sample[offset] = (port.getPin1() > EV3SensorConstants.ADC_REF/2f ? 0.0f : 1.0f);
}
@Override
public String getName()
{
return "Touch";
}
}
}