*/ namespace NoccyLabs\Gpio; 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 */ protected $mapper; public function __construct($force=false) { 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"); } } } public function refresh() { $read = $this->fd_gpio; $write = array(); $except = $read; 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) { } public function setMapper(GpioMapperInterface $mapper=null) { $this->mapper = $mapper; return $this; } public function offsetGet($index) { if ($this->mapper) { $index = $this->mapper->mapLogicalToGpioPin($index); } if (empty($this->gpio[$index])) { $this->gpio[$index] = new GpioPin($index, $this); } return $this->gpio[$index]; } public function offsetExists($index) { return array_key_exists($index, $this->gpio); } public function offsetSet($index,$value) { throw new \Exception(); } public function offsetUnset($index) { unset($this->gpio[$index]); } }