GPIO

A PHP library for accessing the GPIO pins on a Raspberry Pi.

Github星跟踪图

PiPHP: GPIO

Latest Stable Version
Build Status
Code Coverage
Scrutinizer Code Quality
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.

主要指标

概览
名称与所有者PiPHP/GPIO
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-11-24 21:56:51
推送于2020-08-26 22:34:54
最后一次提交2020-08-24 21:50:12
发布数14
最新版本名称0.4.0 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数432
关注者数20
派生数41
提交数99
已启用问题?
问题数22
打开的问题数10
拉请求数17
打开的拉请求数2
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?