Hardware detection for gpio pin

This commit is contained in:
Chris 2014-06-12 00:54:55 +00:00
parent b53cabec90
commit 2fb0088528
2 changed files with 39 additions and 15 deletions

View File

@ -18,13 +18,14 @@ $gpio->setMapper( new WiringPiMapper(2) );
// be the actual GPIO0 pin. // be the actual GPIO0 pin.
$led = $gpio[0] $led = $gpio[0]
->export() ->export()
->setDirection("output") ->setEdge(Gpio::EDGE_NONE)
->setValue(0) ->setDirection(Gpio::DIR_OUT)
->dumpStatus(true); ->setLabel("LED pin")
->setValue(0);
$x = 0; $x = 0;
while(true) { while(true) {
$x = (int)(!$x); $x = (int)(!$x);
$led->setValue($x); $led->setValue($x)->dumpStatus(true);
usleep(500000); usleep(500000);
} }

View File

@ -42,6 +42,8 @@ class GpioPin
protected $handler; protected $handler;
protected $hardware;
protected $label; protected $label;
protected $gpio; protected $gpio;
@ -51,7 +53,25 @@ class GpioPin
$this->pin = (int)$pin; $this->pin = (int)$pin;
$this->gpio = $gpio; $this->gpio = $gpio;
$this->export(); $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() public function __destruct()
@ -61,17 +81,17 @@ class GpioPin
public function setDirection($direction) public function setDirection($direction)
{ {
if (!in_array($direction, array("input", "output"))) { if (!in_array($direction, array("in", "out"))) {
throw new \Exception; throw new \Exception;
} }
$this->direction = $direction; $this->direction = $direction;
@file_put_contents("/sys/class/gpio/gpio{$this->pin}/direction", $direction); $this->sysfsWrite($this->pin, "direction", $direction);
return $this; return $this;
} }
public function getDirection() public function getDirection()
{ {
return $this->direction; return $this->sysfsRead($this->pin, "direction");
} }
public function setValue($value) public function setValue($value)
@ -83,7 +103,7 @@ class GpioPin
public function getValue() public function getValue()
{ {
return $this->value; return $this->sysfsRead($this->pin, "value");
} }
/** /**
@ -110,6 +130,9 @@ class GpioPin
public function export() public function export()
{ {
if (file_exists("/sys/class/gpio/gpio{$this->pin}")) {
return $this;
}
$this->sysfsWrite(null, "export", $this->pin); $this->sysfsWrite(null, "export", $this->pin);
if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) { if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
throw new HardwareException("Unable to export pin {$this->pin}"); throw new HardwareException("Unable to export pin {$this->pin}");
@ -119,7 +142,7 @@ class GpioPin
public function unexport() public function unexport()
{ {
@file_put_contents("/sys/class/gpio/unexport", $this->pin); $this->sysfsWrite(null, "unexport", $this->pin);
return $this; return $this;
} }
@ -150,7 +173,7 @@ class GpioPin
} }
$ret = fgets($f); $ret = fgets($f);
fclose($f); fclose($f);
return $ret; return trim($ret);
} }
public function setEdge($edge) public function setEdge($edge)
@ -202,11 +225,11 @@ class GpioPin
foreach(array( foreach(array(
"Direction" => $direction, "Direction" => $this->getDirection(),
"Value" => ($this->value?1:0), "Value" => ($this->getValue()?1:0),
"Edge" => $edge, "Edge" => $this->getEdge(),
"Label" => $this->label, "Label" => $this->label,
"Hardware" => "unknown", "Hardware" => $this->hardware,
"Int count" => 0 "Int count" => 0
) as $k=>$v) { ) as $k=>$v) {
if ($ansi) { if ($ansi) {