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

lejos.hardware.sensor.NXTTouchSensor 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.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 nameDescriptionunit(s)Getter
TouchDetects a press of the buttonboolean {@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"; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy