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
Modifier and TypeClassDescriptionstatic enum
All available states reported by the sound sensor component. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final long
Debounce of input in microsecondsprotected static final int
Default Pin of sound sensorprotected final com.pi4j.io.gpio.digital.DigitalInput
Pi4J digital input instance used by this component -
Constructor Summary
ConstructorDescriptionSoundSensorComponent
(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.DigitalInputConfig
buildDigitalInputConfig
(com.pi4j.context.Context pi4j, int address, long debounce) Configure Digital Inputvoid
Analyzes the given value passed by an event and triggers 0-n simple events based on it.com.pi4j.io.gpio.digital.DigitalInput
Returns the Pi4JDigitalInput
associated with this component.getState()
Returns the SoundState of the sensorboolean
isNoisy()
Reads the current sensor stateboolean
isSilent()
Reads the current sensor statemapDigitalState
(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalState
to a well-knownSoundSensorComponent.SoundState
void
onNoise
(SimpleEventHandler handler) Sets or disables the handler for the onNoise event.void
onSilence
(SimpleEventHandler handler) Sets or disables the handler for the onSilence event.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEvent
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 Pi4JDigitalInput
associated with this component.- Specified by:
getDigitalInput
in interfaceDigitalEventProvider<SoundSensorComponent.SoundState>
- Returns:
- Pi4J
DigitalInput
instance
-
mapDigitalState
public SoundSensorComponent.SoundState mapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalState
to a well-knownSoundSensorComponent.SoundState
- Specified by:
mapDigitalState
in 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:
dispatchSimpleEvents
in 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
-