Armed the gpio class

This commit is contained in:
Chris 2014-06-12 01:53:57 +02:00
parent 429932bf4b
commit bd5728062e
3 changed files with 56 additions and 21 deletions

35
examples/rf-remote.php Normal file
View File

@ -0,0 +1,35 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
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) {
})
->setLabel("rfint")
->dumpStatus(true);
for($n = 1; $n < 5; $n++) {
$gpio[$n]
->setDirection("input")
->setLabel("rfbt{$n}")
->dumpStatus(true);
}

View File

@ -28,13 +28,15 @@ class Gpio implements \ArrayAccess
/** @var NoccyLabs\Gpio\GpioMapperInterface */
protected $mapper;
public function __construct()
public function __construct($force=false)
{
if (!file_exists("/sys/class/gpio")) {
throw new HardwareException("gpio sysfs is not available");
}
if (!is_writable("/sys/class/gpio/export")) {
throw new HardwareException("gpio sysfs is not writable");
if (!$force) {
if (!file_exists("/sys/class/gpio")) {
throw new HardwareException("gpio sysfs is not available");
}
if (!is_writable("/sys/class/gpio/export")) {
throw new HardwareException("gpio sysfs is not writable");
}
}
}

View File

@ -45,12 +45,12 @@ class GpioPin
public function __construct($pin)
{
$this->pin = (int)$pin;
//$this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb");
$this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb");
}
public function __destruct()
{
//fclose($this->fd);
fclose($this->fd);
}
public function setDirection($direction)
@ -59,7 +59,7 @@ class GpioPin
throw new \Exception;
}
$this->direction = $direction;
//file_put_contents("/sys/class/gpio/{$this->pin}/direction", $direction);
file_put_contents("/sys/class/gpio/{$this->pin}/direction", $direction);
return $this;
}
@ -71,7 +71,7 @@ class GpioPin
public function setValue($value)
{
$this->value = (bool)$value;
//file_put_contents("/sys/class/gpio/{$this->pin}/value", (int)$this->value);
file_put_contents("/sys/class/gpio/{$this->pin}/value", (int)$this->value);
return $this;
}
@ -93,18 +93,16 @@ class GpioPin
public function export()
{
/*
file_put_contents("/sys/class/gpio/export", $this->pin);
if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
throw new \Exception();
}
*/
return $this;
}
public function unexport()
{
//file_put_contents("/sys/class/gpio/unexport", $this->pin);
file_put_contents("/sys/class/gpio/unexport", $this->pin);
return $this;
}
@ -114,7 +112,7 @@ class GpioPin
throw new \Exception;
}
$this->edge = $edge;
//file_put_contents("/sys/class/gpio/{$this->pin}/edge", $edge);
file_put_contents("/sys/class/gpio/{$this->pin}/edge", $edge);
return $this;
}
@ -138,17 +136,17 @@ class GpioPin
{
if ($ansi) {
$status = "\e[44;37;1m GPIO{$this->pin} \e[0m\n";
$direction = "\e[36;1m".
$direction =
($this->direction=="input"?(CS::chr(0x2190)):(CS::chr(0x2192))).
"\e[0m ".$this->direction;
$edge = "\e[33;1m";
if ($this->edge == "rising") {
$edge.= CS::chr(0x21A5)."\e[0m rising";
" ".$this->direction;
$edge = $this->edge; //"";
/*if ($this->edge == "rising") {
$edge.= CS::chr(0x21A5)." rising";
} elseif ($this->edge == "falling") {
$edge.= CS::chr(0x21A7)."\e[0m falling";
$edge.= CS::chr(0x21A7)." falling";
} else {
$edge.= "\e[0m".$this->edge;
}
}*/
} else {
$status = "********** GPIO{$this->pin} **********\n";
$direction = $this->direction;