Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/ext-i2c

bm-ext MIT

Baremetal\I2c\Bus + \Device and the device-driver framework. Drivers are packages registering into the i2c_drivers registry; the platform side is an i2c-hal package.

bus: i2c

An I2C master for the PHP that runs on your board. Baremetal\I2c\Bus opens a bus on two pins and scans it, Baremetal\I2c\Device reads and writes one chip (raw bytes or registers), and device-driver packages add typed classes under Baremetal\I2c\Driver\ (for example Qmi8658) that can also sample in the background and raise events.

ext-i2c is platform-neutral: it requires the virtual php-baremetal/i2c-hal, and conductor adds the HAL for your SDK platform (php-baremetal/esp32-i2c-hal on ESP32). It also pulls in ext-events.

Install

In a project created with phpbm init:

bash
composer require php-baremetal/ext-i2c

Add a device driver the same way; a driver requires ext-i2c, so it is enough on its own:

bash
composer require php-baremetal/imu-qmi8658

It declares the board block i2c:i2c, so phpbm init and phpbm board offer it on boards with an I2C bus.

Example

php
<?php
use Baremetal\I2c\Bus;

$bus = new Bus(sda: 11, scl: 10);

foreach ($bus->scan() as $address => $info) {
    printf("0x%02X %s\n", $address, $info['driver'] ?? '(no driver)');
}

$chip = $bus->device(0x51);           // raw device, 100 kHz
$seconds = ord($chip->readReg(0x04)); // one register byte

Documentation

License

MIT.

Read the full documentation