定制 tasoft/php-i2c-extension 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tasoft/php-i2c-extension

Composer 安装命令:

composer require tasoft/php-i2c-extension

包简介

A simple I2C extension for PHP to read from and write to i2c bus.

README 文档

README

I've created this extension to get access to the i2c bus on my raspberry pi.

Prerequisites

  • The development module for your version of PHP, i.e, php7.4-dev, php8.2-dev.

Installation

Change the configure file on line 5 (php-config7.4), to match your installed version of PHP. Then, run the following commands to install the extension.

$ cd ~
$ git clone https://github.com/tasoftch/php-i2c-extension.git
$ cd php-i2c-extension
$ phpize
$ ./configure --with-php-config=/usr/bin/php-config7.4 --enable-php-i2c
$ make
$ sudo make install

Next, find the location of PHP's INI files on your computer by running the following command.

$ php --ini | grep -i "Configuration File.*Path"

Note: If you're using Microsoft Windows, then run php --ini and look for the value of "Configuration File (php.ini) Path:".

You should see a directory path such as /etc/php/7.4/cli. In that directory, create a new INI file named 20-i2c.ini with the following contents

; configuration for php i2c module
extension=php_i2c

Then, with the file created, test that the I2C extension is loaded by running the following command:

php --ri php_i2c

You should see the following output:

php_i2c

Version => 0.8.0

Usage

The extension adds five function to the global scope:

  1. i2c_open
    This opens the device bus.
  2. i2c_select
    This selects an address of a connected chip.
  3. i2c_read i2c_read_byte i2c_read_2_bytes i2c_read_3_bytes i2c_read_4_bytes
    Reads data from the i2c bus.
  4. i2c_write i2c_write_byte i2c_write_2_bytes i2c_write_3_bytes i2c_write_4_bytes
    Writes data to the i2c bus
  5. i2c_close
    Closes the bus.

Example

I've tested with a Raspberry Pi Model B 3+ and the Adafruit ADS1115 analog to digital converter. It's default i2c address is 0x48.

<?php
$fd = i2c_open("/dev/i2c-1");
i2c_select($fd, 0x48);

for($e=0;$e<30;$e++) {
    // Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s
    i2c_write($fd, 1, [0xc3, 0x85]);
    // or
    // i2c_write_2_bytes( 0x01c385 );
    
    // Wait for conversion completed
    usleep(9000);
    i2c_write($fd, 0);
    $data = i2c_read($fd, 2);

    $value = $data[0]*256 + $data[1];
    printf("Hex: 0x%02x%02x - Int: %d - Float, converted: %f V\n",
        $data[0], $data[1], $value, (float)$value*4.096/32768.0);

    usleep(500000);
}

i2c_close($fd);

Usage PHP

The package also contains a php wrapper class for i2c.

$ composer require tasoft/php-i2c-extension

Please note that the composer installation does not compile the extension! For compilation use the installation guide described before.

Now the same example can be rewritten as:

<?php
use TASoft\Bus\I2C;

$i2c = new I2C(0x48, 1);
for($e=0;$e<30;$e++) {
    // Read for 30 times the value between channel AIN_0 and GND, 4.096 V, 128 samples/s
    $i2c->write16(1, 0xC385);
    // Wait for conversion completed
    usleep(9000);
    $value = $i2c->readRegister16(1);

    printf("Hex: 0x%04x - Int: %d - Float, converted: %f V\n",
        $value, $value, (float)$value*4.096/32768.0);

    usleep(500000);
}

tasoft/php-i2c-extension 适用场景与选型建议

tasoft/php-i2c-extension 是一款 基于 C 开发的 Composer 扩展包,目前已累计 117 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tasoft/php-i2c-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tasoft/php-i2c-extension 我们能提供哪些服务?
定制开发 / 二次开发

基于 tasoft/php-i2c-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 117
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 3
  • 开发语言: C

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-18