Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ $pin->setEdge(InputPinInterface::EDGE_BOTH);
// Create an interrupt watcher
$interruptWatcher = $gpio->createWatcher();

// Register a callback to be triggered on pin interrupts
// Register a callback to be triggered on pin interrupts with a 500 millisecond debounce delay
$interruptWatcher->register($pin, function (InputPinInterface $pin, $value) {
echo 'Pin ' . $pin->getNumber() . ' changed to: ' . $value . PHP_EOL;

// Returning false will make the watcher return false immediately
return true;
});
}, 500);

// Watch for interrupts, timeout after 5000ms (5 seconds)
while ($interruptWatcher->watch(5000));
Expand Down
19 changes: 18 additions & 1 deletion src/Interrupt/InterruptWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class InterruptWatcher implements InterruptWatcherInterface
private $streams;
private $pins;
private $callbacks;
private $delays;
private $lastCallbacks;

/**
* Constructor.
Expand Down Expand Up @@ -40,7 +42,7 @@ public function __destruct()
/**
* {@inheritdoc}
*/
public function register(InputPinInterface $pin, callable $callback)
public function register(InputPinInterface $pin, callable $callback, int $delay = 0)
{
$pinNumber = $pin->getNumber();

Expand All @@ -54,6 +56,8 @@ public function register(InputPinInterface $pin, callable $callback)

$this->pins[$pinNumber] = $pin;
$this->callbacks[$pinNumber] = $callback;
$this->debounceDelays[$pinNumber] = (float)$delay / 1000;
$this->lastCallbackMicrotimes[$pinNumber] = 0;
}

/**
Expand All @@ -68,6 +72,7 @@ public function unregister(InputPinInterface $pin)

unset($this->streams[$pinNumber]);
unset($this->callbacks[$pinNumber]);
unset($this->debounceDelays[$pinNumber]);
unset($this->pins[$pinNumber]);
}
}
Expand All @@ -81,6 +86,8 @@ public function watch($timeout)
$carry = $timeout - ($seconds * 1000);
$micro = $carry * 1000;

$microtimeNow = microtime(true);

$read = $write = [];
$except = $this->streams;

Expand All @@ -103,6 +110,16 @@ public function watch($timeout)
}

foreach ($triggers as $pinNumber => $value) {
if ($this->debounceDelays[$pinNumber] !== 0
&& $this->lastCallbackMicrotimes[$pinNumber] !== 0
&& ($microtimeNow - $this->lastCallbackMicrotimes[$pinNumber] < $this->debounceDelays[$pinNumber])
) {

return true;
}

$this->lastCallbackMicrotimes[$pinNumber] = $microtimeNow;

if (false === call_user_func($this->callbacks[$pinNumber], $this->pins[$pinNumber], $value)) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Interrupt/InterruptWatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ interface InterruptWatcherInterface
*
* @param InputPinInterface $pin
* @param callable $callback
* @param integer $delay time in ms between callbacks
*/
public function register(InputPinInterface $pin, callable $callback);
public function register(InputPinInterface $pin, callable $callback, int $delay = 0);

/**
* Unregister a pin callback.
Expand Down