java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.SoundSensorComponent
- All Implemented Interfaces:
DigitalEventProvider<SoundSensorComponent.SoundState>,EventProvider<DigitalEventListener<SoundSensorComponent.SoundState>,,SoundSensorComponent.SoundState> SimpleEventProvider<SoundSensorComponent.SoundState>
public class SoundSensorComponent
extends Component
implements DigitalEventProvider<SoundSensorComponent.SoundState>
Implementation of the CrowPi sound sensor using GPIO with Pi4J
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAll available states reported by the sound sensor component. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final longDebounce of input in microsecondsprotected static final intDefault Pin of sound sensorprotected final com.pi4j.io.gpio.digital.DigitalInputPi4J digital input instance used by this component -
Constructor Summary
ConstructorsConstructorDescriptionSoundSensorComponent(com.pi4j.context.Context pi4j) Creates a new sound sensor component using the default setup.SoundSensorComponent(com.pi4j.context.Context pi4j, int address, long debounce) Create sound sensor component with custom parameters -
Method Summary
Modifier and TypeMethodDescriptionprotected com.pi4j.io.gpio.digital.DigitalInputConfigbuildDigitalInputConfig(com.pi4j.context.Context pi4j, int address, long debounce) Configure Digital InputvoidAnalyzes the given value passed by an event and triggers 0-n simple events based on it.com.pi4j.io.gpio.digital.DigitalInputReturns the Pi4JDigitalInputassociated with this component.getState()Returns the SoundState of the sensorbooleanisNoisy()Reads the current sensor statebooleanisSilent()Reads the current sensor statemapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalStateto a well-knownSoundSensorComponent.SoundStatevoidonNoise(SimpleEventHandler handler) Sets or disables the handler for the onNoise event.voidonSilence(SimpleEventHandler handler) Sets or disables the handler for the onSilence event.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEventMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.pi4j.crowpi.components.events.DigitalEventProvider
addListener, removeListener
-
Field Details
-
digitalInput
protected final com.pi4j.io.gpio.digital.DigitalInput digitalInputPi4J digital input instance used by this component -
DEFAULT_PIN
protected static final int DEFAULT_PINDefault Pin of sound sensor- See Also:
-
DEFAULT_DEBOUNCE
protected static final long DEFAULT_DEBOUNCEDebounce of input in microseconds- See Also:
-
-
Constructor Details
-
SoundSensorComponent
public SoundSensorComponent(com.pi4j.context.Context pi4j) Creates a new sound sensor component using the default setup.- Parameters:
pi4j- Pi4J context
-
SoundSensorComponent
public SoundSensorComponent(com.pi4j.context.Context pi4j, int address, long debounce) Create sound sensor component with custom parameters- Parameters:
pi4j- Pi4J contextaddress- Custom BCM pin addressdebounce- Time to debounce the sound sensor in microseconds
-
-
Method Details
-
onNoise
Sets or disables the handler for the onNoise event. This event gets triggered whenever the sensor registers noise. Only a single event handler can be registered at once.- Parameters:
handler- Event handler to call or null to disable
-
isNoisy
public boolean isNoisy()Reads the current sensor state- Returns:
- Returns true if there is a noise currently registered by the sensor
-
isSilent
public boolean isSilent()Reads the current sensor state- Returns:
- Returns true if it is silent around the sensor
-
getState
Returns the SoundState of the sensor- Returns:
- Returns the well-known SoundState according to the current noise level
-
onSilence
Sets or disables the handler for the onSilence event. This event gets triggered whenever the sensor is no longer under noise Only a single event handler can be registered at once.- Parameters:
handler- Event handler to call or null to disable
-
getDigitalInput
public com.pi4j.io.gpio.digital.DigitalInput getDigitalInput()Returns the Pi4JDigitalInputassociated with this component.- Specified by:
getDigitalInputin interfaceDigitalEventProvider<SoundSensorComponent.SoundState>- Returns:
- Pi4J
DigitalInputinstance
-
mapDigitalState
public SoundSensorComponent.SoundState mapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalStateto a well-knownSoundSensorComponent.SoundState- Specified by:
mapDigitalStatein interfaceDigitalEventProvider<SoundSensorComponent.SoundState>- Parameters:
digitalState- Pi4J digital state to map- Returns:
- Mapped sound state
-
dispatchSimpleEvents
Analyzes the given value passed by an event and triggers 0-n simple events based on it. This method allows mapping various value/state changes to simple events. Must be attached usingDigitalEventProvider.addListener(EventHandler).- Specified by:
dispatchSimpleEventsin interfaceSimpleEventProvider<SoundSensorComponent.SoundState>- Parameters:
state- Event value
-
buildDigitalInputConfig
protected com.pi4j.io.gpio.digital.DigitalInputConfig buildDigitalInputConfig(com.pi4j.context.Context pi4j, int address, long debounce) Configure Digital Input- Parameters:
pi4j- PI4J Contextaddress- GPIO Address of sound sensor- Returns:
- Return Digital Input configuration
-