定制 piphp/gpio 二次开发

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

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

piphp/gpio

最新稳定版本:0.4.0

Composer 安装命令:

composer require piphp/gpio

包简介

A library for accessing the GPIO pins on a Raspberry Pi

README 文档

README

License Total Downloads

A library for low level access to the GPIO pins on a Raspberry Pi. These pins can be used to control outputs (LEDs, motors, valves, pumps) or read inputs (sensors).

By AndrewCarterUK (Twitter)

Installing

Using composer:

composer require piphp/gpio

Or:

php composer.phar require piphp/gpio

Examples

Setting Output Pins

use PiPHP\GPIO\GPIO;
use PiPHP\GPIO\Pin\PinInterface;

// Create a GPIO object
$gpio = new GPIO();

// Retrieve pin 18 and configure it as an output pin
$pin = $gpio->getOutputPin(18);

// Set the value of the pin high (turn it on)
$pin->setValue(PinInterface::VALUE_HIGH);

Input Pin Interrupts

use PiPHP\GPIO\GPIO;
use PiPHP\GPIO\Pin\InputPinInterface;

// Create a GPIO object
$gpio = new GPIO();

// Retrieve pin 18 and configure it as an input pin
$pin = $gpio->getInputPin(18);

// Configure interrupts for both rising and falling edges
$pin->setEdge(InputPinInterface::EDGE_BOTH);

// Create an interrupt watcher
$interruptWatcher = $gpio->createWatcher();

// Register a callback to be triggered on pin interrupts
$interruptWatcher->register($pin, function (InputPinInterface $pin, $value) {
    echo 'Pin ' . $pin->getNumber() . ' changed to: ' . $value . PHP_EOL;

    // Returning false will make the watcher return false immediately
    return true;
});

// Watch for interrupts, timeout after 5000ms (5 seconds)
while ($interruptWatcher->watch(5000));

Further Reading

SitePoint published a tutorial about powering Raspberry Pi projects with PHP which used this library and shows a push button example with a wiring diagram.

More Resources

PiPHP maintains a resource directory for PHP programming on the Raspberry Pi.

统计信息

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

GitHub 信息

  • Stars: 431
  • Watchers: 20
  • Forks: 41
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-13