java.lang.Object
com.diozero.api.GpioDevice
com.diozero.api.GpioInputDevice<DigitalInputEvent>
com.diozero.api.AbstractDigitalInputDevice
com.diozero.api.DigitalInputDevice
com.diozero.api.SmoothedInputDevice
- All Implemented Interfaces:
DeviceInterface
,DigitalInputDeviceInterface
,DeviceEventConsumer<DigitalInputEvent>
,AutoCloseable
,Runnable
,Consumer<DigitalInputEvent>
- Direct Known Subclasses:
MotionSensor
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).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Smoothed input device builder. -
Field Summary
Fields inherited from class com.diozero.api.AbstractDigitalInputDevice
activeHigh
Fields inherited from class com.diozero.api.GpioDevice
pinInfo
-
Constructor Summary
ConstructorDescriptionSmoothedInputDevice
(int gpio, GpioPullUpDown pud, int threshold, int eventAge, int eventDetectPeriod) SmoothedInputDevice
(GpioDeviceFactoryInterface deviceFactory, int gpio, GpioPullUpDown pud, int threshold, int eventAge, int eventDetectPeriod) SmoothedInputDevice
(GpioDeviceFactoryInterface deviceFactory, PinInfo pinInfo, GpioPullUpDown pud, boolean activeHigh, int threshold, int eventAge, int eventDetectPeriod) -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(DigitalInputEvent event) int
The time in milliseconds to keep items in the queue.int
How frequently (in milliseconds) to check the state of the queue.int
If the number of on events younger than eventAge exceeds this amount, then 'isActive' will return 'True'.void
run()
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.Methods inherited from class com.diozero.api.DigitalInputDevice
close, getName, getPullUpDown, getTrigger, getValue, isActive, removeListener, setListener
Methods inherited from class com.diozero.api.AbstractDigitalInputDevice
disableDeviceListener, enableDeviceListener, isActiveHigh, waitForActive, waitForActive, waitForInactive, waitForInactive, waitForValue, whenActivated, whenDeactivated
Methods inherited from class com.diozero.api.GpioInputDevice
addListener, hasListeners, removeAllListeners, removeListener
Methods inherited from class com.diozero.api.GpioDevice
getGpio
-
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 devicepinInfo
- PinInfo instance to which the device is connectedpud
- Pull up/down configuration, values: NONE, PULL_UP, PULL_DOWNactiveHigh
- Set to true if digital 1 is to be treated as activethreshold
- The value above which the device will be considered "on"eventAge
- The time in milliseconds to keep active events in the queueeventDetectPeriod
- How frequently to check for events- Throws:
RuntimeIOException
- if an I/O error occurs
-
-
Method Details
-
accept
- Specified by:
accept
in interfaceConsumer<DigitalInputEvent>
- Overrides:
accept
in classAbstractDigitalInputDevice
-
run
public void run() -
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)
-