2014-06-06 14:00:34 +00:00
|
|
|
<?php
|
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
/*
|
|
|
|
* 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/>
|
|
|
|
*/
|
|
|
|
|
2014-06-06 14:00:34 +00:00
|
|
|
namespace NoccyLabs\Gpio;
|
|
|
|
|
2014-06-11 23:21:08 +00:00
|
|
|
use NoccyLabs\Gpio\Exception\HardwareException;
|
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
class Gpio implements \ArrayAccess
|
2014-06-06 14:00:34 +00:00
|
|
|
{
|
2014-06-12 00:35:19 +00:00
|
|
|
// 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";
|
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
protected $gpio = array();
|
|
|
|
|
|
|
|
/** @var NoccyLabs\Gpio\GpioMapperInterface */
|
|
|
|
protected $mapper;
|
2014-06-11 23:21:08 +00:00
|
|
|
|
2014-06-11 23:53:57 +00:00
|
|
|
public function __construct($force=false)
|
2014-06-11 23:21:08 +00:00
|
|
|
{
|
2014-06-11 23:53:57 +00:00
|
|
|
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");
|
|
|
|
}
|
2014-06-11 23:21:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
public function refresh()
|
|
|
|
{
|
|
|
|
$read = $this->fd_gpio;
|
|
|
|
$write = array();
|
|
|
|
$except = $read;
|
2014-06-12 16:14:51 +00:00
|
|
|
if (count($read)>0) {
|
|
|
|
stream_select($read, $write, $except, 0);
|
|
|
|
foreach($except as $fd) {
|
|
|
|
$pin = $this->getPinFromFd($fd);
|
|
|
|
$pin->doInterrupt();
|
|
|
|
|
|
|
|
}
|
2014-06-06 17:50:17 +00:00
|
|
|
}
|
2014-06-12 00:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-12 00:35:19 +00:00
|
|
|
public function enableInterrupt(GpioPin $pin, $fd)
|
|
|
|
{
|
|
|
|
$this->fd_gpio[] = $fd;
|
|
|
|
|
|
|
|
$this->fd_pins[$fd] = $pin;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function disableInterrupt(GpioPin $pin)
|
|
|
|
{
|
2014-06-06 17:50:17 +00:00
|
|
|
}
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
public function setMapper(GpioMapperInterface $mapper=null)
|
|
|
|
{
|
|
|
|
$this->mapper = $mapper;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
public function offsetGet($index)
|
|
|
|
{
|
|
|
|
if ($this->mapper) { $index = $this->mapper->mapLogicalToGpioPin($index); }
|
|
|
|
if (empty($this->gpio[$index])) {
|
2014-06-12 00:35:19 +00:00
|
|
|
$this->gpio[$index] = new GpioPin($index, $this);
|
2014-06-06 17:50:17 +00:00
|
|
|
}
|
|
|
|
return $this->gpio[$index];
|
|
|
|
}
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
public function offsetExists($index)
|
|
|
|
{
|
|
|
|
return array_key_exists($index, $this->gpio);
|
|
|
|
}
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
|
|
|
|
public function offsetSet($index,$value)
|
|
|
|
{ throw new \Exception(); }
|
2014-06-06 14:00:34 +00:00
|
|
|
|
2014-06-06 17:50:17 +00:00
|
|
|
public function offsetUnset($index)
|
|
|
|
{
|
|
|
|
unset($this->gpio[$index]);
|
|
|
|
}
|
2014-06-06 14:00:34 +00:00
|
|
|
|
|
|
|
}
|