piphp / gpio
用于访问树莓派GPIO引脚的库
0.4.0
2020-08-24 13:50 UTC
Requires
- php: ^7.3||^8.0
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-08-25 23:22:00 UTC
README
这是一个用于访问树莓派GPIO引脚的底层库。这些引脚可以用来控制输出(LED、电机、阀门、泵)或读取输入(传感器)。
由 AndrewCarterUK 提供
安装
使用 composer
composer require piphp/gpio
或者
php composer.phar require piphp/gpio
示例
设置输出引脚
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);
输入引脚中断
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));
进一步阅读
SitePoint 发布了一篇关于使用 PHP 驱动树莓派项目的教程 使用 PHP 驱动树莓派项目,其中使用了这个库,并展示了一个带布线图的按钮示例。
更多资源
PiPHP 维护了一个资源目录,用于在树莓派上进行 PHP 编程。