Download the PHP package php-io-extensions/i2c without Composer
On this page you can find all versions of the php package php-io-extensions/i2c. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package i2c
I2C
PHP-controllable Linux I²C extension built with Zephir.
The I2C extension allows PHP to communicate with I²C devices on Linux — selecting slave addresses, performing raw reads and writes, and executing combined transactions. Useful for sensors, displays, ADCs, and other hardware connected to an I²C bus.
Requirements
- PHP 8.3+ with development headers (
php-dev/php-devel) - Zephir 0.19+
- FD extension — provides raw integer file descriptors via
Fd\FD::open() - Linux with I²C userspace support (
/dev/i2c-N) i2c-devkernel module loaded
Installation
Install Zephir if you haven't already:
Install the FD extension first — I2C requires raw integer file descriptors that PHP's stream layer cannot provide:
Then clone and build this extension:
The install script handles the full workflow: ensures linux/i2c-dev.h headers are present, loads the i2c-dev kernel module, clean → build → copy .so → write 30-i2c.ini into all detected conf.d directories → verify php -m → reload php-fpm if present.
To use a specific Zephir binary:
API
All methods are static. File descriptors are plain integers. Use the FD extension to open and close them — PHP's stream-based fopen does not produce the raw integer FDs the kernel ioctls require.
I2c\I2CUse
Basic I/O operations on an I²C bus file descriptor.
slave(int $fd, int $address): int
Selects the slave device to communicate with. Must be called before read() or write(). Returns 0 on success, -1 on failure.
read(int $fd, int $bytes_to_read): string
Reads raw bytes from the currently selected slave. Returns the bytes as a binary string, or "" on error.
write(int $fd, string $data, int $bytes_to_write): int
Writes raw bytes to the currently selected slave. Returns the number of bytes written, or -1 on failure.
I2c\I2CConfig
Configuration and advanced transaction operations on an I²C bus file descriptor.
tenbit(int $fd, int $enable): int
Enables (1) or disables (0) 10-bit slave addressing. Returns 0 on success, -1 on failure.
pec(int $fd, int $enable): int
Enables (1) or disables (0) SMBus Packet Error Checking. Returns 0 on success, -1 on failure.
funcs(int $fd): int
Returns the adapter's functionality bitmask, or -1 on failure. Compare against I2C_FUNC_* values from linux/i2c.h.
rdwr(int $fd, array $messages): int
Performs a combined read/write transaction in a single atomic ioctl. Returns the number of messages transferred on success, or -1 on failure.
Each element of $messages is an associative array:
| Key | Type | Description |
|---|---|---|
addr |
int |
Slave address |
flags |
int |
I2C_M_* flags — 0 = write, 0x0001 = read |
data |
string |
Raw bytes; length determines bytes transferred |
Usage
I2C_M flag values for rdwr. PHP does not define these — pass the values directly or define your own constants:
| Constant | Value | Description |
|---|---|---|
I2C_M_WR |
0x0000 |
Write to slave |
I2C_M_RD |
0x0001 |
Read from slave |
I2C_M_TEN |
0x0010 |
Use 10-bit addressing |
I2C_M_NOSTART |
0x4000 |
Skip repeated START condition |
Basic read/write
Combined transaction (write then read, no STOP)
Query adapter capabilities
10-bit addressing
License
Copyright © Project Saturn Studios, LLC. All rights reserved.