*/ 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; protected $dummy; public function __construct($dummy=false) { if (!$dummy) { 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"); } } $this->dummy = $dummy; } public function setMapper(GpioMapperInterface $mapper=null) { $this->mapper = $mapper; 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 (empty($this->gpio[$index])) { 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]); } }