Azuria

Raspberry Pi - php class controlling GPIO - GPIO.php

Sep 27th, 2012
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. ## http://stjernemor.dk
  4.  
  5. class GPIO {
  6.  
  7.    private $_path;
  8.    private $_dir;
  9.    private $_export;
  10.    private $_unexport;
  11.    private $_value;
  12.    private $_direction;
  13.    private $_pin;
  14.    private $_mappedPins;
  15.  
  16.    public function __construct($path = "/sys/class/gpio/", $mPins = false)
  17.    {
  18.    $this->_path = $path;
  19.  
  20.       if (!is_array($mPins))
  21.    $this->_mappedPins = array("1" => "0",
  22.                "2" => "7",
  23.                "3" => "8",
  24.                "4" => "18",
  25.                "5" => "1",
  26.                "6" => "17",
  27.                "7" => "14",
  28.                "8" => "15",
  29.                "9" => "22",
  30.                "10" => "23",
  31.                "11" => "24",
  32.                "12" => "10",
  33.                "13" => "25",
  34.                "14" => "9",
  35.                "15" => "11",
  36.                "16" => "4");
  37.    else
  38.       $this->_mappedPins = $mPins;
  39.                
  40.    }
  41.  
  42.    public function Pins()
  43.    {
  44.       return $this->_mappedPins;
  45.    }
  46.  
  47.    public function Active($pin)
  48.    {
  49.    $pin = $this->_mappedPins[$pin];
  50.    
  51.    $this->_dir = $this->_path . "gpio" . $pin;
  52.         $this->_value = $this->_dir . "/value";
  53.    $this->_export = $this->_path . "export";
  54.    $this->_direction = $this->_dir . "/direction";
  55.         $this->_value = $this->_dir . "/value";
  56.         $this->_pin = $pin;
  57.    }
  58.  
  59.    public function Value($pin, $value = 0)
  60.    {
  61.  
  62.    $value = $this->FixValue($value);
  63.  
  64.    $this->Active($pin);
  65.  
  66.    if (!is_dir($this->_dir))
  67.       $this->Export();
  68.  
  69.    $this->Write($this->_value, $value);
  70.    }
  71.  
  72.    public function Export($direction = "out")
  73.    {
  74.    if (!is_dir($this->_dir))
  75.      $this->Write($this->_export, $this->_pin);
  76.  
  77.    $this->Write($this->_direction, $direction);
  78.    }
  79.  
  80.    public function Unexport($pin)
  81.    {
  82.    $this->Active($pin);
  83.  
  84.    $this->_unexport = $this->_path . "unexport";
  85.    $this->Write($this->_unexport, $this->_pin);
  86.    }
  87.  
  88.    public function Status($pin)
  89.    {
  90.    $this->Active($pin);
  91.    return $this->FixValue($this->Read($this->_value));
  92.    
  93.    }
  94.  
  95.    public function StatusAll()
  96.    {
  97.       $json_data = array();
  98.  
  99.       foreach($this->Pins() as $pin => $value)
  100.    $json_data[$pin] = $this->Status($pin);
  101.  
  102.       return json_encode($json_data);
  103.    }
  104.  
  105.    public function Read($path)
  106.    {
  107.       $cmd = ("/usr/bin/sudo /bin/sh -c 'cat $path'");
  108.       return shell_exec($cmd);
  109.    }
  110.  
  111.    public function Write($path, $value)
  112.    {
  113.       $cmd = ("/usr/bin/sudo /bin/sh -c 'echo \"$value\" > $path'\n");
  114.       echo $value;
  115.       shell_exec($cmd);
  116.    }
  117.  
  118.    public function On()
  119.    {
  120.       foreach ($this->_mappedPins as $pin => $value)
  121.        $this->Value($pin, 1);
  122.    }
  123.  
  124.    public function Off()
  125.    {
  126.       foreach ($this->_mappedPins as $pin => $value)
  127.        $this->Value($pin, 0);
  128.    }
  129.  
  130.    public function Test($delay = 1)
  131.    {
  132.       foreach ($this->_mappedPins as $pin => $value)
  133.       {
  134.        $this->Value($pin, 1);
  135.          sleep($delay);
  136.        $this->Value($pin, 0);
  137.       }
  138.  
  139.    }
  140.  
  141.    //GPIO pins are backwards 1 is off 0 is on so I swapped them.
  142.  
  143.    public function FixValue($value)
  144.    {
  145.    if ($value == 1)
  146.      $value = 0;
  147.         else
  148.      $value = 1;
  149.  
  150.       return $value;
  151.    }
  152. }
  153.  
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment