85 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
/*
 | 
						|
 * Copyright (C) 2014, NoccyLabs
 | 
						|
 * 
 | 
						|
 * This program is free software: you can redistribute it and/or modify
 | 
						|
 * it under the terms of the GNU General Public License as published by
 | 
						|
 * the Free Software Foundation, either version 3 of the License, or
 | 
						|
 * (at your option) any later version.
 | 
						|
 * 
 | 
						|
 * This program is distributed in the hope that it will be useful,
 | 
						|
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
						|
 * GNU General Public License for more details.
 | 
						|
 * 
 | 
						|
 * You should have received a copy of the GNU General Public License
 | 
						|
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 | 
						|
 */
 | 
						|
 | 
						|
namespace NoccyLabs\Gpio;
 | 
						|
 | 
						|
use NoccyLabs\Gpio\Exception\HardwareException;
 | 
						|
 | 
						|
class Gpio implements \ArrayAccess
 | 
						|
{
 | 
						|
    protected $gpio = array();
 | 
						|
    
 | 
						|
    /** @var NoccyLabs\Gpio\GpioMapperInterface */
 | 
						|
    protected $mapper;
 | 
						|
    
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        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 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);
 | 
						|
        }
 | 
						|
        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]);
 | 
						|
    }
 | 
						|
    
 | 
						|
}
 |