import java.io.IOException; import java.util.Arrays; import java.util.concurrent.TimeUnit; // todo: add Dokumentation public class gpio_java { public static String rel_map [][] = { {"rel1","14"}, {"rel2","15"}, {"rel3","18"}, {"rel4","23"}, {"rel5","24"}, {"rel6","25"}, {"rel7","8"}, {"rel8","7"} }; public static String [] action = {"on", "off", "status"}; public static void main (String [] args) { // Zum checken der Anzahl der Argumente checkargs(args); // todo: initialisiere alle acht ports (outputs) init_gpio(); // setze gpio (gemaes der parameter) gpio_getport(args[0]); gpio_cmdhandler(args[0], args[1]); // todo: remove following not used code /* setgpio(true); try { TimeUnit.SECONDS.sleep(1); } catch(InterruptedException e) { System.out.println("sleep error InterruptedException ocure"); } setgpio(false); */ } public static void checkargs(String[] argumens) { int laenge = argumens.length; String [] h = {"-h"}; int idx; boolean parameter1 = false; boolean parameter2 = false; if (laenge == 2) { //ToDO: Pruefe erstes und zweites Argument //Erstes Element(rel) for (idx = 0; idx<8 ; idx++) { if (argumens[0].equals(rel_map[idx][0])) { parameter1 = true; break; } } //Zweites Element (action) for (idx = 0; idx<3 ; idx++) { if (argumens[1].equals(action[idx])) { parameter2 = true; break; } } } else if (Arrays.equals (argumens, h) == true) { show_usage(); System.exit(0); } else { System.out.println("Error. If you need help, type the command -h"); System.exit(0); } //ToDo: Return } public static void init_gpio() { int idx; String port; String cmd; for(idx = 0; idx<8 ; idx++) { port = rel_map[idx][1]; cmd = "/usr/bin/gpio -g mode " + port + " out"; launchcmd(cmd); } return; } public static int gpio_getport(String port) { int idx; for(idx = 0; idx<8 ; idx++) { if(port.equals(rel_map[idx][0])) { return idx; } } return -1; } public static int gpio_cmdhandler(String port, String cmd) { int idx_port; idx_port = gpio_getport(port); if (cmd.equals(action [0])) { //Schalte on cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 1"; launchcmd(cmd); } if (cmd.equals(action [1])) { //Schalte off cmd = "/usr/bin/gpio -g write " + rel_map[idx_port][1] + " 0"; launchcmd(cmd); } /*if (cmd.equals(action [2])) { //Stats geben cmd = "/usr/bin/gpio -g write " + port + status(); launchcmd(cmd); }*/ return -1; } public static void show_usage() { System.out.println("You have to enter exactly 2 arguments."); System.out.println("Example: java gpio_java Argument1 Argument2"); System.out.println("Argument1: rel1, rel2, rel3, ..., rel8"); System.out.println("Argument2: on,off or status"); } public static void launchcmd(String cmd) { // Wenn Simulation True ist kann sie auf dem PC ausfuehren // wenn die Simulation false is auf dem Raspberry PI boolean simulation = false; if(simulation==true){ System.out.println(cmd); } else { try{ Runtime.getRuntime().exec(cmd); } catch(IOException e) { System.out.println("process error IOException"); } } } public static void setgpio(boolean status) { launchcmd("/usr/bin/gpio -g mode 14 out"); if(status == true) { launchcmd("/usr/bin/gpio -g write 14 1"); } else { launchcmd("/usr/bin/gpio -g write 14 0"); } } }