php-linux-gpio/examples/rf-remote.php

61 lines
1.7 KiB
PHP

<?php
require_once __DIR__."/../vendor/autoload.php";
require_once __DIR__."/GLOBALS.php";
/*
* This example demonstrates using a 4-button wireless remote control with the
* GPIO, in this case with the pins mapped against WiringPi for a Rev2 board.
* If you are using this code with any other hardware, please update the mapper
* below.
*
* It is assumed that Pin 0 is the interrupt pin (VT) to signal when a button
* press has been received, and Pin 1-4 are connected to the pins for button
* A to D on the remote control.
*
* Note that the Raspberry Pi GPIO is not 5V tolerant, so use an optoisolator
* or transistors to ensure that you don't zap your preciouos hardware.
*
*/
use NoccyLabs\Gpio\Gpio;
use NoccyLabs\Gpio\GpioMapper\WiringPiMapper;
try {
$gpio = new Gpio(true);
} catch (Gpio\Exception $e) {
error_log("Error: {$e}");
}
// The mapper translates GPIO to logical pins and vice versa
$gpio->setMapper( new WiringPiMapper(2) );
// Access logical pin 0, since we got a mapper assigned. Otherwise this would
// be the actual GPIO0 pin.
$led = $gpio[0]
->export()
->setDirection("input")
->setEdge("rising")
->setHandler(function($e) use($gpio) {
if ($gpio[1]->getValue()) { echo "A"; }
elseif ($gpio[2]->getValue()) { echo "B"; }
elseif ($gpio[3]->getValue()) { echo "C"; }
elseif ($gpio[4]->getValue()) { echo "D"; }
else { echo "None"; }
echo "\n";
})
->setLabel("rfint")
->dumpStatus(true);
for($n = 1; $n < 5; $n++) {
$gpio[$n]
->setDirection("input")
->setLabel("rfbt{$n}")
->dumpStatus(true);
}
while(true) {
$gpio->refresh();
usleep(10000);
}