Improved sysfs file access code
This commit is contained in:
		@@ -19,6 +19,8 @@
 | 
			
		||||
 | 
			
		||||
namespace NoccyLabs\Gpio;
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Gpio\Exception\HardwareException;
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Sansi\Charset as CS;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -41,10 +43,13 @@ class GpioPin
 | 
			
		||||
    protected $handler;
 | 
			
		||||
 | 
			
		||||
    protected $label;
 | 
			
		||||
    
 | 
			
		||||
    protected $gpio;
 | 
			
		||||
 | 
			
		||||
    public function __construct($pin)
 | 
			
		||||
    public function __construct($pin, Gpio $gpio)
 | 
			
		||||
    {
 | 
			
		||||
        $this->pin = (int)$pin;
 | 
			
		||||
        $this->gpio = $gpio;
 | 
			
		||||
        $this->export();
 | 
			
		||||
        $this->fd = fopen("/sys/class/gpio/gpio{$pin}/value", "rb");
 | 
			
		||||
    }
 | 
			
		||||
@@ -80,13 +85,24 @@ class GpioPin
 | 
			
		||||
    {
 | 
			
		||||
        return $this->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;
 | 
			
		||||
@@ -94,9 +110,9 @@ class GpioPin
 | 
			
		||||
    
 | 
			
		||||
    public function export()
 | 
			
		||||
    {
 | 
			
		||||
        @file_put_contents("/sys/class/gpio/export", $this->pin);
 | 
			
		||||
        $this->sysfsWrite(null, "export", $this->pin);
 | 
			
		||||
        if (!file_exists("/sys/class/gpio/gpio{$this->pin}")) {
 | 
			
		||||
            throw new \Exception();
 | 
			
		||||
            throw new HardwareException("Unable to export pin {$this->pin}");
 | 
			
		||||
        }
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
@@ -107,23 +123,54 @@ class GpioPin
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function sysfsWrite($pin, $file, $value)
 | 
			
		||||
    {
 | 
			
		||||
        if ($pin) {
 | 
			
		||||
            $path = "/sys/class/gpio/gpio{$pin}/{$file}";
 | 
			
		||||
        } else {
 | 
			
		||||
            $path = "/sys/class/gpio/{$file}";
 | 
			
		||||
        }
 | 
			
		||||
        if (!is_writable($path)) {
 | 
			
		||||
            throw new HardwareException("Sysfs file {$path} not writable");
 | 
			
		||||
        }
 | 
			
		||||
        $f = fopen($path,"w+");
 | 
			
		||||
        if (!is_resource($f)) {
 | 
			
		||||
            throw new HardwareException("Unable to write to sysfs file {$path}");
 | 
			
		||||
        }
 | 
			
		||||
        fwrite($f,$value);
 | 
			
		||||
        fclose($f);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function sysfsRead($pin, $file)
 | 
			
		||||
    {
 | 
			
		||||
        $path = "/sys/class/gpio/gpio{$pin}/{$file}";
 | 
			
		||||
        $f = fopen($path,"r");
 | 
			
		||||
        if (!is_resource($f)) {
 | 
			
		||||
            throw new HardwareException("Unable to read from sysfs file {$path}");
 | 
			
		||||
        }
 | 
			
		||||
        $ret = fgets($f);
 | 
			
		||||
        fclose($f);
 | 
			
		||||
        return $ret;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function setEdge($edge)
 | 
			
		||||
    {
 | 
			
		||||
        if (!in_array($edge, array("rising", "falling", "both", "none"))) {
 | 
			
		||||
            throw new \Exception;
 | 
			
		||||
        }
 | 
			
		||||
        $this->edge = $edge;
 | 
			
		||||
        @file_put_contents("/sys/class/gpio/gpio{$this->pin}/edge", $edge);
 | 
			
		||||
        $this->sysfsWrite($this->pin, "edge", $edge);
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function getEdge()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->edge;
 | 
			
		||||
        return $this->sysfsRead($this->pin, "edge");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public function setHandler(callable $handler=null)
 | 
			
		||||
    {
 | 
			
		||||
        $this->gpio->setInterruptHandler($this->pin, $this->fd);
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user