Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
[GPIO] Fixes debounce Item state out of sync (#5729) (#5730)
Browse files Browse the repository at this point in the history
* Fixes GPIO Binding Item state out of sync even with debounce set.

Fixes #5729

Signed-off-by: Ingo Schuck <[email protected]> (github: oktett-8)
  • Loading branch information
oktett-8 authored and kaikreuzer committed Dec 8, 2019
1 parent ed9ca23 commit 368c6e1
Showing 1 changed file with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public class GPIOPinLinux implements GPIOPin {
/**
* Initializes paths to special files exposed to user space by kernel
* GPIO framework.
*
* @param pinNumber the pin number as seen by the kernel
*
* @param pinNumber the pin number as seen by the kernel
* @param gpioPinDirectory path to pin directory in <code>sysfs</code>,
* e.g. "/sys/class/gpio/gpio1"
* e.g. "/sys/class/gpio/gpio1"
* @param debounceInterval default debounce interval
*/
public GPIOPinLinux(int pinNumber, String gpioPinDirectory, long debounceInterval) {
Expand Down Expand Up @@ -105,9 +105,9 @@ public GPIOPinLinux(int pinNumber, String gpioPinDirectory, long debounceInterva

/**
* Stops all spawned pin threads.
*
*
* @throws IOException if can't obtain pin lock in timely fashion
* or was interrupted while waiting for lock
* or was interrupted while waiting for lock
*/
public void stopEventProcessing() throws IOException {

Expand Down Expand Up @@ -597,9 +597,6 @@ public void run() {
ByteByReference value = new ByteByReference();
NativeLong zero = new NativeLong(0);

/* Last time (in milliseconds) when the interrupt was generated */
long lastInterruptTime = 0;

fd = LibC.INSTANCE.open(pin.valuePath.toString(), LibC.O_RDONLY | LibC.O_NONBLOCK);

pollfdset[0].fd = fd;
Expand Down Expand Up @@ -628,14 +625,15 @@ public void run() {
case 0:
continue;

/* There is one file descriptor ready */
/* There is one file descriptor ready */
case 1:
/* Is interrupt received? */
if ((pollfdset[0].revents & LibC.POLLPRI) > 0) {

/* Calculate times for software debounce */
long interruptTime = System.currentTimeMillis();
long timeDifference = interruptTime - lastInterruptTime;
/* Software debounce */
if (pin.debounceInterval > 0) {
Thread.sleep(pin.debounceInterval);
}

/* Go to file start and read first byte */
LibC.INSTANCE.lseek(fd, zero, LibC.SEEK_SET);
Expand All @@ -648,18 +646,11 @@ public void run() {
try {
if (pinLock.readLock().tryLock(PINLOCK_TIMEOUT, PINLOCK_TIMEOUT_UNITS)) {
try {

/* Software debounce */
if ((timeDifference > pin.debounceInterval) || (timeDifference < 0)) {

for (GPIOPinEventHandler eventHandler : pin.eventHandlers) {
EventHandlerExecutor eventHandlerExecutor = new EventHandlerExecutor(
pin, eventHandler,
Character.getNumericValue(value.getValue()));
executorService.execute(eventHandlerExecutor);
}

lastInterruptTime = interruptTime;
for (GPIOPinEventHandler eventHandler : pin.eventHandlers) {
EventHandlerExecutor eventHandlerExecutor = new EventHandlerExecutor(
pin, eventHandler,
Character.getNumericValue(value.getValue()));
executorService.execute(eventHandlerExecutor);
}
} finally {
pin.pinLock.readLock().unlock();
Expand Down

0 comments on commit 368c6e1

Please sign in to comment.