Class SoundSensorComponent

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
  • Field Details

    • digitalInput

      protected final com.pi4j.io.gpio.digital.DigitalInput digitalInput
      Pi4J digital input instance used by this component
    • DEFAULT_PIN

      protected static final int DEFAULT_PIN
      Default Pin of sound sensor
      See Also:
    • DEFAULT_DEBOUNCE

      protected static final long DEFAULT_DEBOUNCE
      Debounce 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 context
      address - Custom BCM pin address
      debounce - Time to debounce the sound sensor in microseconds
  • Method Details

    • onNoise

      public void onNoise(SimpleEventHandler handler)
      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

      public void onSilence(SimpleEventHandler handler)
      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 Pi4J DigitalInput associated with this component.
      Specified by:
      getDigitalInput in interface DigitalEventProvider<SoundSensorComponent.SoundState>
      Returns:
      Pi4J DigitalInput instance
    • mapDigitalState

      public SoundSensorComponent.SoundState mapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState)
      Maps a DigitalState to a well-known SoundSensorComponent.SoundState
      Specified by:
      mapDigitalState in interface DigitalEventProvider<SoundSensorComponent.SoundState>
      Parameters:
      digitalState - Pi4J digital state to map
      Returns:
      Mapped sound state
    • dispatchSimpleEvents

      public void dispatchSimpleEvents(SoundSensorComponent.SoundState state)
      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 using DigitalEventProvider.addListener(EventHandler).
      Specified by:
      dispatchSimpleEvents in interface SimpleEventProvider<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 Context
      address - GPIO Address of sound sensor
      Returns:
      Return Digital Input configuration