lejos.hardware.sensor.EV3TouchSensor 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;
/**
* Basic sensor driver for the Lego EV3 Touch sensor
* @author andy
*
*/
/**
* Lego EV3 Touch sensor
* The analog EV3 Touch Sensor is a simple but exceptionally precise tool that detects when its front button is pressed or released.
*
* *
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Touch
* Detects when its front button is pressed
* Binary
* {@link #getTouchMode() }
*
*
*
*
*
*
* See Sensor Product page
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Your name
*
*/
public class EV3TouchSensor extends AnalogSensor
{
public EV3TouchSensor(AnalogPort port)
{
super(port);
init();
}
public EV3TouchSensor(Port port)
{
super(port);
init();
}
protected void init() {
setModes(new SensorMode[]{ new TouchMode() });
}
/**
* Lego EV3 Touch sensor, Touch mode
* Detects when its front button is pressed
*
*
* Size and content of the sample
* The sample contains one element, a value of 0 indicates that the button is not presse, a value of 1 indicates the button is pressed.
*
*
*
* @return A sampleProvider
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*/
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.getPin6() > EV3SensorConstants.ADC_REF/2f ? 1.0f : 0.0f);
}
@Override
public String getName()
{
return "Touch";
}
}
}