Improved sysfs file access code
This commit is contained in:
36
lib/Gpio.php
36
lib/Gpio.php
@ -23,6 +23,18 @@ use NoccyLabs\Gpio\Exception\HardwareException;
|
||||
|
||||
class Gpio implements \ArrayAccess
|
||||
{
|
||||
// Direction
|
||||
const DIR_IN = "in";
|
||||
const DIR_OUT = "out";
|
||||
// Values
|
||||
const VAL_HIGH = 1;
|
||||
const VAL_LOW = 0;
|
||||
// Edges
|
||||
const EDGE_NONE = "none";
|
||||
const EDGE_RISING = "rising";
|
||||
const EDGE_FALLING = "faling";
|
||||
const EDGE_BOTH = "both";
|
||||
|
||||
protected $gpio = array();
|
||||
|
||||
/** @var NoccyLabs\Gpio\GpioMapperInterface */
|
||||
@ -51,7 +63,29 @@ class Gpio implements \ArrayAccess
|
||||
$pin->doInterrupt();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function getPinFromFd($fd)
|
||||
{
|
||||
foreach($this->fd_pins as $pinfd=>$pin) {
|
||||
if ($pinfd == $fd) { return $pin; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected $fd_gpio = array();
|
||||
protected $fd_pins = array();
|
||||
|
||||
public function enableInterrupt(GpioPin $pin, $fd)
|
||||
{
|
||||
$this->fd_gpio[] = $fd;
|
||||
|
||||
$this->fd_pins[$fd] = $pin;
|
||||
|
||||
}
|
||||
|
||||
public function disableInterrupt(GpioPin $pin)
|
||||
{
|
||||
}
|
||||
|
||||
public function setMapper(GpioMapperInterface $mapper=null)
|
||||
@ -64,7 +98,7 @@ class Gpio implements \ArrayAccess
|
||||
{
|
||||
if ($this->mapper) { $index = $this->mapper->mapLogicalToGpioPin($index); }
|
||||
if (empty($this->gpio[$index])) {
|
||||
$this->gpio[$index] = new GpioPin($index);
|
||||
$this->gpio[$index] = new GpioPin($index, $this);
|
||||
}
|
||||
return $this->gpio[$index];
|
||||
}
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
namespace NoccyLabs\Gpio;
|
||||
|
||||
use NoccyLabs\Gpio\Exception\HardwareException;
|
||||
|
||||
use NoccyLabs\Sansi\Charset as CS;
|
||||
|
||||
/**
|
||||
@ -41,10 +43,13 @@ class GpioPin
|
||||
protected $handler;
|
||||
|
||||
protected $label;
|
||||
|
||||
protected $gpio;
|
||||
|
||||
public function __construct($pin)
|
||||
public function __construct($pin, Gpio $gpio)
|
||||
{
|
||||
$this->pin = (int)$pin;
|
||||
$this->gpio = $gpio;
|
||||
$this->export();
|
||||
$this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb");
|
||||
}
|
||||
@ -80,13 +85,24 @@ class GpioPin
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set a descriptive label for the pin, used for debugging and troubleshooting.
|
||||
*
|
||||
* @param string $label
|
||||
* @return NoccyLabs\Gpio\GpioPin
|
||||
*/
|
||||
public function setLabel($label)
|
||||
{
|
||||
$this->label = (string)$label;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned label for this pin.
|
||||
*
|
||||
* @return string The label
|
||||
*/
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->label;
|
||||
@ -94,9 +110,9 @@ class GpioPin
|
||||
|
||||
public function export()
|
||||
{
|
||||
@file_put_contents("/sys/class/gpio/export", $this->pin);
|
||||
$this->sysfsWrite(null, "export", $this->pin);
|
||||
if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
|
||||
throw new \Exception();
|
||||
throw new HardwareException("Unable to export pin {$this->pin}");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
@ -107,23 +123,54 @@ class GpioPin
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sysfsWrite($pin, $file, $value)
|
||||
{
|
||||
if ($pin) {
|
||||
$path = "/sys/class/gpio/gpio{$pin}/{$file}";
|
||||
} else {
|
||||
$path = "/sys/class/gpio/{$file}";
|
||||
}
|
||||
if (!is_writable($path)) {
|
||||
throw new HardwareException("Sysfs file {$path} not writable");
|
||||
}
|
||||
$f = fopen($path,"w+");
|
||||
if (!is_resource($f)) {
|
||||
throw new HardwareException("Unable to write to sysfs file {$path}");
|
||||
}
|
||||
fwrite($f,$value);
|
||||
fclose($f);
|
||||
}
|
||||
|
||||
public function sysfsRead($pin, $file)
|
||||
{
|
||||
$path = "/sys/class/gpio/gpio{$pin}/{$file}";
|
||||
$f = fopen($path,"r");
|
||||
if (!is_resource($f)) {
|
||||
throw new HardwareException("Unable to read from sysfs file {$path}");
|
||||
}
|
||||
$ret = fgets($f);
|
||||
fclose($f);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function setEdge($edge)
|
||||
{
|
||||
if (!in_array($edge, array("rising", "falling", "both", "none"))) {
|
||||
throw new \Exception;
|
||||
}
|
||||
$this->edge = $edge;
|
||||
@file_put_contents("/sys/class/gpio/gpio{$this->pin}/edge", $edge);
|
||||
$this->sysfsWrite($this->pin, "edge", $edge);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEdge()
|
||||
{
|
||||
return $this->edge;
|
||||
return $this->sysfsRead($this->pin, "edge");
|
||||
}
|
||||
|
||||
public function setHandler(callable $handler=null)
|
||||
{
|
||||
$this->gpio->setInterruptHandler($this->pin, $this->fd);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user