gpio_java.java 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package pdu_jctrl;
  2. import java.io.IOException;
  3. import java.util.concurrent.TimeUnit;
  4. public class gpio_java
  5. {
  6. public static void main (String [] args)
  7. {
  8. System.out.println("Hello World");
  9. setgpio(true);
  10. try {
  11. TimeUnit.SECONDS.sleep(1);
  12. }
  13. catch(InterruptedException e)
  14. {
  15. System.out.println("sleep error InterruptedException ocure");
  16. }
  17. setgpio(false);
  18. }
  19. public static void launchcmd(String cmd)
  20. {
  21. // Wenn Simulation True ist kann sie auf dem PC ausfuehren
  22. // wenn die Simulation false is auf dem Raspberry PI
  23. boolean simulation = true;
  24. if(simulation==true){
  25. System.out.println(cmd);
  26. }
  27. else {
  28. try{
  29. Runtime.getRuntime().exec(cmd);
  30. }
  31. catch(IOException e)
  32. {
  33. System.out.println("process error IOException");
  34. }
  35. }
  36. }
  37. public static void setgpio(boolean status)
  38. {
  39. launchcmd("/usr/bin/gpio -g mode 14 out");
  40. if(status == true)
  41. {
  42. launchcmd("/usr/bin/gpio -g write 14 1");
  43. }
  44. else
  45. {
  46. launchcmd("/usr/bin/gpio -g write 14 0");
  47. }
  48. }
  49. }