Module diozero.core

Class SmoothedInputDevice

All Implemented Interfaces:
DeviceInterface, DigitalInputDeviceInterface, DeviceEventConsumer<DigitalInputEvent>, AutoCloseable, Runnable, Consumer<DigitalInputEvent>
Direct Known Subclasses:
MotionSensor

public class SmoothedInputDevice extends DigitalInputDevice implements Runnable

Represents a generic input device which takes its value from the number of active events over a specific time period.

This class extends DigitalInputDevice with a queue which is added to whenever the input device is active. The number of the active events in the queue is compared to a threshold which is used to determine the state of the 'active' property.

Any active events over the specified eventAge are removed by a background thread.

This class is intended for use with devices which exhibit "twitchy" behaviour (such as certain motion sensors). It can enable basic debounce functionality by setting threshold to 1, eventAge = -1 and eventDetectPeriod = debounceTime (ms).

  • Constructor Details

    • SmoothedInputDevice

      public SmoothedInputDevice(int gpio, GpioPullUpDown pud, int threshold, int eventAge, int eventDetectPeriod) throws RuntimeIOException
      Parameters:
      gpio - GPIO to which the device is connected.
      pud - Pull up/down configuration, values: NONE, PULL_UP, PULL_DOWN.
      threshold - The value above which the device will be considered "on".
      eventAge - The time in milliseconds to keep active events in the queue.
      eventDetectPeriod - How frequently to check for events.
      Throws:
      RuntimeIOException - if an I/O error occurs
    • SmoothedInputDevice

      public SmoothedInputDevice(GpioDeviceFactoryInterface deviceFactory, int gpio, GpioPullUpDown pud, int threshold, int eventAge, int eventDetectPeriod) throws RuntimeIOException
      Parameters:
      deviceFactory - Device factory to use to provision this device.
      gpio - GPIO to which the device is connected.
      pud - Pull up/down configuration, values: NONE, PULL_UP, PULL_DOWN.
      threshold - The value above which the device will be considered "on".
      eventAge - The time in milliseconds to keep active events in the queue.
      eventDetectPeriod - How frequently to check for events.
      Throws:
      RuntimeIOException - if an I/O error occurs
    • SmoothedInputDevice

      public SmoothedInputDevice(GpioDeviceFactoryInterface deviceFactory, PinInfo pinInfo, GpioPullUpDown pud, boolean activeHigh, int threshold, int eventAge, int eventDetectPeriod) throws RuntimeIOException
      Parameters:
      deviceFactory - Device factory to use to provision this device
      pinInfo - PinInfo instance to which the device is connected
      pud - Pull up/down configuration, values: NONE, PULL_UP, PULL_DOWN
      activeHigh - Set to true if digital 1 is to be treated as active
      threshold - The value above which the device will be considered "on"
      eventAge - The time in milliseconds to keep active events in the queue
      eventDetectPeriod - How frequently to check for events
      Throws:
      RuntimeIOException - if an I/O error occurs
  • Method Details

    • accept

      public void accept(DigitalInputEvent event)
      Specified by:
      accept in interface Consumer<DigitalInputEvent>
      Overrides:
      accept in class AbstractDigitalInputDevice
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • getThreshold

      public int getThreshold()
      If the number of on events younger than eventAge exceeds this amount, then 'isActive' will return 'True'.
      Returns:
      event threshold
    • setThreshold

      public void setThreshold(int threshold)
      Set the threshold value in terms of number of on events within the specified time period that will trigger an on event to any listeners.
      Parameters:
      threshold - New threshold value.
    • getEventAge

      public int getEventAge()
      The time in milliseconds to keep items in the queue.
      Returns:
      The event age (milliseconds).
    • setEventAge

      public void setEventAge(int eventAge)
      Set the event age (milliseconds).
      Parameters:
      eventAge - New event age value (milliseconds).
    • getEventDetectPeriod

      public int getEventDetectPeriod()
      How frequently (in milliseconds) to check the state of the queue.
      Returns:
      The event detection period (milliseconds)