cleaned up gpio and device code, implemented dummy/dry run mode

This commit is contained in:
2014-06-19 12:27:31 +02:00
parent 3e4765d071
commit 1449ec643f
8 changed files with 481 additions and 84 deletions

View File

@ -40,9 +40,11 @@ class Gpio implements \ArrayAccess
/** @var NoccyLabs\Gpio\GpioMapperInterface */
protected $mapper;
public function __construct($force=false)
protected $dummy;
public function __construct($dummy=false)
{
if (!$force) {
if (!$dummy) {
if (!file_exists("/sys/class/gpio")) {
throw new HardwareException("gpio sysfs is not available");
}
@ -50,44 +52,7 @@ class Gpio implements \ArrayAccess
throw new HardwareException("gpio sysfs is not writable");
}
}
}
public function refresh()
{
$read = $this->fd_gpio;
$write = array();
$except = $read;
if (count($read)>0) {
stream_select($read, $write, $except, 0);
foreach($except as $fd) {
$pin = $this->getPinFromFd($fd);
$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)
{
$this->dummy = $dummy;
}
public function setMapper(GpioMapperInterface $mapper=null)
@ -96,26 +61,59 @@ class Gpio implements \ArrayAccess
return $this;
}
/**
* Get a pin, optionally via previously specified mapper.
*
* @interface ArrayAccess
* @param int
*/
public function offsetGet($index)
{
if ($this->mapper) { $index = $this->mapper->mapLogicalToGpioPin($index); }
if ($this->mapper) {
$index = $this->mapper->mapLogicalToGpioPin($index);
}
if (empty($this->gpio[$index])) {
$this->gpio[$index] = new GpioPin($index, $this);
if ($this->dummy) {
$gpio = new DummyGpioPin($index, $this);
} else {
$gpio = new GpioPin($index, $this);
}
$this->gpio[$index] = $gpio;
}
return $this->gpio[$index];
}
/**
* Check if a GPIO is exported
*
* @interface ArrayAccess
* @param int
*/
public function offsetExists($index)
{
return array_key_exists($index, $this->gpio);
}
/**
* Not callable, get the requested pin via offsetGet() instead.
*
* @interface ArrayAccess
* @throws Exception
* @param int
* @param mixed
*/
public function offsetSet($index,$value)
{ throw new \Exception(); }
/**
* Unlink and unexport an exported GPIO
*
* @interface ArrayAccess
* @param int
*/
public function offsetUnset($index)
{
$this->gpio[$index]->unexport();
unset($this->gpio[$index]);
}