Gpio.class.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. @nom: Gpio
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Classe de gestion des gpio via wiring PI
  6. */
  7. class Gpio{
  8. const GPIO_DEFAULT_PATH = '/usr/bin/gpio';
  9. public $name,$role,$wiringPiNumber,$bcmNumber,$physicalNumber;
  10. function __construct($name,$role,$wiringPiNumber,$bcmNumber,$physicalNumber){
  11. $this->name = $name;
  12. $this->role = $role;
  13. $this->wiringPiNumber = $wiringPiNumber;
  14. $this->bcmNumber = $bcmNumber;
  15. $this->physicalNumber = $physicalNumber;
  16. }
  17. private static function system($cmd){
  18. // For compatibily with plugins wich call that method from GPIO instead of System.
  19. System::command($cmd);
  20. }
  21. public static function mode($pin,$mode = 'out'){
  22. return self::system(self::GPIO_DEFAULT_PATH.' mode '.$pin.' '.$mode);
  23. }
  24. public static function write($pin,$value = 0,$automode = false){
  25. if($automode) self::mode($pin,'out');
  26. return self::system(self::GPIO_DEFAULT_PATH.' write '.$pin.' '.$value);
  27. }
  28. public static function read($pin,$automode = false){
  29. if($automode) self::mode($pin,'in');
  30. return System::commandSilent(self::GPIO_DEFAULT_PATH.' read '.$pin);
  31. }
  32. public static function pulse($pin,$miliseconds,$state){
  33. Gpio::write($pin,$state);
  34. usleep($miliseconds);
  35. $state = $state == 1 ? 0 : 1;
  36. Gpio::write($pin,$state);
  37. }
  38. public static function emit($gpio, $state){
  39. if(isset($GLOBALS['gpio'][$gpio])) {
  40. foreach($GLOBALS['gpio'][$gpio] as $functionName) {
  41. call_user_func_array($functionName, array($gpio,$state));
  42. }
  43. }
  44. if(isset($GLOBALS['gpio']['all'])) {
  45. foreach($GLOBALS['gpio']['all'] as $functionName) {
  46. call_user_func_array($functionName, array($gpio,$state));
  47. }
  48. }
  49. }
  50. public static function listen($gpio,$functionName){
  51. $GLOBALS['gpio'][$gpio][] = $functionName;
  52. }
  53. }
  54. ?>