ext-i2c · Recipe
Stream IMU samples as events
The executor samples the IMU in the background and raises SamplesReady; the event-driven runtime calls your handler, which drains and decodes the samples.
The QMI8658 on a Waveshare ESP32-S3-Touch-LCD-1.85B (SDA 11 / SCL 10), sampled at 10 Hz by the executor. Each
sample raises SamplesReady; the handler reads every sample collected since the last event.
The project
runtime is event-driven: the script registers its handlers and returns, then the reactor waits for events.
{
"name": "acme/imu-events",
"type": "project",
"repositories": [
{ "type": "composer", "url": "https://repository.php-baremetal.com", "canonical": true }
],
"require": {
"php-baremetal/conductor": "^1.0",
"php-baremetal/ext-events": "^1.0",
"php-baremetal/ext-i2c": "^1.0",
"php-baremetal/imu-qmi8658": "^1.0"
},
"config": {
"allow-plugins": { "php-baremetal/conductor": true },
"platform": { "php": "8.5.11" }
},
"extra": {
"baremetal": {
"sdk": "esp32@^2.0",
"board": "waveshare-esp32-s3-touch-lcd-1.85b",
"runtime": "event-driven",
"storage": "embedded"
}
}
}
ext-events would come in through ext-i2c anyway; requiring it states that the script uses Events directly.
The script
<?php
use Baremetal\Events;
use Baremetal\I2c\Bus;
use Baremetal\I2c\Driver\Qmi8658;
use Baremetal\Sensor\Imu\SamplesReady;
$imu = new Qmi8658(new Bus(sda: 11, scl: 10));
$seen = 0;
Events::listen(SamplesReady::class, function (SamplesReady $e) use (&$seen): void {
echo "{$e->count} samples ready\n"; // unread samples in the ring at delivery
foreach ($e->device->drain() as $raw) {
['accel' => [$ax, $ay, $az]] = $e->device->decode($raw);
printf("#%d accel [% .2f % .2f % .2f] g\n", ++$seen, $ax, $ay, $az);
}
});
$imu->poll(hz: 10, depth: 32, event: SamplesReady::class, every: 10);
echo "imu-events: sampling at 10 Hz\n";
How it works
poll()starts a sampler on the executor task: every 100 ms it reads 12 bytes from the chip into a ring of 32 samples, then posts aSamplesReadyevent carrying the device.poll()coalesces by default: while aSamplesReadyis still waiting, a new sample does not queue another one, so one event can cover several samples. The listener drains the ring withdrain(), so none is lost; passcoalesce: falsefor one event per sample.every: 10raises one event per 10 stored samples (once a second here);$e->countis the number of samples waiting in the ring when the event is delivered.poll()returns the device'sBaremetal\I2c\Poller:$poller->setHz(50)changes the rate later, andstop()/start()pause and resume the sampling (the unread samples stay fordrain()).- The reactor delivers the event on the PHP task;
$e->deviceis theQmi8658object. drain()returns the unread samples from the ring, oldest first, without touching the bus. If events were dropped because the queue was full, the nextdrain()still returns those samples.- The handler is typed on
SamplesReadyand only usesdrain()anddecode(), so it works for any polled driver whosedecode()returnsaccel.
With the power-save option on this still works on the default Bus::SYNC bus, but the 10 Hz sampler wakes the
chip ten times a second.
Related
- Background sampling and events PHP API
- Bus ownership and power-save Reference
- imu-qmi8658 Driver
- ext-events Extension