Skip to content

Commit 4067a43

Browse files
committed
Updates
1 parent 80901d3 commit 4067a43

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ This creates `app/MyPi.php`.
110110

111111
```php
112112
use Flat3\RevPi\Interfaces\Module;
113+
use Flat3\RevPi\Interfaces\SerialPort;
113114
use Flat3\RevPi\Led\LedColour;
114115
use Flat3\RevPi\Led\LedPosition;
115116
use Flat3\RevPi\Monitors\DigitalMonitor;
@@ -124,8 +125,8 @@ This creates `app/MyPi.php`.
124125
$port = $pi->serialPort();
125126
$port->setSpeed(BaudRate::B576000);
126127
$port->clearFlag(LocalFlag::CanonicalInput);
127-
$port->onReadable(function (string $data) use ($port) {
128-
$port->write($data);
128+
$port->onReadable(function (SerialPort $port) {
129+
$port->write($port->read(1024));
129130
});
130131

131132
// Monitor the core temperature, writing updated values to the serial port

src/Hardware/Remote/RemoteCharacterDevice.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,26 @@ public function fdopen(): mixed
4040
{
4141
$this->device->request('fdopen')->await();
4242

43-
$this->device->on('readable', function (string $payload) {
43+
$this->device->on('data', function (string $payload) {
4444
fwrite($this->remote, $payload);
4545
});
4646

4747
EventLoop::onReadable($this->remote, function ($callbackId, $stream) {
48-
$data = @fread($stream, Constants::BlockSize);
48+
while (true) {
49+
$data = @fread($stream, Constants::BlockSize);
50+
51+
if ($data === false || $data === '') {
52+
break;
53+
}
4954

50-
if (is_string($data) && $data !== '') {
5155
$this->write($data, strlen($data));
52-
} elseif (! is_resource($stream) || @feof($stream)) {
56+
57+
if (strlen($data) < Constants::BlockSize) {
58+
break;
59+
}
60+
}
61+
62+
if (! is_resource($stream) || @feof($stream)) {
5363
EventLoop::cancel($callbackId);
5464
}
5565
});

src/Interfaces/SerialPort.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function getStopBits(): StopBits;
112112
/**
113113
* Register a callback to be invoked when the port is readable.
114114
*
115-
* @param callable $callback Callback to invoke on readable event.
115+
* @param callable(SerialPort $port): void $callback Callback to invoke on readable event.
116116
* @return string An ID or reference for the registered callback.
117117
*/
118118
public function onReadable(callable $callback): string;

src/Rpc/RpcDevice.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,30 @@ protected function invoke(string $method, array $params): mixed
139139
$stream = $this->device->fdopen();
140140

141141
EventLoop::onReadable($stream, function ($callbackId, $stream) {
142-
$data = @fread($stream, Constants::BlockSize);
142+
while (true) {
143+
$data = @fread($stream, Constants::BlockSize);
144+
145+
if (!is_string($data) || $data === '') {
146+
break;
147+
}
143148

144-
if (is_string($data) && $data !== '') {
145149
$event = new Event;
146-
$event->type = 'readable';
150+
$event->type = 'data';
147151
$event->payload = $data;
148152

149153
try {
150154
$this->socket->sendBinary(serialize($event));
151155
} catch (WebsocketClosedException) {
152156
EventLoop::cancel($callbackId);
157+
break;
158+
}
159+
160+
if (strlen($data) < Constants::BlockSize) {
161+
break;
153162
}
154-
} elseif (! is_resource($stream) || @feof($stream)) {
163+
}
164+
165+
if (!is_resource($stream) || @feof($stream)) {
155166
EventLoop::cancel($callbackId);
156167
}
157168
});

src/Rpc/RpcHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ abstract class RpcHandler
2727
protected WebsocketClient $socket;
2828

2929
/**
30-
* @var array{readable: array<callable>}
30+
* @var array{data: array<callable>}
3131
*/
3232
protected array $callbacks = [
33-
'readable' => [],
33+
'data' => [],
3434
];
3535

3636
/**

src/SerialPort/SerialPort.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getDevice(): Terminal
3434
public function onReadable(callable $callback): string
3535
{
3636
return EventLoop::onReadable($this->stream, function () use ($callback) {
37-
$callback(@fread($this->stream, Constants::BlockSize));
37+
$callback($this);
3838
});
3939
}
4040

tests/Virtual/SerialPortTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public function test_read(): void
2121
->setSpeed(BaudRate::B9600)
2222
->setFlag(RS485Flag::TerminateBus);
2323

24-
$port->onReadable(function ($text) use (&$capture) {
25-
$capture .= $text;
24+
$port->onReadable(function (SerialPort $port) use (&$capture) {
25+
$capture .= $port->read(8192);
2626
});
2727

2828
$device = app(VirtualTerminalDevice::class);

0 commit comments

Comments
 (0)