tgeindre/php-gpio 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tgeindre/php-gpio

Composer 安装命令:

composer require tgeindre/php-gpio

包简介

GPIO-related utils & toolkit PHP library

README 文档

README

A simple library to read/write Raspberry PI GPIOs with PHP.

Forked from ronanguilloux/php-gpio and tested with Atoum.

Install

This library can be installed using Composer:

$ composer require tgeindre/php-gpio

Read/Write Raspberry Pi GPIOs

To read/write Raspberry Pi GPIOs, use the PhpGpio\Gpio class. The instanciation of this class requires an array of pins numbers you will use.

You can define a specific list of pins numbers, according to your usage and your Rasberry Pi version, or you can use the PhpGpio\Utils\Pi class to automaticly find all availables pins:

<?php
namespace myproject;

$pi = new PhpGpio\Utils\Pi;
$pi->getAvailablePins(); // int array

Accessing to the GPIOs requires root permissions, so make sure your code is running with enought permissions. Remember: you should never run your webserver as root.

Here is a simple example of Gpio class usage:

<?php
namespace MyProject;

use PhpGpio\GpioInterface;
use PhpGpio\Gpio;

// Both pins are available on all raspi versions
define('PIN_IN', 4);
define('PIN_OUT', 7);

$gpio = new Gpio([PIN_IN, PIN_OUT]);

// First, setup pins with correct directions
$gpio
     ->setup(PIN_IN, GpioInterface::DIRECTION_IN) // Makes it readable
     ->setup(PIN_OUT, GpioInterface::DIRECTION_OUT) // Writeable
;

// read PIN_IN value and display it
$value = $gpio->read(PIN_IN);
var_dump($value); // string

// write 1 on PIN_OUT
$gpio->write(PIN_OUT, GpioInterface::IO_VALUE_ON);
sleep(1);

// After 1 second, write 0 on PIN_OUT
$gpio->write(PIN_OUT, GpioInterface::IO_VALUE_OFF);

// Then free all pins
// (use the unexport() method to free pins one by one)
$gpio->unexportAll();

Check this page if you need a complete list of availables pins on your Raspberry Pi version.

Demo

Here is a simple demo using a Raspberry Pi 3 and some LED (click on the image to see the video):

Demo video

Source code

Sensors

Currently, this library only supports MCP analogic to digital converter (ADC) with 4 or 8 channels:

MCP3002, MCP3004 & MCP3008

mcp3004

Given that the Rapsberry Pi supports only digital inputs, an ADC, such as a MCP, is required to read analogic data.

MCP3002, MCP3004 and MCP3008 work the same way but have dedicated classes for each version:

  • PhpGpio\Sensor\Mcp\Mcp3002
  • PhpGpio\Sensor\Mcp\Mcp3004
  • PhpGpio\Sensor\Mcp\Mcp3008

It use a SPI interface wich requires, at least, 4 pins:

  • MISO - Master Input Slave Ouput,
  • MOSI - Master Output Slave Input,
  • CLK - Clock,
  • CS - Channel Select (one dedicated pin for each component using the same SPI interface).

Provided classes use a software communication system so you can use any pins you wich.

Here is the connection schema of those 3 versions of MCP ADC:

mcp

Wiring:

  • VDD (power supply) on 3.3V,
  • VREF on 3.3v,
  • AGND on ground (GND),
  • CLK on clock pin,
  • DOUT (digital output) on MISO pin,
  • DIN (digital input) on MOSI pin,
  • CS (channel select) on CS pin,
  • DGND on ground (GND).

Now you are ready to read some data:

namespace MyProject;

use PhpGpio\Gpio;
use PhpGpio\Sensor\Mcp\Mcp3008;

// Defining pins mapping according to your setup
$pinsMapping = [
    'MISO' => 17,
    'MOSI' => 8,
    'CLK' => 23,
    'CS' => 24,
];

// Setup a Gpio class
$gpio = new Gpio(array_values($pins));

// Then we can instanciate the MCP class
$mcp = new Mcp3008(
    $gpio
    $mapping['CLK'],
    $mapping['MOSI'],
    $mapping['MISO'],
    $mapping['CS']
);

// Now let's read some data on the first channel
while (true) {
    echo $mcp->read(['channel' => 0]), "\n";
    // every second
    sleep(1);
}

tgeindre/php-gpio 适用场景与选型建议

tgeindre/php-gpio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 116 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「output」 「input」 「GPIO」 「hardware」 「chipset」 「port」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 116
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-31