rpi.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Controller for RPi web interface
  4. */
  5. namespace PHPPE\Ctrl;
  6. use PHPPE\Core;
  7. use PHPPE\View;
  8. class RPi {
  9. public $pins;
  10. /* ajax hook */
  11. function status($item)
  12. {
  13. foreach(Core::lib("GPIO")->pins as $idx=>$pin) {
  14. echo("pin".$pin."\t".
  15. "pin_".@Core::lib("GPIO")->hdlr[$idx]." ".
  16. "pin_".(Core::lib("GPIO")->get($idx)?"on":"off")."\n"
  17. );
  18. }
  19. die();
  20. }
  21. /* ajax hook */
  22. function toggle($item) {
  23. $item = intval(substr($item,3));
  24. try{
  25. Core::lib("GPIO")->set($item, Core::lib("GPIO")->get($item) ? false : true);
  26. die("OK");
  27. }catch(\Exception $e) {
  28. die("ERR ".$e->getMessage());
  29. }
  30. }
  31. function action($item)
  32. {
  33. if(!empty(Core::lib("GPIO"))) {
  34. //expand pin list with VCC, GND pins
  35. $this->pins = array_flip(Core::lib("GPIO")->pins) +
  36. [1=>"3.3V",2=>"5V",3=>2,4=>"5V",5=>3,6=>"GND",8=>14,9=>"GND",10=>15,14=>"GND",17=>"3.3V",20=>"GND",25=>"GND"];
  37. ksort($this->pins);
  38. Core::lib("GPIO")->hdlr += ["3.3V"=>"out","5V"=>"out","GND"=>"in"];
  39. foreach($this->pins as $pin=>$type) {
  40. $this->smenu[] = [
  41. "list_id" => "gpio",
  42. "id" => $pin,
  43. "name" => $type,
  44. "posx" => (($pin-1)%2)*100+20,
  45. "posy" => floor(($pin-1)/2)*40+100,
  46. "type" => $type=="GND"?"pin_gnd":(substr($type,-1)=="V"?"pin_volt":
  47. "pin_".@Core::lib("GPIO")->hdlr[$type]." pin_off")
  48. ];
  49. }
  50. //add css and js
  51. View::css("rpi.css");
  52. View::jslib("smenu.js",'pe.smenu.init({"link":"rpi_toggle(\"@ID\");", "url":"gpio", "callback":"/rpi/status", "refresh":"2000"});');
  53. View::js("rpi_toggle(id)", "var r = new XMLHttpRequest();r.open('GET', '/rpi/toggle/'+id, true);r.send(null);");
  54. } else {
  55. Core::error("Please run it on a Raspberry Pi");
  56. }
  57. }
  58. }