php-linux-gpio/examples/basic.php

41 lines
931 B
PHP
Raw Permalink Normal View History

2014-06-05 00:20:22 +00:00
<?php
require_once __DIR__."/../vendor/autoload.php";
require_once __DIR__."/GLOBALS.php";
2014-06-05 00:20:22 +00:00
2014-06-12 12:19:22 +00:00
/*
* This example will flash a led connected on pin 0 (GPIO17 on a Pi). If you are
* not on a Pi, please change (or remove) the mapper below and update the pin
* numbers to match.
*
*/
use NoccyLabs\Gpio\Gpio;
use NoccyLabs\Gpio\GpioMapper\WiringPiMapper;
2014-06-05 00:20:22 +00:00
try {
$gpio = new Gpio();
2014-06-05 00:20:22 +00:00
} 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
2014-06-12 00:35:19 +00:00
// be the actual GPIO0 pin.
$led = $gpio[LED_A]
->export()
2014-06-12 00:54:55 +00:00
->setEdge(Gpio::EDGE_NONE)
->setDirection(Gpio::DIR_OUT)
->setLabel("LED pin")
->setValue(0);
2014-06-12 12:19:22 +00:00
// Flash the led
2014-06-12 00:35:19 +00:00
$x = 0;
while(true) {
$x = (int)(!$x);
2014-06-12 00:54:55 +00:00
$led->setValue($x)->dumpStatus(true);
2014-06-12 00:35:19 +00:00
usleep(500000);
}