From bd5728062e9738372bdacd52a02deea70ba578bc Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 12 Jun 2014 01:53:57 +0200 Subject: [PATCH] Armed the gpio class --- examples/rf-remote.php | 35 +++++++++++++++++++++++++++++++++++ lib/Gpio.php | 14 ++++++++------ lib/GpioPin.php | 28 +++++++++++++--------------- 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 examples/rf-remote.php diff --git a/examples/rf-remote.php b/examples/rf-remote.php new file mode 100644 index 0000000..2524769 --- /dev/null +++ b/examples/rf-remote.php @@ -0,0 +1,35 @@ +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); +} + diff --git a/lib/Gpio.php b/lib/Gpio.php index ff88664..08bb3b4 100644 --- a/lib/Gpio.php +++ b/lib/Gpio.php @@ -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"); + } } } diff --git a/lib/GpioPin.php b/lib/GpioPin.php index a0e574c..e13669e 100644 --- a/lib/GpioPin.php +++ b/lib/GpioPin.php @@ -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;