Hardware detection for gpio pin
This commit is contained in:
		@@ -41,6 +41,8 @@ class GpioPin
 | 
			
		||||
    protected $edge;
 | 
			
		||||
    
 | 
			
		||||
    protected $handler;
 | 
			
		||||
    
 | 
			
		||||
    protected $hardware;
 | 
			
		||||
 | 
			
		||||
    protected $label;
 | 
			
		||||
    
 | 
			
		||||
@@ -51,7 +53,25 @@ class GpioPin
 | 
			
		||||
        $this->pin = (int)$pin;
 | 
			
		||||
        $this->gpio = $gpio;
 | 
			
		||||
        $this->export();
 | 
			
		||||
        $this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb");
 | 
			
		||||
        $this->hardware = $this->findHardware();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private function findHardware()
 | 
			
		||||
    {
 | 
			
		||||
    	$chips = glob("/sys/class/gpio/gpiochip*");
 | 
			
		||||
    	$pinchip = null;
 | 
			
		||||
    	foreach($chips as $chip) {
 | 
			
		||||
    		$r = null;
 | 
			
		||||
    		if (preg_match("/([0-9]+?)/", $chip, $r)) {
 | 
			
		||||
    			$base = (int)$r[1];
 | 
			
		||||
    			if ($base < $this->pin) { $pinchip = $chip; }
 | 
			
		||||
    		}
 | 
			
		||||
    	}
 | 
			
		||||
    	if ($pinchip) {
 | 
			
		||||
	    	$hw = trim(file_get_contents($pinchip."/label"));
 | 
			
		||||
    		return $hw;
 | 
			
		||||
   		}
 | 
			
		||||
   		return "unknown";
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function __destruct()
 | 
			
		||||
@@ -61,17 +81,17 @@ class GpioPin
 | 
			
		||||
    
 | 
			
		||||
    public function setDirection($direction)
 | 
			
		||||
    {
 | 
			
		||||
        if (!in_array($direction, array("input", "output"))) {
 | 
			
		||||
        if (!in_array($direction, array("in", "out"))) {
 | 
			
		||||
            throw new \Exception;
 | 
			
		||||
        }
 | 
			
		||||
        $this->direction = $direction;
 | 
			
		||||
        @file_put_contents("/sys/class/gpio/gpio{$this->pin}/direction", $direction);
 | 
			
		||||
        $this->sysfsWrite($this->pin, "direction", $direction);
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function getDirection()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->direction;
 | 
			
		||||
        return $this->sysfsRead($this->pin, "direction");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function setValue($value)
 | 
			
		||||
@@ -83,7 +103,7 @@ class GpioPin
 | 
			
		||||
    
 | 
			
		||||
    public function getValue()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->value;
 | 
			
		||||
        return $this->sysfsRead($this->pin, "value");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -110,6 +130,9 @@ class GpioPin
 | 
			
		||||
    
 | 
			
		||||
    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}");
 | 
			
		||||
@@ -119,7 +142,7 @@ class GpioPin
 | 
			
		||||
    
 | 
			
		||||
    public function unexport()
 | 
			
		||||
    {
 | 
			
		||||
        @file_put_contents("/sys/class/gpio/unexport", $this->pin);
 | 
			
		||||
    	$this->sysfsWrite(null, "unexport", $this->pin);
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
@@ -150,7 +173,7 @@ class GpioPin
 | 
			
		||||
        }
 | 
			
		||||
        $ret = fgets($f);
 | 
			
		||||
        fclose($f);
 | 
			
		||||
        return $ret;
 | 
			
		||||
        return trim($ret);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function setEdge($edge)
 | 
			
		||||
@@ -202,11 +225,11 @@ class GpioPin
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
        foreach(array(    
 | 
			
		||||
            "Direction" => $direction,
 | 
			
		||||
            "Value" => ($this->value?1:0),
 | 
			
		||||
            "Edge" => $edge,
 | 
			
		||||
            "Direction" => $this->getDirection(),
 | 
			
		||||
            "Value" => ($this->getValue()?1:0),
 | 
			
		||||
            "Edge" => $this->getEdge(),
 | 
			
		||||
            "Label" => $this->label,
 | 
			
		||||
            "Hardware" => "unknown",
 | 
			
		||||
            "Hardware" => $this->hardware,
 | 
			
		||||
            "Int count" => 0
 | 
			
		||||
        ) as $k=>$v) {
 | 
			
		||||
            if ($ansi) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user