Class ButtonComponent

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

public class ButtonComponent extends Component implements DigitalEventProvider<ButtonComponent.ButtonState>
Implementation of the CrowPi buttons using GPIO with Pi4J
  • Field Details

    • DEFAULT_DEBOUNCE

      protected static final long DEFAULT_DEBOUNCE
      Default debounce time in microseconds
      See Also:
    • digitalInput

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

    • ButtonComponent

      public ButtonComponent(com.pi4j.context.Context pi4j, Button button)
      Creates a new button component using the default setup for the given CrowPi button.
      Parameters:
      pi4j - Pi4J context
      button - Desired CrowPi button
    • ButtonComponent

      public ButtonComponent(com.pi4j.context.Context pi4j, int address, boolean inverted, long debounce)
      Creates a new button component with custom GPIO address and debounce time.
      Parameters:
      pi4j - Pi4J context
      address - GPIO address of button
      inverted - Specify if button state is inverted
      debounce - Debounce time in microseconds
  • Method Details

    • getState

      public ButtonComponent.ButtonState getState()
      Returns the current state of the touch sensor
      Returns:
      Current button state
    • isDown

      public boolean isDown()
      Checks if button is currently pressed
      Returns:
      True if button is pressed
    • isUp

      public boolean isUp()
      Checks if button is currently depressed (= NOT pressed)
      Returns:
      True if button is depressed
    • mapDigitalState

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

      public void dispatchSimpleEvents(ButtonComponent.ButtonState 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<ButtonComponent.ButtonState>
      Parameters:
      state - Event value
    • onDown

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

      public void onUp(SimpleEventHandler handler)
      Sets or disables the handler for the onUp event. This event gets triggered whenever the button is no longer pressed. 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<ButtonComponent.ButtonState>
      Returns:
      Pi4J DigitalInput instance