Class TouchSensorComponent

java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.TouchSensorComponent
All Implemented Interfaces:
DigitalEventProvider<TouchSensorComponent.TouchState>, EventProvider<DigitalEventListener<TouchSensorComponent.TouchState>,TouchSensorComponent.TouchState>, SimpleEventProvider<TouchSensorComponent.TouchState>

public class TouchSensorComponent extends Component implements DigitalEventProvider<TouchSensorComponent.TouchState>
Implementation of the CrowPi touch 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 touch sensor
      See Also:
    • DEFAULT_DEBOUNCE

      protected static final long DEFAULT_DEBOUNCE
      Debounce of input in microseconds
      See Also:
  • Constructor Details

    • TouchSensorComponent

      public TouchSensorComponent(com.pi4j.context.Context pi4j)
      Creates a new touch sensor component using the default setup.
      Parameters:
      pi4j - Pi4J context
    • TouchSensorComponent

      public TouchSensorComponent(com.pi4j.context.Context pi4j, int address, long debounce)
      Create touch sensor component with custom parameters
      Parameters:
      pi4j - Pi4J context
      address - GPIO Pin of Raspberry
      debounce - Time in Microseconds to debounce input
  • Method Details

    • getState

      Returns the current state of the touch sensor
      Returns:
      Current touch sensor state
    • mapDigitalState

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

      public boolean isTouched()
      Method to check if touch sensor is currently touched
      Returns:
      True if touch sensor is currently touched
    • dispatchSimpleEvents

      public void dispatchSimpleEvents(TouchSensorComponent.TouchState 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<TouchSensorComponent.TouchState>
      Parameters:
      state - Event value
    • onTouch

      public void onTouch(SimpleEventHandler handler)
      Sets or disables the handler for the onTouch event. This event gets triggered whenever the sensor is touched. Only a single event handler can be registered at once.
      Parameters:
      handler - Event handler to call or null to disable
    • onRelease

      public void onRelease(SimpleEventHandler handler)
      Sets or disables the handler for the onRelease event. This event gets triggered whenever the sensor is no longer touched. 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<TouchSensorComponent.TouchState>
      Returns:
      Pi4J DigitalInput instance
    • 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 touch sensor
      debounce - debounce time in microseconds
      Returns:
      Return Digital Input configuration