Skip to content

Commit 899eb4e

Browse files
committed
Updates
1 parent 87b5701 commit 899eb4e

13 files changed

+82
-36
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ $pi->output1()->reset();
170170

171171
### Monitoring IO Variables
172172

173-
The `DigitalMonitor` will cause the callback to be called whenever the monitored value changes.
173+
The monitor will cause the callback to be called whenever the monitored value changes.
174174

175175
```php
176176
use Flat3\RevPi\Monitors\DigitalMonitor;
@@ -183,6 +183,27 @@ $pi->input1()->monitor(new DigitalMonitor, function($newValue) {
183183
});
184184
```
185185

186+
#### Monitor::debounce
187+
188+
Wraps a callback to fire **only after changes stop** for the given interval (in milliseconds).
189+
190+
```php
191+
$pi->input1()->monitor(new DigitalMonitor, Monitor::debounce(function($value) {
192+
// Handle change after quiet period
193+
}, 200));
194+
```
195+
196+
#### Monitor::throttle
197+
198+
Wraps a callback to fire **at most once per interval** (in milliseconds).
199+
200+
```php
201+
$pi->input1()->monitor(new DigitalMonitor, Monitor::throttle(function($value) {
202+
// Handle change no more than every 200ms
203+
}, 200));
204+
```
205+
206+
186207
### Process Image (Low-Level)
187208

188209
Get the raw process image interface for advanced access:
@@ -309,13 +330,13 @@ $port = app(\Flat3\RevPi\Interfaces\SerialPort::class); // Usually you want $pi-
309330

310331
### Monitoring with Custom Monitors
311332

312-
If you want to create a custom monitor (beyond DigitalMonitor):
333+
If you want to create a custom monitor:
313334

314335
```php
315-
use Flat3\RevPi\Interfaces\Monitor;
336+
use Flat3\RevPi\Monitors\Monitor;
316337

317-
class MyMonitor implements Monitor {
318-
public function evaluate(int|bool|null $next): bool {
338+
class MyMonitor extends Monitor {
339+
public function evaluate(int|bool $next): bool {
319340
// Implement custom transition/action logic here
320341
// e.g. if crossing a threshold
321342
// Return true if the monitor has detected sufficient change

src/IO/IO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Flat3\RevPi\IO;
66

77
use Flat3\RevPi\Interfaces\Module;
8-
use Flat3\RevPi\Interfaces\Monitor;
8+
use Flat3\RevPi\Monitors\Monitor;
99

1010
abstract class IO
1111
{

src/Interfaces/Module.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Flat3\RevPi\Interfaces;
66

77
use Flat3\RevPi\Led\LedPosition;
8+
use Flat3\RevPi\Monitors\Monitor;
89

910
/**
1011
* Interface Module

src/Interfaces/Monitor.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Modules/Module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use Flat3\RevPi\Constants;
88
use Flat3\RevPi\Events\PollingEvent;
99
use Flat3\RevPi\Interfaces\Module as ModuleInterface;
10-
use Flat3\RevPi\Interfaces\Monitor;
1110
use Flat3\RevPi\Interfaces\ProcessImage;
1211
use Flat3\RevPi\Interfaces\SerialPort;
12+
use Flat3\RevPi\Monitors\Monitor;
1313
use Illuminate\Support\Facades\Event;
1414
use Revolt\EventLoop;
1515

src/Monitors/DeadbandMonitor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
namespace Flat3\RevPi\Monitors;
66

77
use Flat3\RevPi\Exceptions\NotSupportedException;
8-
use Flat3\RevPi\Interfaces\Monitor;
98

10-
class DeadbandMonitor implements Monitor
9+
class DeadbandMonitor extends Monitor
1110
{
1211
protected bool $wasOutside = false;
1312

src/Monitors/DigitalMonitor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Flat3\RevPi\Monitors;
66

7-
use Flat3\RevPi\Interfaces\Monitor;
8-
9-
class DigitalMonitor implements Monitor
7+
class DigitalMonitor extends Monitor
108
{
119
protected int|bool|null $previous = null;
1210

src/Monitors/Monitor.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flat3\RevPi\Monitors;
6+
7+
use Revolt\EventLoop;
8+
9+
abstract class Monitor
10+
{
11+
abstract public function evaluate(int|bool $next): bool;
12+
13+
public static function debounce(callable $callback, int $milliseconds): callable
14+
{
15+
$timerId = null;
16+
17+
return function (...$args) use (&$timerId, $callback, $milliseconds) {
18+
if ($timerId !== null) {
19+
EventLoop::cancel($timerId);
20+
}
21+
22+
$timerId = EventLoop::delay($milliseconds / 1000, function () use (&$timerId, $callback, $args) {
23+
$callback(...$args);
24+
$timerId = null;
25+
});
26+
};
27+
}
28+
29+
public static function throttle(callable $callback, int $milliseconds): callable
30+
{
31+
$throttling = false;
32+
33+
return function (...$args) use (&$throttling, $callback, $milliseconds) {
34+
if ($throttling) {
35+
return;
36+
}
37+
38+
$throttling = true;
39+
$callback(...$args);
40+
41+
EventLoop::delay($milliseconds / 1000, function () use (&$throttling) {
42+
$throttling = false;
43+
});
44+
};
45+
}
46+
}

src/Monitors/MovingAverageMonitor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Flat3\RevPi\Monitors;
66

7-
use Flat3\RevPi\Interfaces\Monitor;
8-
9-
class MovingAverageMonitor implements Monitor
7+
class MovingAverageMonitor extends Monitor
108
{
119
/**
1210
* @var array<int|bool>

src/Monitors/RangeMonitor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Flat3\RevPi\Monitors;
44

5-
use Flat3\RevPi\Interfaces\Monitor;
6-
7-
class RangeMonitor implements Monitor
5+
class RangeMonitor extends Monitor
86
{
97
public function __construct(protected int|float $min, protected int|float $max) {}
108

0 commit comments

Comments
 (0)