2014-06-06 17:50:17 +00:00
|
|
|
<?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\Sansi\Charset as CS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GPIO pin implementation. This class wraps all interaction with a GPIO pin,
|
|
|
|
* thus decoupling it from the main Gpio class.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class GpioPin
|
|
|
|
{
|
|
|
|
protected $fd;
|
|
|
|
|
|
|
|
protected $pin;
|
|
|
|
|
|
|
|
protected $value = 0;
|
|
|
|
|
|
|
|
protected $direction;
|
|
|
|
|
|
|
|
protected $edge;
|
|
|
|
|
|
|
|
protected $handler;
|
|
|
|
|
|
|
|
protected $label;
|
|
|
|
|
|
|
|
public function __construct($pin)
|
|
|
|
{
|
|
|
|
$this->pin = (int)$pin;
|
2014-06-11 23:56:02 +00:00
|
|
|
$this->export();
|
2014-06-11 23:58:25 +00:00
|
|
|
$this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb");
|
2014-06-06 17:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2014-06-11 23:53:57 +00:00
|
|
|
fclose($this->fd);
|
2014-06-06 17:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setDirection($direction)
|
|
|
|
{
|
|
|
|
if (!in_array($direction, array("input", "output"))) {
|
|
|
|
throw new \Exception;
|
|
|
|
}
|
|
|
|
$this->direction = $direction;
|
2014-06-11 23:59:17 +00:00
|
|
|
@file_put_contents("/sys/class/gpio/gpio{$this->pin}/direction", $direction);
|
2014-06-06 17:50:17 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDirection()
|
|
|
|
{
|
|
|
|
return $this->direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setValue($value)
|
|
|
|
{
|
|
|
|
$this->value = (bool)$value;
|
2014-06-11 23:59:17 +00:00
|
|
|
@file_put_contents("/sys/class/gpio/gpio{$this->pin}/value", (int)$this->value);
|
2014-06-06 17:50:17 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLabel($label)
|
|
|
|
{
|
|
|
|
$this->label = (string)$label;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLabel()
|
|
|
|
{
|
|
|
|
return $this->label;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function export()
|
|
|
|
{
|
2014-06-11 23:59:17 +00:00
|
|
|
@file_put_contents("/sys/class/gpio/export", $this->pin);
|
2014-06-06 17:50:17 +00:00
|
|
|
if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unexport()
|
|
|
|
{
|
2014-06-11 23:59:17 +00:00
|
|
|
@file_put_contents("/sys/class/gpio/unexport", $this->pin);
|
2014-06-06 17:50:17 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setEdge($edge)
|
|
|
|
{
|
|
|
|
if (!in_array($edge, array("rising", "falling", "both", "none"))) {
|
|
|
|
throw new \Exception;
|
|
|
|
}
|
|
|
|
$this->edge = $edge;
|
2014-06-11 23:59:17 +00:00
|
|
|
@file_put_contents("/sys/class/gpio/gpio{$this->pin}/edge", $edge);
|
2014-06-06 17:50:17 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEdge()
|
|
|
|
{
|
|
|
|
return $this->edge;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHandler(callable $handler=null)
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doInterrupt()
|
|
|
|
{
|
|
|
|
fseek($this->fd,0,SEEK_SET);
|
|
|
|
$val = fgets($this->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dumpStatus($ansi=false)
|
|
|
|
{
|
|
|
|
if ($ansi) {
|
2014-06-11 23:21:08 +00:00
|
|
|
$status = "\e[44;37;1m GPIO{$this->pin} \e[0m\n";
|
2014-06-11 23:53:57 +00:00
|
|
|
$direction =
|
2014-06-06 17:50:17 +00:00
|
|
|
($this->direction=="input"?(CS::chr(0x2190)):(CS::chr(0x2192))).
|
2014-06-11 23:53:57 +00:00
|
|
|
" ".$this->direction;
|
|
|
|
$edge = $this->edge; //"";
|
|
|
|
/*if ($this->edge == "rising") {
|
|
|
|
$edge.= CS::chr(0x21A5)." rising";
|
2014-06-06 17:50:17 +00:00
|
|
|
} elseif ($this->edge == "falling") {
|
2014-06-11 23:53:57 +00:00
|
|
|
$edge.= CS::chr(0x21A7)." falling";
|
2014-06-06 17:50:17 +00:00
|
|
|
} else {
|
|
|
|
$edge.= "\e[0m".$this->edge;
|
2014-06-11 23:53:57 +00:00
|
|
|
}*/
|
2014-06-06 17:50:17 +00:00
|
|
|
} else {
|
|
|
|
$status = "********** GPIO{$this->pin} **********\n";
|
|
|
|
$direction = $this->direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach(array(
|
|
|
|
"Direction" => $direction,
|
|
|
|
"Value" => ($this->value?1:0),
|
|
|
|
"Edge" => $edge,
|
|
|
|
"Label" => $this->label,
|
|
|
|
"Hardware" => "unknown",
|
|
|
|
"Int count" => 0
|
|
|
|
) as $k=>$v) {
|
|
|
|
if ($ansi) {
|
|
|
|
$status.= sprintf(" \e[32;1m%10s\e[0m: \e[0m%s\e[0m\n", $k, $v);
|
|
|
|
} else {
|
|
|
|
$status.= sprintf(" %-10s: %s\n", $k, $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error_log($status);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|