soundsensor.java

来自「专业汽车级嵌入式操作系统OSEK的源代码」· Java 代码 · 共 57 行

JAVA
57
字号
package lejos.nxt;/** * Abstraction for a NXT sound sensor. *  */public class SoundSensor implements SensorConstants {	ADSensorPort port;		/**	 * Create a sound sensor object attached to the specified port.	 * The sensor will be set to DB mode.	 * @param port port, e.g. Port.S1	 */	public SoundSensor(ADSensorPort port)	{	   this.port = port;	   port.setTypeAndMode(TYPE_SOUND_DB,                           MODE_PCTFULLSCALE);	}		/**	 * Create a sound sensor object attached to the specified port,	 * and sets DB or DBA mode.	 * @param port port, e.g. Port.S1	 * @param dba true to set DBA mode, false for DB mode.	 */	public SoundSensor(SensorPort port, boolean dba)	{	   this.port = port;       port.setTypeAndMode(    		   (dba ? TYPE_SOUND_DBA    				: TYPE_SOUND_DB),    		   MODE_PCTFULLSCALE);   	}		/**	 * Set DB or DBA mode.	 * @param dba true to set DBA mode, false for DB mode.	 */	public void setDBA(boolean dba)	{	    port.setType((dba ? TYPE_SOUND_DBA	    				  : TYPE_SOUND_DB));	}	/**	 * Read the current sensor value.	 * @return value as a percentage.	 */	public int readValue()	{		return ((1023 - port.readRawValue()) * 100/ 1023);  	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?