Package com.diozero.api
Class SmoothedInputDevice
java.lang.Object
- All Implemented Interfaces:
DigitalInputDeviceInterface
,InputEventListener<DigitalInputEvent>
,java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
MotionSensor
public class SmoothedInputDevice extends WaitableDigitalInputDevice
Represents a generic input device which takes its value from the number of active events over a specific time period.
This class extends WaitableDigitalInputDevice
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).
-
Field Summary
-
Constructor Summary
Constructors Constructor Description SmoothedInputDevice(int gpio, GpioPullUpDown pud, int threshold, int eventAge, int eventDetectPeriod)
SmoothedInputDevice(GpioDeviceFactoryInterface deviceFactory, int gpio, GpioPullUpDown pud, int threshold, int eventAge, int eventDetectPeriod)
-
Method Summary
Modifier and Type Method Description int
getEventAge()
The time in milliseconds to keep items in the queue.int
getEventDetectPeriod()
How frequently (in milliseconds) to check the state of the queue.int
getThreshold()
If the number of on events younger than eventAge exceeds this amount, then 'isActive' will return 'True'.void
setEventAge(int eventAge)
Set the event age (milliseconds).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.void
valueChanged(DigitalInputEvent event)
Methods inherited from class com.diozero.api.WaitableDigitalInputDevice
disableDeviceListener, waitForActive, waitForActive, waitForInactive, waitForInactive, waitForValue
Methods inherited from class com.diozero.api.DigitalInputDevice
close, getPullUpDown, getTrigger, getValue, isActive, removeListener, setListener
Methods inherited from class com.diozero.api.AbstractDigitalInputDevice
enableDeviceListener, isActiveHigh, whenActivated, whenDeactivated
Methods inherited from class com.diozero.api.GpioInputDevice
addListener, removeAllListeners, removeListener
-
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
-
-
Method Details
-
valueChanged
- Specified by:
valueChanged
in interfaceInputEventListener<DigitalInputEvent>
- Overrides:
valueChanged
in classWaitableDigitalInputDevice
-
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)
-