Advertisement
Guest User

Untitled

a guest
Aug 9th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class GPIO {
  3.  
  4. // Using BCM pin numbers.
  5. private $pins = ['0', '1', '4', '7', '8', '9', '10', '11', '14', '15', '17', '18', '21', '22', '23', '24', '25'];
  6.  
  7. // exported pins for when we unexport all
  8. private $exportedPins = array();
  9.  
  10. // Setup pin, takes pin number and direction (in or out)
  11. public function setup($pinNo, $direction) {
  12. // Check if valid BCM number
  13. if($this->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. exec('gpio export '.$pinNo.' '.$direction);
  26. }
  27.  
  28. // Add to exported pins array
  29. $exportedPins[] = $pinNo;
  30. } else {
  31. echo 'Error! Not a valid pin!';
  32. }
  33. }
  34.  
  35. public function input($pinNo) {
  36. if($this->isExported($pinNo)) {
  37. if($this->currentDirection($pinNo) != "out") {
  38. return file_get_contents('/sys/devices/virtual/gpio/gpio'.$pinNo.'/value');
  39. } else {
  40. echo 'Error! Wrong Direction for this pin!';
  41. }
  42. }
  43. }
  44.  
  45. // Value == 1 or 0, where 1 = on, 0 = off
  46. public function output($pinNo, $value) {
  47. if($this->isExported($pinNo)) {
  48. if($this->currentDirection($pinNo) != "in") {
  49. file_put_contents('/sys/devices/virtual/gpio/gpio'.$pinNo.'/value', $value);
  50. } else {
  51. echo 'Error! Wrong Direction for this pin! Meant to be out while it is ' . $this->currentDirection($pinNo);
  52. }
  53. }
  54. }
  55.  
  56. public function unexport($pinNo) {
  57. if($this->isExported($pinNo)) {
  58. //file_put_contents('/sys/class/gpio/unexport', $pinNo);
  59. exec('gpio unexport '.$pinNo);
  60. foreach ($this->exportedPins as $key => $value) {
  61. if($value == $pinNo) unset($key);
  62. }
  63. }
  64. }
  65.  
  66. public function unexportAll() {
  67. //foreach ($this->exportedPins as $key => $pinNo) file_put_contents('/sys/class/gpio/unexport', $pinNo);
  68. //$this->exportedPins = array();
  69. exec('gpio unexportall');
  70. }
  71.  
  72. // Check if exported
  73. public function isExported($pinNo) {
  74. return file_exists('/sys/class/gpio/gpio'.$pinNo);
  75. }
  76.  
  77. public function currentDirection($pinNo) {
  78. return file_get_contents('/sys/class/gpio/gpio'.$pinNo.'/direction');
  79. }
  80.  
  81. // Check for valid direction, in or out
  82. public function isValidDirection($direction) {
  83. return (($direction == "in") || ($direction == "out"));
  84. }
  85.  
  86. // Check for valid pin
  87. public function isValidPin($pinNo) {
  88. return in_array($pinNo, $this->pins);
  89. }
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement