Class Button

All Implemented Interfaces:
DigitalInputDeviceInterface, InputEventListener<DigitalInputEvent>, java.io.Closeable, java.lang.AutoCloseable

public class Button
extends DigitalInputDevice

Provides push button related utility methods.

From the ButtonTest example:

Button
 
try (Button button = new Button(inputPin, GpioPullUpDown.PULL_UP)) {
	button.addListener(event -> Logger.debug("valueChanged({})", event));
	Logger.debug("Waiting for 10s - *** Press the button connected to input pin " + inputPin + " ***");
	SleepUtil.sleepSeconds(10);
}
 
 

Controlling an LED with a button ButtonControlledLed:

Button controlled LED
 
try (Button button = new Button(buttonPin, GpioPullUpDown.PULL_UP); LED led = new LED(ledPin)) {
	button.whenPressed(led::on);
	button.whenReleased(led::off);
	Logger.info("Waiting for 10s - *** Press the button connected to pin {} ***", Integer.valueOf(buttonPin));
	SleepUtil.sleepSeconds(10);
}
 
 
  • Constructor Details

  • Method Details

    • isPressed

      public boolean isPressed()
      Get the current state.
      Returns:
      Return true if the button is currently pressed.
    • isReleased

      public boolean isReleased()
      Get the current state.
      Returns:
      Return true if the button is currently released.
    • whenPressed

      public void whenPressed​(Action action)
      Action to perform when the button is pressed.
      Parameters:
      action - Action function to invoke.
    • whenReleased

      public void whenReleased​(Action action)
      Action to perform when the button is released.
      Parameters:
      action - Action function to invoke.