se/wired-pi

WiredPi是一个通过wiringPi连接PHP和Raspberry Pi GPIO的库

v0.1-beta 2013-12-01 15:47 UTC

This package is not auto-updated.

Last update: 2024-09-10 01:27:56 UTC


README

Latest Stable Version SensioLabsInsight

WiredPi是一个通过wiringPi连接PHP和Raspberry Pi GPIO的库。

开发分支是master分支。

Build Status

目录
  1. 安装
  2. 使用

安装

建议通过Composer进行安装。

{
    "require": {
        "se/wired-pi": "dev-master"
    }
}

WiredPi内部使用WiringPi库。非常感谢@drogon。安装步骤如下

$ cd /opt
$ sudo mkdir wiringpi
$ sudo chown $USER ./wiringpi
$ cd wiringpi
$ git clone git://git.drogon.net/wiringPi ./
$ git pull origin
$ ./build

(需要git-core包进行git操作。通过sudo apt-get install git-core安装。)

使用

基本使用

要求composer自动加载文件并创建一个新的板。

require_once __DIR__.'/vendor/autoload.php';

use \SE\Component\WiredPi;

$platform = new WiredPi\Platform\RaspberryPi();
$board = new WiredPi\Board($platform);

下一步是为端口应用端口映射,以便它知道哪些引脚可以控制。

$map = new WiredPi\PortMap(array(
    // number of the GPIO-pin,
    18
    // you can set default options too, STATE_ON turns the pin by default on
    23 => array(WiredPi\Port::STATE => WiredPi\Port::STATE_ON)
));

现在将端口映射添加到您的板实例中并刷新它。设置立即生效。

$board->setPorts($map);
$board->refresh(); // applies the current state to the microcontroller

// Modification of ports need to be applied again
$board->getPort(18)->on();
$board->refresh();

读取引脚

要读取引脚的状态,您可以使用传递给板的平台实例。

$port = $board->getPort(18);
$status = $platform->read($port); // returns 0 or 1

默认情况下,引脚设置为OUT。

要从IN接收引脚的状态,请将模式设置为IN。

$port = $board->getPort(18);
$port->setMode(WiredPi\Port::MODE_IN);

// Do something when Pin switches to 1
while(true) {
    
    if($platform->read($port) == '1') {
        print sprintf('Pin %s went to %s', $port, '1')
        break;
    }
    usleep(5000); // Let the system catch up
}

原型服务器

WiredPi包含一个原型服务器,可以控制引脚而无需在PHP脚本内设置它们。通过内置的PHP服务器运行它(自PHP 5.4.0起)。

$ php -S localhost:8000 scripts/server.php

更多示例请参阅examples/