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

lejos.hardware.sensor.NXTSoundSensor 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;
import lejos.robotics.SampleProvider;

/**
 * NXT Sound sensor
* Description * *

*

* * * * * * * * * * * * * * * * * * * * * *
Supported modes
Mode nameDescriptionunit(s)Getter
dBAMeasures sound level adjusted to the sensitivity of the human earN/A, normalized {@link #getDBAMode() }
dBMeasures sound levelN/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"; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy