lejos.hardware.sensor.RCXThermometer Maven / Gradle / Ivy
Show all versions of lejos-ev3-api Show documentation
package lejos.hardware.sensor;
import lejos.hardware.port.LegacySensorPort;
/**
* Lego RCX temperature sensor
* The sensor measures both atmospheric pressure and temperature.
*
*
* The code for this sensor has not been tested. Please report test results to
* the leJOS forum.
*
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* Temperature
* Measures temperature
* Degree Celcius
* {@link #getTemperatureMode() }
*
*
*
*
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Soren Hilmer
*
*/
public class RCXThermometer extends AnalogSensor implements SensorConstants {
LegacySensorPort port;
/**
* Create an RCX temperature sensor object attached to the specified port.
* @param port port, e.g. Port.S1
*/
public RCXThermometer(LegacySensorPort port)
{
super(port);
init();
}
protected void init() {
setModes(new SensorMode[]{ new TemperatureMode() });
port.setTypeAndMode(TYPE_TEMPERATURE, MODE_RAW);
}
/**
* Return a sample provider in temperature mode
*/
public SensorMode getTemperatureMode() {
return getMode(0);
}
private class TemperatureMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
sample[offset] = (785-NXTRawValue(port.getPin1()))/8.0f +273.15f; // Kelvin
}
@Override
public String getName() {
return "Temperature";
}
}
}