210 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			210 lines
		
	
	
		
			5.5 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;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								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 DummyGpioPin extends GpioPin
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    public function getPin()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->pin;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    private function findHardware()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								   		return "dummy";
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function __destruct()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function setDirection($direction)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!in_array($direction, array("in", "out"))) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $this->direction = $direction;
							 | 
						||
| 
								 | 
							
								        $this->sysfsWrite($this->pin, "direction", $direction);
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function getDirection()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->sysfsRead($this->pin, "direction");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function setValue($value)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->value = (bool)$value;
							 | 
						||
| 
								 | 
							
								        $this->sysfsWrite("{$this->pin}", "value", (int)$this->value);
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function getValue()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->sysfsRead($this->pin, "value");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Set a descriptive label for the pin, used for debugging and troubleshooting.
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param string $label
							 | 
						||
| 
								 | 
							
								     * @return NoccyLabs\Gpio\GpioPin 
							 | 
						||
| 
								 | 
							
								     */    
							 | 
						||
| 
								 | 
							
								    public function setLabel($label)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->label = (string)$label;
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Get the assigned label for this pin.
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @return string The label
							 | 
						||
| 
								 | 
							
								     */    
							 | 
						||
| 
								 | 
							
								    public function getLabel()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->label;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function export()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (file_exists("/sys/class/gpio/gpio{$this->pin}")) {
							 | 
						||
| 
								 | 
							
								        	return $this;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $this->sysfsWrite(null, "export", $this->pin);
							 | 
						||
| 
								 | 
							
								        if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
							 | 
						||
| 
								 | 
							
								            throw new HardwareException("Unable to export pin {$this->pin}");
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $this->hardware = $this->findHardware();
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function unexport()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								    	$this->sysfsWrite(null, "unexport", $this->pin);
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function sysfsWrite($pin, $file, $value)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if ($pin!==null) {
							 | 
						||
| 
								 | 
							
								            $path = "/sys/class/gpio/gpio{$pin}/{$file}";
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            $path = "/sys/class/gpio/{$file}";
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        printf("%16.4f  \e[32;1m%-5s\e[0m  \e[1m%-15s\e[0m (\e[33m%s\e[0m) => \e[36;1m%-10s\e[0m \n",
							 | 
						||
| 
								 | 
							
								            microtime(true),
							 | 
						||
| 
								 | 
							
								            "write",
							 | 
						||
| 
								 | 
							
								            $this->label,
							 | 
						||
| 
								 | 
							
								            $file,
							 | 
						||
| 
								 | 
							
								            $value
							 | 
						||
| 
								 | 
							
								            );
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function sysfsRead($pin, $file)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $path = "/sys/class/gpio/gpio{$pin}/{$file}";
							 | 
						||
| 
								 | 
							
								        printf("%16.4f  \e[31;1m%-5s\e[0m  \e[1m%-15s\e[0m (\e[33m%s\e[0m)\n",
							 | 
						||
| 
								 | 
							
								            microtime(true),
							 | 
						||
| 
								 | 
							
								            "read",
							 | 
						||
| 
								 | 
							
								            $this->label,
							 | 
						||
| 
								 | 
							
								            $file
							 | 
						||
| 
								 | 
							
								            );
							 | 
						||
| 
								 | 
							
								        return 0;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function setEdge($edge)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!in_array($edge, array("rising", "falling", "both", "none"))) {
							 | 
						||
| 
								 | 
							
								            throw new \Exception;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $this->edge = $edge;
							 | 
						||
| 
								 | 
							
								        $this->sysfsWrite($this->pin, "edge", $edge);
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function getEdge()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return $this->sysfsRead($this->pin, "edge");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function setHandler(callable $handler=null)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->gpio->setInterruptHandler($this->pin, $this->fd);
							 | 
						||
| 
								 | 
							
								        return $this;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function doInterrupt()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        fseek($this->fd,0,SEEK_SET);
							 | 
						||
| 
								 | 
							
								        $val = fgets($this->fd);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function dumpStatus($ansi=false)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if ($ansi) {
							 | 
						||
| 
								 | 
							
								            $status = "\e[44;37;1m          GPIO{$this->pin}          \e[0m\n";
							 | 
						||
| 
								 | 
							
								            $direction = 
							 | 
						||
| 
								 | 
							
								                ($this->direction=="input"?(CS::chr(0x2190)):(CS::chr(0x2192))).
							 | 
						||
| 
								 | 
							
								                " ".$this->direction;
							 | 
						||
| 
								 | 
							
								            $edge = $this->edge; //"";
							 | 
						||
| 
								 | 
							
								            /*if ($this->edge == "rising") {
							 | 
						||
| 
								 | 
							
								                $edge.= CS::chr(0x21A5)." rising";
							 | 
						||
| 
								 | 
							
								            } elseif ($this->edge == "falling") {
							 | 
						||
| 
								 | 
							
								                $edge.= CS::chr(0x21A7)." falling";
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								                $edge.= "\e[0m".$this->edge;
							 | 
						||
| 
								 | 
							
								            }*/
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            $status = "********** GPIO{$this->pin} **********\n";
							 | 
						||
| 
								 | 
							
								            $direction = $this->direction;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								        foreach(array(    
							 | 
						||
| 
								 | 
							
								            "Direction" => $this->getDirection(),
							 | 
						||
| 
								 | 
							
								            "Value" => ($this->getValue()?1:0),
							 | 
						||
| 
								 | 
							
								            "Edge" => $this->getEdge(),
							 | 
						||
| 
								 | 
							
								            "Label" => $this->label,
							 | 
						||
| 
								 | 
							
								            "Hardware" => $this->hardware,
							 | 
						||
| 
								 | 
							
								            "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;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								   
							 | 
						||
| 
								 | 
							
								}
							 |