lejos.hardware.sensor.NXTSoundSensor 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;
import lejos.robotics.SampleProvider;
/**
* NXT Sound sensor
* Description
*
*
*
*
* Supported modes
*
*
* Mode name
* Description
* unit(s)
* Getter
*
*
* dBA
* Measures sound level adjusted to the sensitivity of the human ear
* N/A, normalized
* {@link #getDBAMode() }
*
*
* dB
* Measures sound level
* N/A, normalized
* {@link #getDBMode() }
*
*
*
*
*
*
* See Mindstorms NXT HDK/SDK
* See The
* leJOS sensor framework
* See {@link lejos.robotics.SampleProvider leJOS conventions for
* SampleProviders}
*
*
*
*
* @author Lawrie Griffiths
*
*/
public class NXTSoundSensor extends AnalogSensor implements SensorConstants {
protected static final long SWITCH_DELAY = 10;
/**
* Create a sound sensor.
*
* @param port
* the sensor port to use
*/
public NXTSoundSensor(Port port) {
super(port);
init();
}
/**
* Create a sound sensor.
*
* @param port
* the sensor port to use
*/
public NXTSoundSensor(AnalogPort port) {
super(port);
init();
}
private void init() {
setModes(new SensorMode[]{ new DBAMode(), new DBMode() });
}
public class DBMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchType(TYPE_SOUND_DB, SWITCH_DELAY);
sample[offset] = 1.0f - normalize(port.getPin1());
}
@Override
public String getName() {
return "Sound DB";
}
}
/**
* get a sample provider the returns the sound level
* @return the sample provider
*/
public SampleProvider getDBMode() {
return getMode(1);
}
/**
* get a sample provider the returns the sound level adjusted to how a human ear would experience it
* @return the sample provider
*/
public SampleProvider getDBAMode() {
return getMode(0);
}
private class DBAMode implements SensorMode {
@Override
public int sampleSize() {
return 1;
}
@Override
public void fetchSample(float[] sample, int offset) {
switchType(TYPE_SOUND_DBA, SWITCH_DELAY);
sample[offset] = 1.0f - normalize(port.getPin1());
}
@Override
public String getName() {
return "Sound DBA";
}
}
}