From 2fb00885280a092996f316b780bb9bd8de6d8875 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 12 Jun 2014 00:54:55 +0000 Subject: [PATCH] Hardware detection for gpio pin --- examples/basic.php | 9 +++++---- lib/GpioPin.php | 45 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/examples/basic.php b/examples/basic.php index 2226188..f713e7f 100644 --- a/examples/basic.php +++ b/examples/basic.php @@ -18,13 +18,14 @@ $gpio->setMapper( new WiringPiMapper(2) ); // be the actual GPIO0 pin. $led = $gpio[0] ->export() - ->setDirection("output") - ->setValue(0) - ->dumpStatus(true); + ->setEdge(Gpio::EDGE_NONE) + ->setDirection(Gpio::DIR_OUT) + ->setLabel("LED pin") + ->setValue(0); $x = 0; while(true) { $x = (int)(!$x); - $led->setValue($x); + $led->setValue($x)->dumpStatus(true); usleep(500000); } diff --git a/lib/GpioPin.php b/lib/GpioPin.php index 05b5dc4..f5d2664 100644 --- a/lib/GpioPin.php +++ b/lib/GpioPin.php @@ -41,6 +41,8 @@ class GpioPin protected $edge; protected $handler; + + protected $hardware; protected $label; @@ -51,7 +53,25 @@ class GpioPin $this->pin = (int)$pin; $this->gpio = $gpio; $this->export(); - $this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb"); + $this->hardware = $this->findHardware(); + } + + private function findHardware() + { + $chips = glob("/sys/class/gpio/gpiochip*"); + $pinchip = null; + foreach($chips as $chip) { + $r = null; + if (preg_match("/([0-9]+?)/", $chip, $r)) { + $base = (int)$r[1]; + if ($base < $this->pin) { $pinchip = $chip; } + } + } + if ($pinchip) { + $hw = trim(file_get_contents($pinchip."/label")); + return $hw; + } + return "unknown"; } public function __destruct() @@ -61,17 +81,17 @@ class GpioPin public function setDirection($direction) { - if (!in_array($direction, array("input", "output"))) { + if (!in_array($direction, array("in", "out"))) { throw new \Exception; } $this->direction = $direction; - @file_put_contents("/sys/class/gpio/gpio{$this->pin}/direction", $direction); + $this->sysfsWrite($this->pin, "direction", $direction); return $this; } public function getDirection() { - return $this->direction; + return $this->sysfsRead($this->pin, "direction"); } public function setValue($value) @@ -83,7 +103,7 @@ class GpioPin public function getValue() { - return $this->value; + return $this->sysfsRead($this->pin, "value"); } /** @@ -110,6 +130,9 @@ class GpioPin public function export() { + if (file_exists("/sys/class/gpio/gpio{$this->pin}")) { + return $this; + } $this->sysfsWrite(null, "export", $this->pin); if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) { throw new HardwareException("Unable to export pin {$this->pin}"); @@ -119,7 +142,7 @@ class GpioPin public function unexport() { - @file_put_contents("/sys/class/gpio/unexport", $this->pin); + $this->sysfsWrite(null, "unexport", $this->pin); return $this; } @@ -150,7 +173,7 @@ class GpioPin } $ret = fgets($f); fclose($f); - return $ret; + return trim($ret); } public function setEdge($edge) @@ -202,11 +225,11 @@ class GpioPin foreach(array( - "Direction" => $direction, - "Value" => ($this->value?1:0), - "Edge" => $edge, + "Direction" => $this->getDirection(), + "Value" => ($this->getValue()?1:0), + "Edge" => $this->getEdge(), "Label" => $this->label, - "Hardware" => "unknown", + "Hardware" => $this->hardware, "Int count" => 0 ) as $k=>$v) { if ($ansi) {