PHP code example of php-io-extensions / i2c

1. Go to this page and download the library: Download php-io-extensions/i2c library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

php-io-extensions / i2c example snippets


use Fd\FD;
use I2c\I2CUse;
use I2c\I2CConfig;

$fd = FD::open('/dev/i2c-1', 2); // O_RDWR = 2
// ... use $fd with I2CUse and I2CConfig methods ...
FD::close($fd);

use Fd\FD;
use I2c\I2CUse;
use I2c\I2CConfig;

$fd = FD::open('/dev/i2c-1', 2); // O_RDWR

I2CUse::slave($fd, 0x48);
I2CUse::write($fd, "\x00", 1);

$data = I2CUse::read($fd, 2);

FD::close($fd);

$fd = FD::open('/dev/i2c-1', 2);

$messages = [
    ['addr' => 0x48, 'flags' => 0x0000, 'data' => "\x00"],
    ['addr' => 0x48, 'flags' => 0x0001, 'data' => "\x00\x00"],
];

I2CConfig::rdwr($fd, $messages);

FD::close($fd);

$fd = FD::open('/dev/i2c-1', 2);

$funcs = I2CConfig::funcs($fd);
echo "Adapter funcs bitmask: {$funcs}\n";

FD::close($fd);

$fd = FD::open('/dev/i2c-1', 2);

I2CConfig::tenbit($fd, 1);
I2CUse::slave($fd, 0x1A2);
I2CUse::write($fd, "\x00", 1);
$data = I2CUse::read($fd, 4);

FD::close($fd);