├── GPIO.php ├── README.mdown └── testGPIO.php /GPIO.php: -------------------------------------------------------------------------------- 1 | isValidPin($pinNo)) { 14 | // if exported, unexport it first 15 | if($this->isExported($pinNo)) { 16 | $this->unexport($pinNo); 17 | } 18 | 19 | // Export pin 20 | file_put_contents('/sys/class/gpio/export', $pinNo); 21 | 22 | // if valid direction then set direction 23 | if($this->isValidDirection($direction)) { 24 | file_put_contents('/sys/class/gpio/gpio'.$pinNo.'/direction', $direction); 25 | } 26 | 27 | // Add to exported pins array 28 | $exportedPins[] = $pinNo; 29 | } else { 30 | echo 'Error! Not a valid pin!'; 31 | } 32 | } 33 | 34 | public function input($pinNo) { 35 | if($this->isExported($pinNo)) { 36 | if($this->currentDirection($pinNo) != "out") { 37 | return file_get_contents('/sys/class/gpio/gpio'.$pinNo.'/value'); 38 | } else { 39 | echo 'Error! Wrong Direction for this pin!'; 40 | } 41 | } 42 | } 43 | 44 | // Value == 1 or 0, where 1 = on, 0 = off 45 | public function output($pinNo, $value) { 46 | if($this->isExported($pinNo)) { 47 | if($this->currentDirection($pinNo) != "in") { 48 | file_put_contents('/sys/class/gpio/gpio'.$pinNo.'/value', $value); 49 | } else { 50 | echo 'Error! Wrong Direction for this pin! Meant to be out while it is ' . $this->currentDirection($pinNo); 51 | } 52 | } 53 | } 54 | 55 | public function unexport($pinNo) { 56 | if($this->isExported($pinNo)) { 57 | file_put_contents('/sys/class/gpio/unexport', $pinNo); 58 | foreach ($this->exportedPins as $key => $value) { 59 | if($value == $pinNo) unset($key); 60 | } 61 | } 62 | } 63 | 64 | public function unexportAll() { 65 | foreach ($this->exportedPins as $key => $pinNo) file_put_contents('/sys/class/gpio/unexport', $pinNo); 66 | $this->exportedPins = array(); 67 | } 68 | 69 | // Check if exported 70 | public function isExported($pinNo) { 71 | return file_exists('/sys/class/gpio/gpio'.$pinNo); 72 | } 73 | 74 | public function currentDirection($pinNo) { 75 | return file_get_contents('/sys/class/gpio/gpio'.$pinNo.'/direction'); 76 | } 77 | 78 | // Check for valid direction, in or out 79 | public function isValidDirection($direction) { 80 | return (($direction == "in") || ($direction == "out")); 81 | } 82 | 83 | // Check for valid pin 84 | public function isValidPin($pinNo) { 85 | return in_array($pinNo, $this->pins); 86 | } 87 | } 88 | ?> -------------------------------------------------------------------------------- /README.mdown: -------------------------------------------------------------------------------- 1 | This is version 1.0 of the PHP-GPIO. 2 | 3 | It is a simple PHP library to alloww access to the GPIO on the Raspberry Pi. 4 | 5 | The code should be readable enough to not need a guide, check the testGPIO.php file for an example of basic use. 6 | 7 | --- 8 | 9 | ##Licensing 10 | 11 | Copyright (C) 2012 Aaron Pearce 12 | 13 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /testGPIO.php: -------------------------------------------------------------------------------- 1 | setup(17, "out"); 7 | $gpio->setup(22, "out"); 8 | 9 | echo "Turning on Pins 17 and 22\n"; 10 | $gpio->output(17, 1); 11 | $gpio->output(22, 1); 12 | 13 | echo "Sleeping!\n"; 14 | sleep(3); 15 | 16 | echo "Turning off Pins 17 and 22\n"; 17 | $gpio->output(17, 0); 18 | $gpio->output(22, 0); 19 | 20 | echo "Unexporting all pins\n"; 21 | $gpio->unexportAll(); 22 | 23 | ?> --------------------------------------------------------------------------------