No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

gpio_java.java 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import java.io.IOException;
  2. import java.util.Arrays;
  3. import java.util.concurrent.TimeUnit;
  4. // todo: add Dokumentation
  5. public class gpio_java
  6. {
  7. public static String rel_map [][] = {
  8. {"rel1","14"},
  9. {"rel2","15"},
  10. {"rel3","18"},
  11. {"rel4","23"},
  12. {"rel5","24"},
  13. {"rel6","25"},
  14. {"rel7","8"},
  15. {"rel8","7"}
  16. };
  17. public static String [] action = {"on", "off", "status"};
  18. public static void main (String [] args)
  19. {
  20. // Zum checken der Anzahl der Argumente
  21. checkargs(args);
  22. // todo: initialisiere alle acht ports (outputs)
  23. init_gpio();
  24. // setze gpio (gemaes der parameter)
  25. gpio_getport(args[0]);
  26. gpio_cmdhandler(args[0], args[1]);
  27. // todo: remove following not used code
  28. /*
  29. setgpio(true);
  30. try {
  31. TimeUnit.SECONDS.sleep(1);
  32. }
  33. catch(InterruptedException e)
  34. {
  35. System.out.println("sleep error InterruptedException ocure");
  36. }
  37. setgpio(false);
  38. */
  39. }
  40. public static void checkargs(String[] argumens)
  41. {
  42. int laenge = argumens.length;
  43. String [] h = {"-h"};
  44. int idx;
  45. boolean parameter1 = false;
  46. boolean parameter2 = false;
  47. if (laenge == 2)
  48. {
  49. //ToDO: Pruefe erstes und zweites Argument
  50. //Erstes Element(rel)
  51. for (idx = 0; idx<8 ; idx++)
  52. {
  53. if (argumens[0].equals(rel_map[idx][0]))
  54. {
  55. parameter1 = true;
  56. break;
  57. }
  58. }
  59. //Zweites Element (action)
  60. for (idx = 0; idx<3 ; idx++)
  61. {
  62. if (argumens[1].equals(action[idx]))
  63. {
  64. parameter2 = true;
  65. break;
  66. }
  67. }
  68. }
  69. else if (Arrays.equals (argumens, h) == true)
  70. {
  71. show_usage();
  72. System.exit(0);
  73. }
  74. else
  75. {
  76. System.out.println("Error. If you need help, type the command -h");
  77. System.exit(0);
  78. }
  79. //ToDo: Return
  80. }
  81. public static void init_gpio()
  82. {
  83. int idx;
  84. String port;
  85. String cmd;
  86. for(idx = 0; idx<8 ; idx++)
  87. {
  88. port = rel_map[idx][1];
  89. cmd = "/usr/bin/gpio -g mode " + port + " out";
  90. launchcmd(cmd);
  91. }
  92. return;
  93. }
  94. public static int gpio_getport(String port)
  95. {
  96. int idx;
  97. for(idx = 0; idx<8 ; idx++)
  98. {
  99. if(port.equals(rel_map[idx][0]))
  100. {
  101. return idx;
  102. }
  103. }
  104. return -1;
  105. }
  106. public static int gpio_cmdhandler(String port, String cmd)
  107. {
  108. int idx_port;
  109. idx_port = gpio_getport(port);
  110. if (cmd.equals(action [0]))
  111. {
  112. //Schalte on
  113. cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 1";
  114. launchcmd(cmd);
  115. }
  116. if (cmd.equals(action [1]))
  117. {
  118. //Schalte off
  119. cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 0";
  120. launchcmd(cmd);
  121. }
  122. /*if (cmd.equals(action [2]))
  123. {
  124. //Stats geben
  125. cmd = "/usr/bin/gpio -g write " + port + status();
  126. launchcmd(cmd);
  127. }*/
  128. return -1;
  129. }
  130. public static void show_usage()
  131. {
  132. System.out.println("You have to enter exactly 2 arguments.");
  133. System.out.println("Example: java gpio_java Argument1 Argument2");
  134. System.out.println("Argument1: rel1, rel2, rel3, ..., rel8");
  135. System.out.println("Argument2: on,off or status");
  136. }
  137. public static void launchcmd(String cmd)
  138. {
  139. // Wenn Simulation True ist kann sie auf dem PC ausfuehren
  140. // wenn die Simulation false is auf dem Raspberry PI
  141. boolean simulation = false;
  142. if(simulation==true){
  143. System.out.println(cmd);
  144. }
  145. else {
  146. try{
  147. Runtime.getRuntime().exec(cmd);
  148. }
  149. catch(IOException e)
  150. {
  151. System.out.println("process error IOException");
  152. }
  153. }
  154. }
  155. public static void setgpio(boolean status)
  156. {
  157. launchcmd("/usr/bin/gpio -g mode 14 out");
  158. if(status == true)
  159. {
  160. launchcmd("/usr/bin/gpio -g write 14 1");
  161. }
  162. else
  163. {
  164. launchcmd("/usr/bin/gpio -g write 14 0");
  165. }
  166. }
  167. }