Class Button

  • All Implemented Interfaces:
    DeviceInterface, DigitalInputDeviceInterface, DeviceEventConsumer<DigitalInputEvent>, AutoCloseable, Consumer<DigitalInputEvent>

    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("Event: {}", 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(nanoTime -> led::on);
       button.whenReleased(nanoTime -> led::off);
       Logger.info("Waiting for 10s - *** Press the button connected to pin {} ***", Integer.valueOf(buttonPin));
       SleepUtil.sleepSeconds(10);
     }